Translate text between languages by sending JSON requests to the Machine Translation REST API.
Endpoint
|
Region |
Endpoint |
|
China (Hangzhou) |
|
API editions
Two editions target different content types:
|
Edition |
Path |
Best for |
Scene value |
|
General edition |
|
General-purpose text |
|
|
E-commerce edition |
|
Product titles, descriptions, and reviews |
|
Full request URL pattern:
http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/{edition}
Authentication
Sign every request with an AccessKey ID and AccessKey secret. The signing process is covered in [Signature method]().
Store your AccessKey credentials in environment variables or a configuration file. Do not hard-code them in source code.
Request format
Send a POST request with a JSON body. Both requests and responses use UTF-8 encoding.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
string |
Yes |
Format of the source text. Valid values: |
|
|
string |
Yes |
Language code of the source text, such as |
|
|
string |
Yes |
Language code to translate into, such as |
|
|
string |
Yes |
Text to translate. |
|
|
string |
Yes |
Translation context. Valid values are listed in the API editions table. |
Example request (curl)
This example translates "Hello" from English to Chinese with the E-commerce edition:
curl -X POST "http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/ecommerce" \
-H "Content-Type: application/json; charset=UTF-8" \
-d '{
"FormatType": "text",
"SourceLanguage": "en",
"TargetLanguage": "zh",
"SourceText": "Hello",
"Scene": "title"
}'
This example omits authentication headers. Without valid signature headers, the request returns an error. Sign every request before sending it as described in [Signature method]().
Example request (Java)
public class SampleRequest {
public static void main(String[] args) {
String serviceURL = "http://mt.cn-hangzhou.aliyuncs.com/api/translate/web/ecommerce";
String accessKeyId = "<your-access-key-id>"; // AccessKey ID
String accessKeySecret = "<your-access-key-secret>"; // AccessKey secret
String postBody = "{\n" +
" \"FormatType\": \"text\",\n" +
" \"SourceLanguage\": \"en\",\n" +
" \"TargetLanguage\": \"zh\",\n" +
" \"SourceText\": \"Hello\",\n" +
" \"Scene\": \"title\"\n" +
"}";
// For the signature helper code, see "Signature method"
String result = Sender.sendPost(serviceURL, postBody, accessKeyId, accessKeySecret);
System.out.println(result);
}
}
Replace the placeholders with your actual values:
|
Placeholder |
Description |
Example |
|
|
Your AccessKey ID |
|
|
|
Your AccessKey secret |
|
Response format
Success response
A successful request returns a JSON string. Response fields vary by edition.
Error response
A failed request returns a JSON object with these fields:
|
Field |
Type |
Description |
|
|
string |
Error code that identifies the issue. |
|
|
string |
Human-readable error description. |
|
|
string |
Unique request identifier. Provide this value when contacting support. May be absent in some error responses. |
Example error response:
{
"Code": "10004",
"Message": "Parameter error"
}
Authentication-level errors (such as missing or invalid signatures) return an XML response instead of JSON. Ensure your error handling accounts for both formats.