English | 中文
- SDK Integration
- Requirements
- Securely Configure Access Credentials
- Credentials
- Endpoint Configuration
- HTTP Connection Pool
- HTTPS Request Configuration
- HTTP(S) Proxy
- Timeouts
- Retries
- Error Handling
- Debugging
When calling APIs, it is recommended to integrate the SDK in your project. Using the SDK simplifies development, speeds up integration, and reduces long-term maintenance costs. Volcengine SDK integration typically includes three steps: importing the SDK, configuring access credentials, and writing API call code. This document explains common scenarios and best practices.
- The SDK requires Java 1.8.0_131+. Download from: http://developers.sun.com/downloads/.
- If your Java version is Java 9 or later, add
javax.annotation-apibecause it was removed from JDK 9+.
To prevent credential leakage, do not hardcode credentials in plaintext in your code. Volcengine provides multiple secure ways to load credentials, such as environment variables.
⚠️ NoteEnvironment variables configured with
exportare only effective for the current session. They will be lost when the session ends. To persist them, add the exports to your shell startup scripts.
| Key | Command |
|---|---|
VOLCENGINE_ACCESS_KEY |
export VOLCENGINE_ACCESS_KEY=yourAccessKeyID |
VOLCENGINE_SECRET_KEY |
export VOLCENGINE_SECRET_KEY=yourSecretAccessKey |
VOLCENGINE_SESSION_TOKEN |
export VOLCENGINE_SESSION_TOKEN=yourSessionToken |
Verify: run echo $VOLCENGINE_ACCESS_KEY. If it returns the correct value, the configuration is effective.
Two options are provided: GUI setup and command line setup.
Verify: open Command Prompt and run echo %VOLCENGINE_ACCESS_KEY%, echo %VOLCENGINE_SECRET_KEY%, echo %VOLCENGINE_SESSION_TOKEN%. If the returned values are correct, the configuration is effective.
On Windows 10, right-click This PC -> Properties -> Advanced system settings -> Environment Variables -> System variables/User variables -> New, then set:
| Variable | Example |
|---|---|
| AccessKey Id | Name: VOLCENGINE_ACCESS_KEY Value: ***** |
| AccessKey Secret | Name: VOLCENGINE_SECRET_KEY Value: ***** |
| Session Token | Name: VOLCENGINE_SESSION_TOKEN Value: ***** |
Run Command Prompt as Administrator, and add environment variables:
setx VOLCENGINE_ACCESS_KEY yourAccessKeyID /M
setx VOLCENGINE_SECRET_KEY yourAccessKeySecret /M
setx VOLCENGINE_SESSION_TOKEN yourSessionToken /M
⚠️ Note
/Mmeans system-level variables. You may omit it for user-level variables.
Volcengine SDK supports three common authentication mechanisms: AK/SK, STS temporary credentials, and AssumeRole.
You can refer to: Environment Variable Setup
AK/SK is a pair of permanent access keys created in the Volcengine console. The SDK signs each request to authenticate.
⚠️ Notes
- Do not embed or expose AK/SK in client-side applications.
- Use a configuration center or environment variables.
- Follow least privilege principles.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String ak = "Your AK";
String sk = "Your SK";
String region = "cn-beijing";
// 1. Using static AK/SK may leak credentials; do not use in production.
Credentials akSkCredential = Credentials.getCredentials(ak, sk);
// 2. Recommended in production: read from env vars: VOLCENGINE_ACCESS_KEY / VOLCENGINE_SECRET_KEY
// Credentials akSkCredential = Credentials.getEnvCredentials();
ApiClient apiClient = new ApiClient()
.setCredentials(akSkCredential)
.setRegion(region);
}
}STS (Security Token Service) provides temporary credentials (temporary AK/SK and Token).
⚠️ Notes
- Least privilege.
- Use a reasonable TTL. Shorter is safer; avoid exceeding 1 hour.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String ak = "Your AK";
String sk = "Your SK";
String sessionToken = "Your Session Token";
String region = "cn-beijing";
Credentials sessionTokenCredential = Credentials.getCredentials(ak, sk, sessionToken);
// Credentials sessionTokenCredential = Credentials.getEnvCredentials();
ApiClient apiClient = new ApiClient()
.setCredentials(sessionTokenCredential)
.setRegion(region);
}
}AssumeRole supports dynamic credentials with auto refresh.
⚠️ Notes
- Least privilege.
- Choose a reasonable TTL; maximum is 12 hours.
- Use fine-grained roles and policies.
import com.volcengine.auth.CredentialProvider;
import com.volcengine.auth.StsAssumeRoleProvider;
public class SampleCode {
public static void main(String[] args) {
String region = "cn-beijing";
StsAssumeRoleProvider stsAssumeRoleProvider = new StsAssumeRoleProvider(
"YourAccessKey",
"YourSecretKey",
"YourRoleName",
"YourAccountId");
// Optional fields
stsAssumeRoleProvider.setHost("sts.volcengineapi.com");
stsAssumeRoleProvider.setRegion("cn-north-1");
stsAssumeRoleProvider.setTimeout(30);
stsAssumeRoleProvider.setDurationSeconds(3600);
stsAssumeRoleProvider.setExpireBufferSeconds(60);
stsAssumeRoleProvider.setSchema("https");
CredentialProvider credentialProvider = new CredentialProvider(stsAssumeRoleProvider);
ApiClient apiClient = new ApiClient()
.setCredentialProvider(credentialProvider)
.setRegion(region);
}
}
- Default: if
endpointis not specified, the SDK uses Automatic Endpoint Resolution.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String region = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(region)
.setEndpoint("<example>.<regionId>.volcengineapi.com");
}
}import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId);
}
}Volcengine provides a flexible endpoint resolution mechanism. The SDK automatically builds the endpoint based on service name and region, and supports DualStack.
- Whether region is in the bootstrap list.
- Built-in list:
./volcengine-java-sdk-core/src/main/java/com/volcengine/endpoint/DefaultEndpointProvider.java#BOOTSTRAP_REGION. - Only predefined regions (e.g.,
cn-beijing-autodriving,ap-southeast-2) or user-configured regions are auto-resolved; others fall back toopen.volcengineapi.com. - You can extend the list via env var
VOLC_BOOTSTRAP_REGION_LIST_CONForcustomBootstrapRegion.
- Built-in list:
- DualStack support (IPv6)
- Enable via
setUseDualStack(true)or env varVOLC_ENABLE_DUALSTACK=true. - When enabled, the suffix changes from
volcengineapi.comtovolcengine-api.com.
- Enable via
- Construct endpoint:
- Global services:
<service>.volcengineapi.com. - Regional services:
<service>.<region>.volcengineapi.com.
- Global services:
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
import java.util.HashSet;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId)
.setUseDualStack(true)
.setCustomBootstrapRegion(new HashSet<String>() {{
add("custom_example_region1");
add("custom_example_region2");
}});
}
}| Global service | DualStack | Format |
|---|---|---|
| Yes | Yes | {Service}.volcengine-api.com |
| Yes | No | {Service}.volcengineapi.com |
| No | Yes | {Service}.{region}.volcengine-api.com |
| No | No | {Service}.{region}.volcengineapi.com |
Whether a service is global depends on the service itself and cannot be changed. See: ./volcengine-java-sdk-core/src/main/java/com/volcengine/endpoint/StandardEndpointProvider.java#SERVICE_INFOS.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
import com.volcengine.endpoint.StandardEndpointProvider;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setEndpointResolver(new StandardEndpointProvider())
.setRegion(regionId)
.setUseDualStack(true);
}
}Default
maxIdleConns: 5keepAliveDurationMs:5 * 60 * 1000
The Java SDK uses com.squareup.okhttp.ConnectionPool. You can adjust the pool behavior via these two parameters.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId)
.setMaxIdleConns(5)
.setKeepAliveDurationMs(5 * 60 * 1000);
}
}Default:
disableSSL=false(HTTPS)
Set disableSSL=true to use HTTP.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId)
.setDisableSSL(false);
}
}Default:
verifyingSsl=true
Set verifyingSsl=false to skip SSL verification.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId)
.setVerifyingSsl(false);
}
}Default: no proxy
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId)
.setHttpProxy("http://your_http_proxy:proxy_port")
.setHttpsProxy("http://your_https_proxy:proxy_port");
}Supported environment variables:
http_proxy/HTTP_PROXY,https_proxy/HTTPS_PROXY
Priority: code > environment variables.
Default (milliseconds)
connectTimeout:10 * 1000writeTimeout:10 * 1000readTimeout:10 * 1000
The Java SDK client uses com.squareup.okhttp.OkHttpClient. Configure timeouts via these parameters.
import com.volcengine.ApiClient;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String regionId = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(regionId)
.setConnectTimeout(10 * 1000)
.setReadTimeout(15 * 1000)
.setWriteTimeout(15 * 1000);
}
}Default
autoRetry:trueminRetryDelayMs:300maxRetryDelayMs:300 * 1000retryCondition:com.volcengine.retryer.DefaultRetryConditionbackoffStrategy:com.volcengine.retryer.ExponentialBackoffStrategyretryErrorCode: empty set
The SDK retries on network errors and throttling. Business logic errors are not retried.
import com.volcengine.ApiClient;
import com.volcengine.retryer.DefaultRetryCondition;
import com.volcengine.retryer.ExponentialBackoffStrategy;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String region = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(region)
.setDebugging(true)
.setAutoRetry(true)
.setNumMaxRetries(3)
.setMinRetryDelayMs(1000)
.setMaxRetryDelayMs(3000)
.setRetryCondition(new DefaultRetryCondition())
.setBackoffStrategy(new ExponentialBackoffStrategy())
.addRetryErrorCode("InvalidAuthorization");
}
}The default retry condition includes:
- Retry on network errors.
- Retry on server throttling errors.
- Retry on user-specified error codes (
retryErrorCodes).
Built-in strategies:
| Name | Description | Formula |
|---|---|---|
NoBackoffStrategy |
No backoff, retry immediately | delay=0.0 |
ExponentialBackoffStrategy |
Exponential backoff with bounds | delay=min(minRetryDelay*2^n, maxRetryDelay) |
ExponentialWithRandomJitterBackoffStrategy |
Exponential with jitter | base=min(minRetryDelay*2^n, maxRetryDelay); delay=base+U(0,base) |
Errors are categorized as client errors, network/timeout errors, server errors, and other errors. (See the original Chinese guide for complete code samples.)
Default:
debug=false
Java SDK logging uses SLF4J. Configure your own logging settings. Example:
<!-- enable debug logs -->
<logger name="com.volcengine.sdkcore" level="debug"/>