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

# Kling 画像生成

> ネイティブの contents、settings、options を使用して Kling Image 4.0 の生成タスクを送信します。

MixRoute API キーを Bearer トークンとして送信します。モデル一覧の ID は `kling-image-4.0` ですが、リクエストには以下のパスを使用します。`/kling/image-generation/kling-image-4.0` は使用できません。`/v1` プレフィックスや、リクエストボディの `model` フィールド、`metadata` ラッパーは不要です。

`POST https://api.mixroute.ai/kling/image-generation/kling-4.0`

## リクエストボディ

| フィールド | 型 | 必須 | 説明 |
| - | - | - | - |
| `contents` | array | はい | 入力の配列。空でない `prompt` 項目を含めます。 |
| `settings` | object | いいえ | 生成する枚数、解像度、アスペクト比を指定します。 |
| `options` | object | いいえ | コールバック、独自のタスク ID、透かしの設定です。 |

### 入力項目

| `type` | その他のフィールド |
| - | - |
| `prompt` | 必須。プロンプトは `text` に入力します。トップレベルに `prompt` は指定しません。 |
| `refer_image` | `url` にアクセス可能な画像 URL または Base64 画像データを指定し、`id` にタスク内で一意の参照名を指定します。 |
| `element` | `element_id` に接続先のアカウントで利用できる複数画像の Element の ID を、`id` に一意の参照名を指定します。 |

参照画像と Element は合計 10 個までです。プロンプトで `@id` を使うと入力を明示的に参照できます。画像は JPG、JPEG、PNG のいずれかで、1 枚あたり 30 MB 以下、縦横ともに 300 px 以上、幅と高さの比は 2:5～5:2 です。

プロンプトは英語のみの場合は 50,000 文字、英語以外のみの場合は 21,000 文字までです。混在する場合は、英語の文字数を 50,000 で割った値と、英語以外の文字数を 21,000 で割った値の合計が 1 以下になるようにしてください。

### 出力設定

| フィールド | 型 | デフォルト | 説明 |
| - | - | - | - |
| `result_type` | string | `single` | `single` は独立した画像、`series` は関連する画像を生成します。 |
| `n` | integer | `1` | 1～9 枚。`result_type` が `single` の場合のみ適用されます。 |
| `series_amount` | integer / string | `4` | 2～10 枚、または `auto`。`series` モードでのみ適用されます。`auto` ではサービスが枚数を決めます。 |
| `resolution` | string | `2k` | `2k`、`4k`、`8k` から選択できます。 |
| `aspect_ratio` | string | `auto` | `auto`、`16:9`、`9:16`、`1:1`、`4:3`、`3:4`、`3:2`、`2:3`、`21:9` から選択できます。 |

### オプション

| フィールド | 型 | 必須 | 説明 |
| - | - | - | - |
| `callback_url` | string | いいえ | コールバック URL です。ポーリングには送信時の `data.id` に含まれるゲートウェイのタスク ID を使います。 |
| `external_task_id` | string | いいえ | 任意のカスタム ID です。照会に使用するゲートウェイのタスク ID の代わりにはなりません。 |
| `watermark_info.enabled` | boolean | いいえ | `true` にすると透かし付きの画像もリクエストします。デフォルトは `false` です。 |

## 画像を 1 枚生成する

```bash theme={null}
curl --request POST 'https://api.mixroute.ai/kling/image-generation/kling-4.0' \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "contents": [
      {
        "type": "prompt",
        "text": "A single blue ceramic mug centered on a white table, studio product photograph."
      }
    ],
    "settings": {
      "result_type": "single",
      "n": 1,
      "resolution": "2k",
      "aspect_ratio": "1:1"
    }
  }'
```

タスクの受付に成功すると、`code: 0` とゲートウェイのタスク ID が `data.id` に返ります。この時点では画像は返りません。

## タスクを照会して画像を取得する

`data.id` を `KLING_TASK_ID` に設定し、1 回につき 1 件ずつ照会します。状態とエラーについては [Kling タスクの照会](https://mixroute.ai/docs/ja/api-reference/endpoint/kling-tasks.md) を参照してください。

```bash theme={null}
curl --get 'https://api.mixroute.ai/kling/tasks' \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --data-urlencode "task_ids=$KLING_TASK_ID"
```

```json theme={null}
{
  "code": 0,
  "message": "SUCCEED",
  "data": [
    {
      "id": "task_example",
      "status": "succeeded",
      "outputs": [
        {
          "type": "image",
          "url": "https://example.com/generated.png"
        }
      ]
    }
  ]
}
```

該当タスクの状態が `succeeded` になったら、`outputs` で `type: "image"` の項目から `url` を取得します。結果の URL には MixRoute API キーを送信しないでください。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.