Hoppa till huvudinnehåll

API för dokumentöversättning

Översätt dokumentfiler som stöds asynkront med din API-nyckel.

Arbetsflöde​

  1. Ladda upp ett dokument med POST /v1/translation/document-translation/api.
  2. Spara det returnerade translationId.
  3. Hämta status med GET /v1/translation/document-translation-status/{translationId}.
  4. När statusen är completed laddar du ner den översatta filen via data.documentUrl.

1) Ladda upp dokument​

Endpoint​

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

Autentisering​

x-api-key: YOUR_API_KEY

Innehållstyp​

Använd multipart/form-data och skicka filen i fältet file.

Formulärfält​

Obligatoriska:

  • file fil
  • sourceLang sträng
  • targetLang sträng
  • model sträng

Valfria:

  • domain sträng
  • writingStyle sträng
  • tone sträng
  • customPrompt sträng

Språkkoder finns i Språk som stöds. Modeller som stöds finns i Modeller som stöds. Filtyper som stöds finns i Dokumenttyper som stöds.

Valideringsregler​

  • sourceLang, targetLang: måste matcha ^[a-zA-Z]+(-[a-zA-Z]+)?$, minst 2 och högst 10 tecken.
  • model: måste vara ett av värdena i Modeller som stöds.
  • file: måste ha ett av filtilläggen i Dokumenttyper som stöds.

cURL-exempel​

curl --location 'https://api.gpttranslator.co/v1/translation/document-translation/api' \
--header 'x-api-key: YOUR_API_KEY' \
--form 'file=@"/absolute/path/sample.docx"' \
--form 'sourceLang="en"' \
--form 'targetLang="fr"' \
--form 'model="gpt-4.1-mini-2025-04-14"' \
--form 'tone="professional"'

JavaScript-exempel​

const apiKey = "YOUR_API_KEY";
const fileInput = document.querySelector("#fileInput");

const formData = new FormData();
formData.append("file", fileInput.files[0]);
formData.append("sourceLang", "en");
formData.append("targetLang", "fr");
formData.append("model", "gpt-4.1-mini-2025-04-14");
formData.append("tone", "professional");

const uploadRes = await fetch(
"https://api.gpttranslator.co/v1/translation/document-translation/api",
{
method: "POST",
headers: {
"x-api-key": apiKey,
},
body: formData,
},
);

const uploadData = await uploadRes.json();
const translationId = uploadData?.data?.translationId;

if (!translationId) {
console.error("Upload failed:", uploadData);
}

Python-exempel​

import requests

api_key = "YOUR_API_KEY"
url = "https://api.gpttranslator.co/v1/translation/document-translation/api"

headers = {
"x-api-key": api_key
}

data = {
"sourceLang": "en",
"targetLang": "fr",
"model": "gpt-4.1-mini-2025-04-14",
"tone": "professional"
}

with open("/absolute/path/sample.docx", "rb") as f:
files = {
"file": ("sample.docx", f)
}
response = requests.post(url, headers=headers, data=data, files=files)

print(response.status_code)
print(response.json())

Lyckat svar 201​

{
"message": "Document translation added successfully",
"data": {
"translationId": "12345"
},
"error": null,
"statusCode": 201
}

Svarstyper​

Valideringsfel i begäran (400) från middleware:

{
"error": "Invalid model type. Please enter a valid model type e.g. ..."
}

Ingen fil uppladdad (400):

{
"error": "No file uploaded. Please upload a file for translation.",
"statusCode": 400,
"data": null,
"message": "No file uploaded. Please upload a file for translation."
}

Ogiltig filtyp (400):

{
"error": "Invalid file type. Please upload one of: docx, xlsx, pdf, pptx, srt, vtt, xml, json, yaml, yml, csv, txt, md, html, eml.",
"statusCode": 400,
"data": null,
"message": "Invalid file type. Please upload one of: docx, xlsx, pdf, pptx, srt, vtt, xml, json, yaml, yml, csv, txt, md, html, eml."
}

Ogiltig API-nyckelheader (401):

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

Ogiltig konfiguration av API-nyckel/användare/organisation (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
}

Serverfel (500):

{
"error": "Internal server error",
"message": "An error occurred while processing the document translation request. Please try again later.",
"data": null,
"statusCode": 500
}

2) Kontrollera översättningsstatus​

Endpoint​

GET https://api.gpttranslator.co/v1/translation/document-translation-status/{translationId}

Autentisering​

x-api-key: YOUR_API_KEY

cURL-exempel​

curl --location 'https://api.gpttranslator.co/v1/translation/document-translation-status/12345' \
--header 'x-api-key: YOUR_API_KEY'

