跳到主要内容

文本翻译 API

使用 GPT Translator API 翻译文本。

端点​

POST https://api.gpttranslator.co/v1/translation/text-translation/api

身份验证​

在请求头中添加您的 API 密钥:

x-api-key: YOUR_API_KEY
Content-Type: application/json

请求体​

必填字段:

  • text 字符串:要翻译的源文本
  • sourceLang 字符串:源语言代码
  • targetLang 字符串:目标语言代码
  • model 字符串:翻译模型名称

可选字段:

  • domain 字符串:专业领域
  • writingStyle 字符串:写作风格偏好
  • tone 字符串:语气偏好
  • customPrompt 字符串:自定义翻译指令

语言代码另见支持的语言。 支持的模型见支持的模型。

验证规则​

  • text:必填,不能为空。
  • sourceLang、targetLang:必须匹配 ^[a-zA-Z]+(-[a-zA-Z]+)?$,最少 2 个字符,最多 11 个字符。必须是支持的语言中的代码。
  • model:必须是支持的模型中的值之一。

示例​

cURL​

curl --location 'https://api.gpttranslator.co/v1/translation/text-translation/api' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"text": "hello world",
"sourceLang": "en",
"targetLang": "fr",
"model": "gpt-4.1-mini-2025-04-14"
}'

JavaScript​

const response = await fetch(
"https://api.gpttranslator.co/v1/translation/text-translation/api",
{
method: "POST",
headers: {
"x-api-key": "YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
text: "hello world",
sourceLang: "en",
targetLang: "fr",
model: "gpt-4.1-mini-2025-04-14",
}),
},
);

const data = await response.json();
console.log(data);

Python​

import requests

url = "https://api.gpttranslator.co/v1/translation/text-translation/api"
headers = {
"x-api-key": "YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"text": "hello world",
"sourceLang": "en",
"targetLang": "fr",
"model": "gpt-4.1-mini-2025-04-14"
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

响应类型​

成功 200​

{
"message": "Text translation completed successfully",
"error": null,
"data": {
"translatedText": "bonjour le monde",
"originalText": "hello world",
"totalWords": 2,
"isRTL": false,
"tokensConsumed": 3
},
"statusCode": 200
}

请求验证错误 400​

验证中间件错误以如下格式返回:

{
"error": "Invalid source language. Please select a supported language."
}

其他可能的验证错误:

  • Invalid target language. Please select a supported language.
  • Invalid model type. Please enter a valid model type e.g. ...
  • Text cannot be empty.
  • Text is required.

缺少 API 密钥/请求头无效 401​

{
"message": "Invalid api key",
"error": "Invalid api key",
"statusCode": 401,
"data": null
}

API 密钥或用户设置错误 400​

数据库中的密钥无效:

{
"message": "Invalid api key",
"error": "Invalid api key",
"statusCode": 400,
"data": null
}

用户未激活:

{
"message": "User not found or user is not active",
"error": "User not found or user is not active",
"statusCode": 400,
"data": null
}

缺少组织:

{
"message": "Organization Id not found for the user",
"error": "Organization Id not found for the user",
"statusCode": 400,
"data": null
}

套餐限制 402​

{
"message": "only subscribed users are allowed to use this model",
"error": "only subscribed users are allowed to use this model",
"data": null,
"statusCode": 402
}

已达到 Token 上限 402​

{
"message": "You have reached the limit of words for translation. Please upgrade your plan to continue using this feature.",
"error": "You have reached the limit of words for translation. Please upgrade your plan to continue using this feature.",
"data": {
"requiredTokens": 123,
"tokens": 45
},
"statusCode": 402
}

处理/服务器错误 500​

{
"message": "Error in text translation for api",
"error": "Detailed server error message",
"data": null,
"statusCode": 500
}