> ## 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 セッションの作成

> クレジット購入用の Stripe Checkout セッションを作成するエンドポイントです。

## POST /credits/checkout

クレジットパッケージ購入のための Stripe Checkout セッションを作成します。ログインから得た JWT アクセストークンによるセッション認証が必要です。

<Note>
  **サードパーティ連携の場合:** API キー認証の `POST /credits/checkout/api` を利用してください。
</Note>

### リクエストボディ

<ParamField body="packageId" type="string" required>
  購入するクレジットパッケージの ID。利用可能な一覧は `/credits/packages` で取得します。
</ParamField>

<ParamField body="successUrl" type="string">
  購入成功後にリダイレクトする URL。未指定時はダッシュボード（成功表示）へ。
</ParamField>

<ParamField body="cancelUrl" type="string">
  購入キャンセル時のリダイレクト先 URL。未指定時はダッシュボード（キャンセル表示）へ。
</ParamField>

### レスポンス

<ResponseField name="sessionId" type="string">
  Stripe Checkout セッションの ID。
</ResponseField>

<ResponseField name="url" type="string">
  Stripe の決済ページ URL。購入完了のためユーザーにリダイレクトします。
</ResponseField>

<ResponseField name="package" type="object">
  選択されたパッケージの詳細。

  <Expandable title="package のプロパティ">
    <ResponseField name="id" type="string">
      パッケージ識別子。
    </ResponseField>

    <ResponseField name="name" type="string">
      パッケージ名。
    </ResponseField>

    <ResponseField name="credits" type="number">
      含まれるクレジット数。
    </ResponseField>

    <ResponseField name="priceCents" type="number">
      セント単位の価格。
    </ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/credits/checkout \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
          "packageId": "pkg_pro",
          "successUrl": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
          "cancelUrl": "https://example.com/cancel"
        }'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/credits/checkout', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      packageId: 'pkg_pro'
    })
  });

  const data = await response.json();
  // Redirect user to data.url
  window.location.href = data.url;
  ```
</RequestExample>

<ResponseExample>
  ```json 成功 (200) theme={null}
  {
    "sessionId": "cs_test_a1b2c3d4e5f6g7h8i9j0",
    "url": "https://checkout.stripe.com/c/pay/cs_test_a1b2c3d4e5f6g7h8i9j0",
    "package": {
      "id": "pkg_pro",
      "name": "Pro Pack",
      "credits": 500,
      "priceCents": 1999
    }
  }
  ```

  ```json パッケージが見つからない (404) theme={null}
  {
    "message": "Credit package not found"
  }
  ```
</ResponseExample>

***

## POST /credits/checkout/api

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

### 認証

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

```
Authorization: Bearer llmgen_your_api_key_here
```

### リクエストボディ

`/credits/checkout` と同じです。

### レスポンス

`/credits/checkout` と同じです。

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/credits/checkout/api \
    -H "Authorization: Bearer llmgen_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
          "packageId": "pkg_pro",
          "successUrl": "https://yourapp.com/success?session_id={CHECKOUT_SESSION_ID}",
          "cancelUrl": "https://yourapp.com/cancel"
        }'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/credits/checkout/api', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer llmgen_your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      packageId: 'pkg_pro',
      successUrl: 'https://yourapp.com/success?session_id={CHECKOUT_SESSION_ID}',
      cancelUrl: 'https://yourapp.com/cancel'
    })
  });

  const data = await response.json();
  // Redirect user to data.url in your app
  ```

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

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/credits/checkout/api',
      headers={
          'Authorization': 'Bearer llmgen_your_api_key_here',
          'Content-Type': 'application/json'
      },
      json={
          'packageId': 'pkg_pro',
          'successUrl': 'https://yourapp.com/success',
          'cancelUrl': 'https://yourapp.com/cancel'
      }
  )

  data = response.json()
  ```
</RequestExample>
