> ## 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 セッションを作成します。

## 概要

プランへの登録のため Stripe Checkout セッションを作成します。決済完了用にユーザーへリダイレクトすべき URL を返します。セッション認証（JWT アクセストークン）が必要です。

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

## 認証

<ParamField header="Authorization" type="string" required>
  JWT アクセストークン。形式: `Bearer YOUR_ACCESS_TOKEN`
</ParamField>

## リクエストボディ

<ParamField body="planId" type="string" required>
  契約するプラン ID。次のいずれかです: `starter`、`professional`、`business`、`agency`。
</ParamField>

<ParamField body="billingCycle" type="string" default="monthly">
  請求頻度。`monthly` または `yearly`。
</ParamField>

<ParamField body="successUrl" type="string">
  決済成功後のリダイレクト URL。既定はアプリのダッシュボードです。
</ParamField>

<ParamField body="cancelUrl" type="string">
  ユーザーが決済をキャンセルした場合のリダイレクト URL。既定は料金ページです。
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/checkout \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "planId": "professional",
      "successUrl": "https://yourapp.com/subscription/success",
      "cancelUrl": "https://yourapp.com/pricing"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/checkout', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      planId: 'professional',
      successUrl: 'https://yourapp.com/subscription/success',
      cancelUrl: 'https://yourapp.com/pricing'
    })
  });

  const data = await response.json();
  // Redirect user to Stripe checkout
  window.location.href = data.url;
  ```

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

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/checkout',
      headers={
          'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
          'Content-Type': 'application/json'
      },
      json={
          'planId': 'professional',
          'successUrl': 'https://yourapp.com/subscription/success',
          'cancelUrl': 'https://yourapp.com/pricing'
      }
  )
  data = response.json()
  print(f"Redirect to: {data['url']}")
  ```
</RequestExample>

<ResponseExample>
  ```json レスポンス theme={null}
  {
    "sessionId": "cs_test_a1b2c3d4e5f6g7h8i9j0",
    "url": "https://checkout.stripe.com/c/pay/cs_test_a1b2c3d4...",
    "plan": {
      "id": "professional",
      "name": "Professional",
      "monthlyCredits": 3000,
      "priceCents": 1299
    }
  }
  ```
</ResponseExample>

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

<ResponseField name="sessionId" type="string">
  Stripe Checkout セッション ID。後からセッションを検証する際に利用できます。
</ResponseField>

<ResponseField name="url" type="string">
  決済ページへユーザーを転送する URL。Stripe ホストの Checkout です。
</ResponseField>

<ResponseField name="plan" type="object">
  選択されたプランの詳細。
</ResponseField>

## Checkout の流れ

1. **セッション作成**: 希望プランで本エンドポイントを呼び出す
2. **ユーザーを転送**: 返却された `url` へ誘導する
3. **決済処理**: ユーザーが Stripe 上で支払いを完了する
4. **Webhook**: Stripe が Webhook で通知する
5. **成功後の転送**: ユーザーが `successUrl` へリダイレクトされる
6. **検証（任意）**: `/subscriptions/current` で契約状態を確認する

## エラーレスポンス

<ResponseField name="400">
  不正なリクエスト — 無効なプラン ID、または既にアクティブな契約がある場合など。
</ResponseField>

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

<Note>
  既にアクティブな契約がある場合は Checkout ではなく、請求ポータル（`/subscriptions/portal`）からプラン変更する必要があります。
</Note>

***

## POST /subscriptions/checkout/api

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

### 認証

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

```
Authorization: Bearer llmgen_your_api_key_here
```

### リクエストボディ

`/subscriptions/checkout` と同一です。

### レスポンス

`/subscriptions/checkout` と同一です。

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/checkout/api \
    -H "Authorization: Bearer llmgen_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
      "planId": "professional",
      "successUrl": "https://yourapp.com/subscription/success",
      "cancelUrl": "https://yourapp.com/pricing"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/checkout/api', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer llmgen_your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      planId: 'professional',
      successUrl: 'https://yourapp.com/subscription/success',
      cancelUrl: 'https://yourapp.com/pricing'
    })
  });

  const data = await response.json();
  // Redirect user to Stripe checkout
  window.location.href = data.url;
  ```

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

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/checkout/api',
      headers={
          'Authorization': 'Bearer llmgen_your_api_key_here',
          'Content-Type': 'application/json'
      },
      json={
          'planId': 'professional',
          'successUrl': 'https://yourapp.com/subscription/success',
          'cancelUrl': 'https://yourapp.com/pricing'
      }
  )
  data = response.json()
  print(f"Redirect to: {data['url']}")
  ```
</RequestExample>
