Call Machine Translation with the REST API

更新时间:
复制 MD 格式

Translate text between languages by sending JSON requests to the Machine Translation REST API.

Endpoint

Region

Endpoint

China (Hangzhou)

mt.cn-hangzhou.aliyuncs.com

API editions

Two editions target different content types:

Edition

Path

Best for

Scene value

General edition

/api/translate/web/general

General-purpose text

general

E-commerce edition

/api/translate/web/ecommerce

Product titles, descriptions, and reviews

title

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]().

Important

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

FormatType

string

Yes

Format of the source text. Valid values: text (plain text) and html (HTML content).

SourceLanguage

string

Yes

Language code of the source text, such as en or zh.

TargetLanguage

string

Yes

Language code to translate into, such as zh or en.

SourceText

string

Yes

Text to translate.

Scene

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"
  }'
Important

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-access-key-id>

Your AccessKey ID

LTAI5tXxx

<your-access-key-secret>

Your AccessKey secret

xXxXxXx

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

Code

string

Error code that identifies the issue.

Message

string

Human-readable error description.

RequestId

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"
}
Note

Authentication-level errors (such as missing or invalid signatures) return an XML response instead of JSON. Ensure your error handling accounts for both formats.