JavaScript-exempel​

const apiKey = "YOUR_API_KEY";
const translationId = "12345";

const statusRes = await fetch(
`https://api.gpttranslator.co/v1/translation/document-translation-status/${translationId}`,
{
headers: {
"x-api-key": apiKey,
},
},
);

const statusData = await statusRes.json();
console.log(statusData);

Python-exempel​

import requests

api_key = "YOUR_API_KEY"
translation_id = "12345"
url = f"https://api.gpttranslator.co/v1/translation/document-translation-status/{translation_id}"

response = requests.get(url, headers={"x-api-key": api_key})

print(response.status_code)
print(response.json())

Svarsformat​

{
"message": "Translation status fetched successfully",
"translationStatus": "completed",
"data": {
"documentUrl": "https://api.gpttranslator.co/v1/translation/download-document/65f0d3f6f0a1c91234567890"
},
"error": null,
"statusCode": 200
}

Statusvärden​

  • waiting: jobbet står i kö.
  • active: översättningen pågår.
  • completed: översättningen är klar och data.documentUrl är tillgänglig.
  • failed: översättningen misslyckades. Kontrollera error.

Svarstyper​

Kö-id saknas (400):

{
"error": "Queue ID is required",
"message": "Queue ID is required to fetch translation status",
"data": null,
"statusCode": 400
}

Ogiltig API-nyckelheader (401):

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

Ogiltig konfiguration av API-nyckel/användare/organisation (400):

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

Jobbet hittades inte (404):

{
"error": "Translation job not found",
"message": "The requested translation job does not exist.",
"data": null,
"statusCode": 404
}

Obehörig (401) om användarkontext saknas:

{
"error": "Unauthorized",
"message": "You are not authorized to access this resource.",
"data": null,
"statusCode": 401
}

Obehörig åtkomst till kön (401):

{
"error": "Unauthorized access to translation status",
"message": "You do not have permission to access the status of this translation job.",
"data": null,
"statusCode": 401
}

Fel i affärsregel från köresultatet (exempel 402):

{
"message": "Translation status fetched successfully",
"translationStatus": "failed",
"data": null,
"error": "only subscribed users are allowed to use this model",
"statusCode": 402
}

Serverfel (500):

{
"error": "Internal server error",
"message": "An error occurred while fetching the translation status. Please try again later.",
"data": null,
"statusCode": 500
}

3) Ladda ner översatt dokument​

När statusen är completed anropar du documentUrl från statussvaret.

Endpoint​

GET https://api.gpttranslator.co/v1/translation/download-document/{translationId}

Valfri frågeparameter:

  • fileType=translated (standard)
  • fileType=source

cURL-exempel​

curl --location 'https://api.gpttranslator.co/v1/translation/download-document/65f0d3f6f0a1c91234567890' \
--header 'x-api-key: YOUR_API_KEY' \
--output translated-file

JavaScript-exempel​

const apiKey = "YOUR_API_KEY";
const translationId = "65f0d3f6f0a1c91234567890";

const downloadRes = await fetch(
`https://api.gpttranslator.co/v1/translation/download-document/${translationId}`,
{
headers: {
"x-api-key": apiKey,
},
},
);

if (!downloadRes.ok) {
const err = await downloadRes.json();
console.error("Download failed:", err);
} else {
const blob = await downloadRes.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "translated-document";
a.click();
URL.revokeObjectURL(url);
}

Python-exempel​

import requests

api_key = "YOUR_API_KEY"
translation_id = "65f0d3f6f0a1c91234567890"
url = f"https://api.gpttranslator.co/v1/translation/download-document/{translation_id}"

response = requests.get(url, headers={"x-api-key": api_key}, stream=True)

if response.status_code == 200:
with open("translated-document", "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
else:
print(response.status_code)
try:
print(response.json())
except Exception:
print("Download failed")

Svarstyper​

Lyckat (200):

  • Returnerar binärt filinnehåll.
  • Headers innehåller:
    • Content-Type (baserat på den översatta filens typ)
    • Content-Disposition: attachment; filename="..."

Ogiltig API-nyckelheader (401):

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

Ogiltig konfiguration av API-nyckel/användare/organisation (400):

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

Nedladdningsfel:

{ "error": "Invalid translation ID" }
{ "error": "Translation not found or you do not have permission to access it" }
{ "error": "Translation is currently active. Please wait for completion." }
{ "error": "Translated file not available" }

Bästa praxis​

Hämta status var 2:a till 5:e sekund tills translationStatus blir completed eller failed.