Configuration¶
Every setting resolves through a config chain:
builder > JVM system property > environment variable > ~/.dashscope/config.toml > default
wsBaseUrl has no config-file key; raiseOnError, httpClient and executor are builder-only.
The API key additionally falls back to the 1.x key file (DASHSCOPE_API_KEY_FILE_PATH or
~/.dashscope/api_key), so DashScopeClient.builder().build() works with just
DASHSCOPE_API_KEY exported.
Client options¶
Client-level options (DashScopeClient.builder() / DashScopeAsyncClient.builder()):
| Builder method | Environment variable | Default | Description |
|---|---|---|---|
apiKey(...) |
DASHSCOPE_API_KEY |
— | API key (config chain above) |
baseUrl(...) |
DASHSCOPE_BASE_URL / DASHSCOPE_HTTP_BASE_URL |
region endpoint | HTTP base URL |
wsBaseUrl(...) |
DASHSCOPE_WEBSOCKET_BASE_URL |
region endpoint | WebSocket base URL |
workspace(...) |
DASHSCOPE_WORKSPACE |
— | Workspace id (required by some APIs, e.g. decision) |
region(...) |
DASHSCOPE_REGION |
cn-beijing |
MaaS gateway region |
timeout(...) |
DASHSCOPE_TIMEOUT |
5 minutes | Single-request timeout (Duration) |
maxRetries(...) |
DASHSCOPE_MAX_RETRIES |
2 | Automatic retries on retryable failures |
raiseOnError(...) |
— | true |
false switches to 2.x-style isError() returns |
wsPingInterval(...) |
DASHSCOPE_WS_PING_INTERVAL |
0 (off) | WebSocket keepalive ping; 20–30 s behind NAT/proxies |
httpClient(...) |
— | SDK-owned | Inject your own OkHttpClient (you own its lifecycle) |
executor(...) |
— | SDK-owned | Inject the async executor (you own its lifecycle) |
try (DashScopeClient client = DashScopeClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.workspace("llm-xxxx")
.timeout(Duration.ofSeconds(60))
.maxRetries(3)
.build()) {
// ...
}
Each setting also has a JVM system property (e.g. -Ddashscope.api.key=sk-...,
-Ddashscope.base.url=http://127.0.0.1:8000, -Ddashscope.timeout=60), which sits between the
builder and the environment variable in the chain.
Config file¶
The config file is a flat key = value TOML subset; duration values are in seconds.
Its path defaults to ~/.dashscope/config.toml and can be overridden with DASHSCOPE_CONFIG:
# ~/.dashscope/config.toml
api_key = "sk-..."
base_url = "https://dashscope.aliyuncs.com"
workspace = "llm-xxxx"
region = "cn-beijing"
timeout = 300 # seconds
max_retries = 2
ws_ping_interval = 0 # seconds, 0 = off
Per-request overrides¶
Every resource method has a (params, RequestOptions) overload. Null fields inherit the client
config.
import com.alibaba.dashscope.RequestOptions;
import java.time.Duration;
RequestOptions options = RequestOptions.builder()
.timeout(Duration.ofSeconds(30))
.maxRetries(0)
.header("X-Custom-Header", "value")
.enableEncryption(true) // DashScope encrypted HTTP channel for this call
.build();
client.chat().completions().create(params, options);
RequestOptions supports timeout, maxRetries, apiKey, workspace, extra headers,
enableEncryption, and wsQueueCapacity (bounded WebSocket event buffering).
Encrypted channel¶
enableEncryption(true) turns on the DashScope encrypted HTTP channel (RSA envelope
encryption) for that call — useful for sensitive payloads. End-to-end example:
ChatCompletionsCreateEncrypted.
Full runnable example: ChatCompletionsCreateEncrypted.java (auto-synced)
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.examples.atomic.chat;
import com.alibaba.dashscope.DashScopeClient;
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.examples.ExampleSupport;
import com.alibaba.dashscope.types.completions.ChatCompletion;
import com.alibaba.dashscope.types.completions.ChatCompletionCreateParams;
import com.alibaba.dashscope.types.messages.Message;
import java.util.Arrays;
/**
* Encrypted chat channel (J7): {@code RequestOptions.enableEncryption(true)} fetches the server's
* latest RSA public key ({@code GET /public-keys/latest}), wraps a fresh per-request AES key with
* it, AES-GCM-encrypts the sensitive {@code input} part, and decrypts the encrypted {@code output}
* transparently. The call shape is identical to {@link ChatCompletionsCreate}.
*
* <p>Encryption is per-request: the same client can mix plain and encrypted calls.
*/
public final class ChatCompletionsCreateEncrypted {
public static void main(String[] args) throws Exception {
String model = System.getenv().getOrDefault("DASHSCOPE_MODEL", "qwen3.7-plus");
try (DashScopeClient client = ExampleSupport.client()) {
System.out.println("----- encrypted request -----");
ChatCompletion resp =
client
.chat()
.completions()
.create(
ChatCompletionCreateParams.builder()
.model(model)
.messages(
Arrays.asList(
Message.system("You are a helpful assistant."),
Message.user("请用一句话介绍杭州")))
.build(),
RequestOptions.builder().enableEncryption(true).build());
// outputText() is already decrypted by the SDK.
System.out.println(resp.outputText());
System.out.println("request_id: " + resp.getRequestId());
}
}
}
Custom HTTP client & executor¶
Inject your own OkHttpClient to control proxies, connection pools, or interceptors — when you
inject it, you own its lifecycle (the SDK will not shut it down on close()). The same
applies to executor(...) on the async client.
OkHttpClient http = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.corp", 8080)))
.build();
try (DashScopeClient client = DashScopeClient.builder().httpClient(http).build()) {
// ...
}
Logging & telemetry¶
- Wire logging (environment variable only):
DASHSCOPE_NETWORK_LOGGING_LEVEL=BASIC,HEADERS, orBODY(defaultNONE). Credential headers are always redacted. - Telemetry opt-out:
DASHSCOPE_DISABLE_SDK_HEADERS=1(ortrue) drops thex-dashscope-sdk-client/x-dashscope-sdk-session-idtracking headers;User-Agentand caller-set headers are unaffected.
AgentStudio endpoint¶
The 3.0 baseUrl does not apply to AgentStudio. Its endpoint is overridden separately with
DASHSCOPE_AGENTSTUDIO_URL.