> ## 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/api-reference/audio/create-translation) 不同，后者接收音频文件，并且始终输出英文文本。
</Note>

对于 agent 工作流，建议先查找当前推荐的翻译 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/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>
