Chuyển tới nội dung chính

API dịch văn bản

Dịch văn bản bằng GPT Translator API.

Endpoint​

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

Xác thực​

Thêm khóa API vào header của yêu cầu:

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

Nội dung yêu cầu​

Trường bắt buộc:

  • text chuỗi: Văn bản nguồn cần dịch
  • sourceLang chuỗi: Mã ngôn ngữ nguồn
  • targetLang chuỗi: Mã ngôn ngữ đích
  • model chuỗi: Tên mô hình dịch

Trường tùy chọn:

  • domain chuỗi: Lĩnh vực chuyên môn
  • writingStyle chuỗi: Phong cách viết mong muốn
  • tone chuỗi: Giọng điệu mong muốn
  • customPrompt chuỗi: Hướng dẫn dịch tùy chỉnh

Mã ngôn ngữ được mô tả riêng trong Ngôn ngữ được hỗ trợ. Các mô hình được hỗ trợ được mô tả trong Mô hình được hỗ trợ.

Quy tắc xác thực dữ liệu​

  • text: Bắt buộc. Không được để trống.
  • sourceLang, targetLang: Phải khớp với ^[a-zA-Z]+(-[a-zA-Z]+)?$, tối thiểu 2 ký tự, tối đa 11 ký tự. Phải là mã trong Ngôn ngữ được hỗ trợ.
  • model: Phải là một trong các giá trị trong Mô hình được hỗ trợ.

Ví dụ​

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

Các loại phản hồi​

Thành công 200​

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

Lỗi xác thực yêu cầu 400​

Lỗi từ middleware xác thực được trả về theo định dạng sau:

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

Các lỗi xác thực khác có thể gặp:

  • 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.

Thiếu khóa API/Header không hợp lệ 401​

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

Lỗi khóa API hoặc thiết lập người dùng 400​

Khóa không hợp lệ trong cơ sở dữ liệu:

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

Người dùng không hoạt động:

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

Thiếu tổ chức:

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

Giới hạn gói 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
}

Đã đạt giới hạn 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
}

Lỗi xử lý/máy chủ 500​

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