# 從 ByteDance 的 API 遷移

> 將為 ByteDance 自家 API 撰寫的程式碼移轉到 Kerox：保留請求內容並加上一層包裝，更換主機與金鑰，再以 Kerox 的方式讀取結果。

加上 `"format": "native"` 後，ByteDance 模型會接受 ByteDance 自家的請求內容，因此原本傳送的參數都不必更動。需要調整的是：主機、金鑰、`model` 和回呼的位置，以及取得結果的方式。其他模型沒有這個選項，它們接受[Kerox 的參數名稱](https://kerox.ai/zh-hant/docs/models.md)，而 ByteDance 模型也同樣接受這些名稱。

<a id="changes"></a>

## 需要修改的地方
| 在原本的 ByteDance 程式碼中 | 在 Kerox 上 |
| --- | --- |
| ByteDance 的端點 | `POST https://api.kerox.ai/v1/tasks`，圖片和影片都使用此端點 |
| ByteDance 的 API 金鑰 | Kerox 金鑰：`Authorization: Bearer $KEROX_API_KEY`（[快速入門](https://kerox.ai/zh-hant/docs/quickstart.md#key)） |
| `model`：ByteDance 的模型名稱 | `model`：Kerox 模型 ID，例如 `bytedance/seedream-5.5`（[模型](https://kerox.ai/zh-hant/docs/models.md)） |
| 請求內容的其餘部分 | 保持不變，放在 `input` 內，並在旁邊加上 `"format": "native"` |
| `callback_url` | `webhook_url`，放在最上層，與 `model` 並列（[Webhook](https://kerox.ai/zh-hant/docs/webhooks.md)） |
| 回應中的結果 | 回應會立即提供任務的 `id`；輪詢 `GET /v1/tasks/{id}` 或等待 Webhook，然後讀取 `outputs` 中的檔案（[任務](https://kerox.ai/zh-hant/docs/tasks.md#polling)） |

部分 ByteDance 參數的處理方式不同：

- **接受但忽略**：`service_tier`、`priority`、`safety_identifier`。包含這些參數的請求內容可照常運作；由於這些參數不會傳給模型，因此會使用 ByteDance 的預設值。
- **已移動**：`callback_url` 改為最上層的 `webhook_url`。
- **請移除**：`execution_expires_after`、`response_format`、`stream`。包含這些欄位的請求會被拒絕，並回傳 HTTP `400` `invalid_request`，`param` 會指出欄位名稱。

<a id="examples"></a>

## 修改前後對照
圖片請求。移除 `response_format`；結果一律是 `outputs` 中的檔案 URL。


```json
{
  "model": "<ByteDance model name>",
  "prompt": "a quiet coastal lighthouse at dusk",
  "size": "2K",
  "response_format": "url",
  "watermark": false
}
```
```json
{
  "model": "bytedance/seedream-5.5",
  "format": "native",
  "input": {
    "prompt": "a quiet coastal lighthouse at dusk",
    "size": "2K",
    "watermark": false
  }
}
```


影片請求。`callback_url` 移到外層成為 `webhook_url`；`service_tier` 可以保留，但會被忽略。


```json
{
  "model": "<ByteDance model name>",
  "content": [{ "type": "text", "text": "a red fox running through fresh snow" }],
  "ratio": "16:9",
  "resolution": "720p",
  "duration": 5,
  "service_tier": "default",
  "callback_url": "https://your-app.example.com/webhooks/kerox"
}
```
```json
{
  "model": "bytedance/seedance-2.5",
  "format": "native",
  "webhook_url": "https://your-app.example.com/webhooks/kerox",
  "input": {
    "content": [{ "type": "text", "text": "a red fox running through fresh snow" }],
    "ratio": "16:9",
    "resolution": "720p",
    "duration": 5,
    "service_tier": "default"
  }
}
```


<a id="native"></a>

## 送出請求
送出影片請求的範例。各模型在此格式中接受的參數，請參閱 `GET /v1/models/{id}` 中的 `input_schema`，以及各自[模型頁面](https://kerox.ai/zh-hant/docs/models.md)中的 ByteDance 格式章節。


```bash
curl -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "bytedance/seedance-2.5",
    "format": "native",
    "input": {
      "content": [
        { "type": "text", "text": "a red fox running through fresh snow" }
      ],
      "ratio": "16:9",
      "resolution": "720p",
      "duration": 5
    }
  }'
```
```python
import os
import requests

resp = requests.post(
    "https://api.kerox.ai/v1/tasks",
    headers={
        "Authorization": f"Bearer {os.environ['KEROX_API_KEY']}",
        "Content-Type": "application/json",
    },
    json={
        "model": "bytedance/seedance-2.5",
        "format": "native",
        "input": {
            "content": [
                {"type": "text", "text": "a red fox running through fresh snow"}
            ],
            "ratio": "16:9",
            "resolution": "720p",
            "duration": 5,
        },
    },
    timeout=30,
)
print(resp.json())
```
```javascript
const res = await fetch('https://api.kerox.ai/v1/tasks', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.KEROX_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'bytedance/seedance-2.5',
    format: 'native',
    input: {
      content: [
        { type: 'text', text: 'a red fox running through fresh snow' },
      ],
      ratio: '16:9',
      resolution: '720p',
      duration: 5,
    },
  }),
});
console.log(await res.json());
```


回應及後續流程與一般任務相同：請參閱[任務與輪詢](https://kerox.ai/zh-hant/docs/tasks.md#polling)。
