Obtain an RSA public key
Use this API operation to retrieve a public key ID and its value.
HTTP API call
Description
Before encrypting the value of the input field, call this operation to obtain the latest supported public key ID and its value.
Prerequisites
- You have activated Alibaba Cloud Model Studio and obtained an API key. For more information, see Obtain and configure an API key.
- Set the API key as an environment variable to reduce the threat of key leakage. For instructions, see Set an API key as an environment variable (to be unpublished and merged into "Get and configure an API key"). You can also configure the API key in your code, but this increases the threat of leakage.
API call
GET /api/v1/public-keys/latest
Request parameters
Parameter passing | Field | Data type | Required | Description | Example |
|---|---|---|---|---|---|
Header | Authorization | String | Yes | The API key for authentication. | "Authorization":"Bearer d1**2a" |
Response parameters
Field | Data type | Required | Description |
|---|---|---|---|
request_id | String | Yes | The request ID. |
public_key | String | No | The value of the RSA public key. |
public_key_id | String | No | The ID of the RSA public key. |
Error response parameters
Name | Type | Required | Description |
|---|---|---|---|
request_id | String | Yes | The request ID. |
code | String | No | The error code and error message. For more information, see Error messages. |
message | String | No |
Request example
curl
curl -X GET 'https://dashscope.aliyuncs.com/api/v1/public-keys/latest' \
--header 'Authorization: Bearer xxx'
Python SDK
When using the DashScope SDK, you do not need to manually call this API to obtain the public key. When calling a model, set enable_encryption=True, and the SDK will automatically obtain the latest RSA public key, generate an AES key, and encrypt the input. You do not need to handle public key retrieval or encryption/decryption details.
import os
from dashscope import Generation
resp = Generation.call(
model='qwen-plus',
input={'messages': [{'role': 'user', 'content': 'Hello'}]},
api_key=os.getenv('DASHSCOPE_API_KEY'),
enable_encryption=True,
)
print(resp)
Successful response example
{
"request_id": "bcb3***",
"data": {
"public_key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnojrB579xgPQN5f46SvoRAiQBPWBaPzWh7hp51fWI+OsQk7KqH0qMcw8i0eK5rfOvJIPujOQgnes1ph9/gKAst9NzXVIl9JJYUSPtzTvOabhp4yvS3KBf9g3xHYVjYgW33SOY74Ue/tgbCXn717rV6gXb4sVvq9XK/1BrDcGbEOQEZEgBTFkm/g3lpWLQtACwwqHffoA9eQtkkz15ZFKosAgbR8LedfIvxAl2zk15REzxXiRcFgc9/tLF0U1t2Sxt9FkQefxYwn6EZawTsRJvf4kqF3MaPdTcDbOp0iSNvCl2qzPSf/F+Oll2CUM1tFAEu81oa4l0WaDR3UtvqOtyQIDAQAB",
"public_key_id": "1"
}
}
Error response example
{
"request_id": "bcb3***",
"code": "InvalidParameter",
"message": "Required parameter(s) missing or invalid, please check the request parameters."
}
Error codes
For more information about error codes, see Error messages.