Use the KMS instance SDK client to call the GetSecretValue API and retrieve a secret value stored in your KMS instance.
Prerequisites
Before you begin, make sure you have:
A KMS instance with at least one secret created
A client key file and its password — download the client key from the KMS instance details page in the console
The endpoint of your KMS instance — the format is
<KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.comThe certificate authority (CA) certificate of your KMS instance — download it from the instance details page
The
GetSecretValuepermission on the target secret
Complete example
How it works
The example has two main parts:
1. Initialize the client (reference)
initClient() constructs a Config object with your KMS instance credentials and endpoint, then creates a Client instance. Initialize the client once at application startup and reuse it across all subsequent API calls.
2. Call GetSecretValue (API reference)
getSecretValueSample() builds a GetSecretValueRequest with the secret name and calls client.getSecretValue(). The response includes:
getSecretName()— the name of the secretgetSecretData()— the secret value (commented out in the example to avoid accidental logging)getVersionStages()— the version stages associated with the returned versiongetRequestId()— the request ID for debugging
If the call fails, a TeaException is thrown. The catch block prints the error code, message, HTTP status code, host ID, and request ID to help diagnose the issue.
What's next
Learn how the client is initialized: Initialize the client
See the full API specification: GetSecretValue API reference