# Webhook エンドポイント

> iMario がイベントを送るエンドポイント、テストイベント、署名シークレット、配信ログ。オーナーと管理者のみ。

Source: https://imario.ai/ja/docs/api/reference/webhooks

ベース URL は `https://api.imario.ai/v1` です。各呼び出しには、記載のスコープを持つキーが必要です。キーのロールも、記載のロール以上である必要があります（[認証](https://imario.ai/ja/docs/api/authentication)を参照）。エラーについては [API のエラー](https://imario.ai/ja/docs/api/errors)を参照してください。

## Webhook エンドポイントの一覧を取得
`GET /v1/webhook-endpoints`

**スコープ** `webhooks:read` · **ロール** オーナーまたは管理者

キーのモード（本番またはテスト）のエンドポイントを、新しい順に返します。`webhook_endpoint` のフィールドは `id`、`livemode`（テストキーで作成したエンドポイントはテスト用で、サンドボックスのイベントだけを受け取ります）、`created_at`、`url`、`description`、`events`（イベントの種類。すべてなら `["*"]`）、`enabled`、`disabled_reason`（配信が3日間失敗し続けて iMario がオフにした場合は `delivery_failures`、ほかに `disabled_by_user` または null）です。

**パラメーター**

| 名前 | 場所 | 型 | 必須 | 説明 |
|---|---|---|---|---|
| `limit` | クエリ | `integer` | いいえ | 返すオブジェクトの数（1〜100）。既定値は `20` です。 |
| `cursor` | クエリ | `string` | いいえ | 前のページの `next_cursor`。200文字まで。 |

**例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

**レスポンス**

リストを返します：`{"object": "list", "data": [...], "has_more": false, "next_cursor": null}`。[ページ分割](https://imario.ai/ja/docs/api/pagination)を参照してください。

## Webhook エンドポイントを作成
`POST /v1/webhook-endpoints`

**スコープ** `webhooks:write` · **ロール** オーナーまたは管理者

レスポンスには `secret`（`whsec_...`）が含まれます。表示されるのはこの1回だけです。`webhook-signature`（Standard Webhooks）の検証に使うので、保管してください。URL は https で、公開アドレスだけに解決される必要があります。配信のたびに、あらためて確認します。`webhook_endpoint` のフィールドは `id`、`livemode`（テストキーで作成したエンドポイントはテスト用で、サンドボックスのイベントだけを受け取ります）、`created_at`、`url`、`description`、`events`（イベントの種類。すべてなら `["*"]`）、`enabled`、`disabled_reason`（配信が3日間失敗し続けて iMario がオフにした場合は `delivery_failures`、ほかに `disabled_by_user` または null）です。

**リクエスト本文**

| フィールド | 型 | 必須 | 説明 |
|---|---|---|---|
| `url` | `string` | はい | 公開されている https の URL。リダイレクトはたどりません。2,048文字まで。 |
| `events` | `string[]` | はい | 受け取るイベントの種類。すべての種類を受け取るなら ["*"]。20件まで。 |
| `description` | `string` | いいえ | 200文字まで。 |

**例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/imario/webhooks",
    "events": ["study.completed", "study.failed", "report.completed"],
    "description": "Production receiver"
  }'
```

## Webhook エンドポイントを取得
`GET /v1/webhook-endpoints/{endpoint_id}`

**スコープ** `webhooks:read` · **ロール** オーナーまたは管理者

`webhook_endpoint` のフィールドは `id`、`livemode`（テストキーで作成したエンドポイントはテスト用で、サンドボックスのイベントだけを受け取ります）、`created_at`、`url`、`description`、`events`（イベントの種類。すべてなら `["*"]`）、`enabled`、`disabled_reason`（配信が3日間失敗し続けて iMario がオフにした場合は `delivery_failures`、ほかに `disabled_by_user` または null）です。

**例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## Webhook エンドポイントを更新
`PATCH /v1/webhook-endpoints/{endpoint_id}`

**スコープ** `webhooks:write` · **ロール** オーナーまたは管理者

変更するフィールドだけを送ってください。`webhook_endpoint` のフィールドは `id`、`livemode`（テストキーで作成したエンドポイントはテスト用で、サンドボックスのイベントだけを受け取ります）、`created_at`、`url`、`description`、`events`（イベントの種類。すべてなら `["*"]`）、`enabled`、`disabled_reason`（配信が3日間失敗し続けて iMario がオフにした場合は `delivery_failures`、ほかに `disabled_by_user` または null）です。

**リクエスト本文**

| フィールド | 型 | 必須 | 説明 |
|---|---|---|---|
| `url` | `string` | いいえ | 2,048文字まで。 |
| `events` | `string[]` | いいえ | 20件まで。 |
| `description` | `string` | いいえ | 200文字まで。 |
| `enabled` | `boolean` | いいえ | false で配信を止めます（待機中の配信は破棄します）。true でエンドポイントを再びオンにします。 |

**例**

```bash
curl -X PATCH https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "enabled": false
  }'
