> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenlab.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# 建立翻譯

> 將文字翻譯為目標語言

## 概覽

使用 `/v1/translations` 進行**文字對文字翻譯**。

<Note>
  這個 endpoint 與 [音訊翻譯](/zh-Hant/api-reference/audio/create-translation) 不同，後者會接收音訊檔案，並且一律輸出英文文字。
</Note>

針對 agent workflow，建議先找出目前推薦的翻譯 model：

```bash theme={null}
curl "https://api.tokenlab.sh/v1/models?recommended_for=translation" \
  -H "Authorization: Bearer sk-your-api-key"
```

接著把選定的 model 明確傳給 `/v1/translations`。

<Note>
  `recommended_for=translation` 只適用於**文字翻譯**（`POST /v1/translations`），不適用於 [音訊翻譯](/zh-Hant/api-reference/audio/create-translation)。
</Note>

## 請求主體

<ParamField body="model" type="string" required>
  由 `/v1/models?recommended_for=translation` 回傳的翻譯 model ID。
</ParamField>

<ParamField body="text" type="string" required>
  要翻譯的來源文字。
</ParamField>

<ParamField body="target_language" type="string" required>
  目標語言代碼或語言名稱，取決於 model 支援情況。
</ParamField>

<ParamField body="source_language" type="string">
  選用的來源語言提示。
</ParamField>

<ParamField body="mime_type" type="string" default="text/plain">
  來源文字格式。支援值：`text/plain`、`text/html`。
</ParamField>

<ParamField body="user" type="string">
  選用的終端使用者識別碼，用於濫用監控與請求追蹤。
</ParamField>

## 回應

<ResponseField name="text" type="string">
  翻譯後的文字。
</ResponseField>

<ResponseField name="model" type="string">
  產生翻譯結果的 model。
</ResponseField>

<ResponseField name="source_language" type="string | null">
  model 偵測到或接受的來源語言。
</ResponseField>

<ResponseField name="target_language" type="string">
  本次請求使用的目標語言。
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.tokenlab.sh/v1/translations" \
    -H "Authorization: Bearer sk-your-api-key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "doubao-seed-translation",
      "text": "你好，歡迎使用 TokenLab。",
      "target_language": "en"
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI
  import requests

  client = OpenAI(
      api_key="sk-your-api-key",
      base_url="https://api.tokenlab.sh/v1"
  )

  response = requests.post(
      "https://api.tokenlab.sh/v1/translations",
      headers={
          "Authorization": "Bearer sk-your-api-key",
          "Content-Type": "application/json",
      },
      json={
          "model": "doubao-seed-translation",
          "text": "你好，歡迎使用 TokenLab。",
          "target_language": "en",
      },
      timeout=30,
  )

  print(response.json()["text"])
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "text": "Hello, welcome to TokenLab.",
    "model": "doubao-seed-translation",
    "source_language": "zh",
    "target_language": "en"
  }
  ```
</ResponseExample>
