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

# Checkout セッションの検証

> 完了した Checkout セッションを検証し、クレジットが付与されたことを確認します。

## 概要

決済完了後に本エンドポイントで支払い処理とクレジット付与を確認します。Webhook が処理されなかった場合のフォールバックや、クライアント側での確認に役立ちます。

<Note>
  クレジットは通常 Stripe の Webhook で自動付与されます。本エンドポイントはエッジケースやクライアント側確認用の手動検証手段です。
</Note>

<Note>
  **サードパーティ連携の場合:** API キー認証の `GET /credits/verify-session/{sessionId}/api` を利用してください。
</Note>

## 認証

<ParamField header="Authorization" type="string" required>
  JWT アクセストークン（セッション認証）。形式: `Bearer YOUR_ACCESS_TOKEN`
</ParamField>

## パスパラメーター

<ParamField path="sessionId" type="string" required>
  `/credits/checkout` から返された Stripe Checkout セッション ID。
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X GET https://api.llmgenerator.com/api/v1/credits/verify-session/cs_test_a1b2c3d4e5f6 \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
  ```

  ```javascript JavaScript theme={null}
  const sessionId = 'cs_test_a1b2c3d4e5f6';
  const response = await fetch(
    `https://api.llmgenerator.com/api/v1/credits/verify-session/${sessionId}`,
    {
      headers: {
        'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
      }
    }
  );

  const data = await response.json();
  if (data.success) {
    console.log(`Added ${data.credits} credits`);
  }
  ```

  ```python Python theme={null}
  import requests

  session_id = 'cs_test_a1b2c3d4e5f6'
  response = requests.get(
      f'https://api.llmgenerator.com/api/v1/credits/verify-session/{session_id}',
      headers={'Authorization': 'Bearer YOUR_ACCESS_TOKEN'}
  )
  data = response.json()
  print(f"Verified: {data['credits']} credits")
  ```
</RequestExample>

<ResponseExample>
  ```json 成功 theme={null}
  {
    "success": true,
    "credits": 500,
    "sessionId": "cs_test_a1b2c3d4e5f6"
  }
  ```

  ```json 決済未完了 theme={null}
  {
    "success": false,
    "message": "Payment not completed"
  }
  ```
</ResponseExample>

## レスポンスフィールド

<ResponseField name="success" type="boolean">
  セッション検証が成功したかどうか。
</ResponseField>

<ResponseField name="credits" type="integer">
  購入によるクレジット数。
</ResponseField>

<ResponseField name="sessionId" type="string">
  検証したセッション ID。
</ResponseField>

## エラーレスポンス

<ResponseField name="400">
  不正なリクエスト — 決済未完了、またはセッションのメタデータが無効です。
</ResponseField>

<ResponseField name="401">
  未認証 — トークンが無効または未指定です。
</ResponseField>

<ResponseField name="403">
  禁止 — セッションが認証ユーザーに属しません。
</ResponseField>

## べき等性

本エンドポイントはべき等です。クレジットが既に Webhook により付与済みの場合、二重付与は行わず、成功とクレジット量を返します。

## 典型的な流れ

1. ユーザーが Stripe で決済を完了する
2. Stripe が `successUrl` にリダイレクトする
3. 本エンドポイントで付与を検証・確認する
4. ユーザーに完了を表示する
5. UI の残高表示を更新する

<Tip>
  利用可能ではありますが、推奨フローは Stripe Webhook を受信し、`/credits/balance` に基づいて UI を更新することです。
</Tip>

***

## GET /credits/verify-session/{sessionId}/api

API キー認証で Checkout セッションを検証します。サードパーティ連携向けです。

### 認証

API キー認証が必要です。`Authorization` ヘッダーに API キーを付与します。

```
Authorization: Bearer llmgen_your_api_key_here
```

### パスパラメーター

上記と同じです。

### レスポンス

上記と同じです。

<RequestExample>
  ```bash cURL theme={null}
  curl -X GET https://api.llmgenerator.com/api/v1/credits/verify-session/cs_test_a1b2c3d4e5f6/api \
    -H "Authorization: Bearer llmgen_your_api_key_here"
  ```

  ```javascript JavaScript theme={null}
  const sessionId = 'cs_test_a1b2c3d4e5f6';
  const response = await fetch(
    `https://api.llmgenerator.com/api/v1/credits/verify-session/${sessionId}/api`,
    {
      headers: {
        'Authorization': 'Bearer llmgen_your_api_key_here'
      }
    }
  );

  const data = await response.json();
  if (data.success) {
    console.log(`Added ${data.credits} credits`);
  }
  ```

  ```python Python theme={null}
  import requests

  session_id = 'cs_test_a1b2c3d4e5f6'
  response = requests.get(
      f'https://api.llmgenerator.com/api/v1/credits/verify-session/{session_id}/api',
      headers={'Authorization': 'Bearer llmgen_your_api_key_here'}
  )
  data = response.json()
  print(f"Verified: {data['credits']} credits")
  ```
</RequestExample>