```

## Webhook エンドポイントを削除
`DELETE /v1/webhook-endpoints/{endpoint_id}`

**スコープ** `webhooks:write` · **ロール** オーナーまたは管理者

このエンドポイントへの待機中の配信は破棄します。`{object, id, deleted: true}` を返します。

**例**

```bash
curl -X DELETE https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## テストイベントを送信
`POST /v1/webhook-endpoints/{endpoint_id}/test`

**スコープ** `webhooks:write` · **ロール** オーナーまたは管理者

このエンドポイントだけに `ping` イベントを送るようキューに入れ（`data.object` はこのエンドポイント）、`webhook_delivery` を返します。フィールドは `id`、`endpoint_id`、`event_id`、`event_type`、`status`（pending、delivered、failed）、`attempts`、`last_status_code`、`last_error`、`created_at`、`delivered_at`、`next_attempt_at` です。

**例**

```bash
curl -X POST https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID/test \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## 署名シークレットを更新
`POST /v1/webhook-endpoints/{endpoint_id}/rotate-secret`

**スコープ** `webhooks:write` · **ロール** オーナーまたは管理者

新しい `secret` を返します。表示されるのはこの1回だけです。24時間は、配信に2つの署名（新しいシークレットと古いシークレットによるもの）が付くため、受信側はイベントを取りこぼさずに切り替えられます。`webhook_endpoint` のフィールドは `id`、`livemode`（テストキーで作成したエンドポイントはテスト用で、サンドボックスのイベントだけを受け取ります）、`created_at`、`url`、`description`、`events`（イベントの種類。すべてなら `["*"]`）、`enabled`、`disabled_reason`（配信が3日間失敗し続けて iMario がオフにした場合は `delivery_failures`、ほかに `disabled_by_user` または null）です。

**例**

```bash
curl -X POST https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID/rotate-secret \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

## エンドポイントの配信ログを取得
`GET /v1/webhook-endpoints/{endpoint_id}/deliveries`

**スコープ** `webhooks:read` · **ロール** オーナーまたは管理者

新しい順に返します。保存期間は30日です。各 `webhook_delivery` のフィールドは `id`、`endpoint_id`、`event_id`（`webhook-id` ヘッダーの値）、`event_type`、`status`（pending、delivered、failed）、`attempts`、`last_status_code`、`last_error`、`created_at`、`delivered_at`、`next_attempt_at` です。失敗した配信は、1分後、5分後、30分後、2時間後、6時間後、12時間後に再試行します。

**パラメーター**

| 名前 | 場所 | 型 | 必須 | 説明 |
|---|---|---|---|---|
| `limit` | クエリ | `integer` | いいえ | 返すオブジェクトの数（1〜100）。既定値は `20` です。 |
| `cursor` | クエリ | `string` | いいえ | 前のページの `next_cursor`。200文字まで。 |

**例**

```bash
curl https://api.imario.ai/v1/webhook-endpoints/ENDPOINT_ID/deliveries \
  -H "Authorization: Bearer $IMARIO_API_KEY"
```

**レスポンス**

リストを返します：`{"object": "list", "data": [...], "has_more": false, "next_cursor": null}`。[ページ分割](https://imario.ai/ja/docs/api/pagination)を参照してください。
