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

# 非同期タスクの流れ：送信、状態の確認、結果の取得

> APIXO の非同期タスク パターンを理解します。生成タスクを送信し、そのステータスをポーリングするか Webhook を使用し、結果 URL を取得します。

非同期生成リクエストは、出力の準備ができる前にタスク ID を返します。**モデルIDとタスクIDを一緒に保存します**。再起動後に進行状況を確認する場合は、同じペアを使用します。プロンプトを再度送信すると、別のジョブが開始されます。

以下の [送信](https://apixo.ai/docs/ja/api-reference/generate-task.md)、[ステータス チェック](https://apixo.ai/docs/ja/api-reference/status-task.md)、および結果の取得を通じて単一のタスクに従います。タスクのライフサイクルがすでにわかっている場合は、[ポーリングと Webhook を比較](https://apixo.ai/docs/ja/guides/polling-vs-webhooks.md)して配信方法を選択します。

## パタン

```
┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│   Submit    │────▶│  Poll or    │────▶│   Retrieve  │
│   Task      │     │  Callback   │     │   Results   │
└─────────────┘     └─────────────┘     └─────────────┘
      │                   │                   │
      ▼                   ▼                   ▼
   taskId            state check          resultUrls
```

## ステップ 1: タスクを送信する

`POST` リクエストを送信して生成タスクを作成します。

```bash theme={null}
POST https://api.apixo.ai/api/v1/generateTask/{model}
```

**リクエスト:**

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "text-to-image",
    "prompt": "A beautiful sunset over mountains"
  }
}
```

**応答:**

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_abc123xyz"
  }
}
```

API はすぐに `taskId` を返します。実際の生成はバックグラウンドで行われます。

## ステップ 2: 結果の取得

結果を受け取るには 2 つのオプションがあります。

### オプション A: 輪詢 (入門に推奨)

タスクのステータスを定期的に確認します。

```bash theme={null}
GET https://api.apixo.ai/api/v1/statusTask/{model}?taskId={taskId}
```

**応答(進行中):**

```json theme={null}
{
  "data": {
    "taskId": "task_abc123xyz",
    "state": "processing"
  }
}
```

**応答(完了):**

```json theme={null}
{
  "data": {
    "taskId": "task_abc123xyz",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://cdn.apixo.ai/...\"]}",
    "costTime": 12500
  }
}
```

### オプション B: Webhook（本番環境で推奨）

送信時に `callback_url` を指定してください。

```json theme={null}
{
  "request_type": "callback",
  "callback_url": "https://your-server.com/webhook/apixo",
  "input": { ... }
}
```

APIXO はタスクが完了したときに、その結果をあなたのURLに `POST` します。

実装の詳細については、[Webhook](https://apixo.ai/docs/ja/api-reference/webhooks.md)を参照してください。

## タスクの状態

| 状態 | 説明 |
| - | - |
| `pending` | タスクがキューに入れられ、開始を待っています。 |
| `processing` | タスクは処理中です。 |
| `success` | タスクは正常に完了しました。 |
| `failed` | タスクが失敗しました（`failMsg` を確認してください） |

## ポーリングのベストプラクティス

1. ほとんどのタスクでは **3 ～ 5 秒間隔** で開始します
2. **長時間実行タスク（ビデオなど）には指数バックオフを使用します**
3. **最大タイムアウトを設定します**（例：5 分）
4. **再試行ロジックを使用して失敗を適切に処理します**

```javascript theme={null}
const pollWithBackoff = async (taskId, maxWait = 300000) => {
  let interval = 3000; // Start at 3s
  const startTime = Date.now();
  
  while (Date.now() - startTime < maxWait) {
    const result = await checkStatus(taskId);
    
    if (result.state === 'success') return result;
    if (result.state === 'failed') throw new Error(result.failMsg);
    
    await sleep(interval);
    interval = Math.min(interval * 1.5, 30000); // Max 30s
  }
  
  throw new Error('Timeout waiting for result');
};
```

## 各アプローチの使用タイミング

| アプローチ | 最適な用途 |
| - | - |
| **ポーリング** | シンプルな統合、クライアント側アプリ、テスト |
| **Webhook** | 実稼働サーバー、リアルタイムアプリ、高負荷 |

## 次のステップ

> [card] [生成 APIの概要](https://apixo.ai/docs/ja/models.md)
>
> 画像、ビデオ、オーディオモデルの非同期APIパターンを理解する

> [card] [タスクの生成](https://apixo.ai/docs/ja/api-reference/generate-task.md)
>
> 生成タスクを送信し、タスクIDを取得する

> [card] [ポーリングとWebhookの比較](https://apixo.ai/docs/ja/guides/polling-vs-webhooks.md)
>
> 両方のアプローチを詳しく掘り下げる


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