> ## 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.

# 請求ポータルセッションの作成

> 契約や支払い方法を管理する Stripe Billing Portal のセッションを作成します。

## 概要

Stripe Customer Portal のセッションを作成します。ユーザーはそこで次の操作ができます。

* 支払い方法の更新
* 請求履歴の確認
* プラン変更
* 請求書のダウンロード
* 契約の解約

セッション認証（JWT アクセストークン）が必要です。

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

## 認証

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

## リクエストボディ

<ParamField body="returnUrl" type="string">
  ポータル終了後のリダイレクト URL。既定はアプリのダッシュボードです。
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/portal \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "returnUrl": "https://yourapp.com/settings/billing"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/portal', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      returnUrl: 'https://yourapp.com/settings/billing'
    })
  });

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

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

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/portal',
      headers={
          'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
          'Content-Type': 'application/json'
      },
      json={
          'returnUrl': 'https://yourapp.com/settings/billing'
      }
  )
  data = response.json()
  print(f"Redirect to: {data['url']}")
  ```
</RequestExample>

<ResponseExample>
  ```json レスポンス theme={null}
  {
    "url": "https://billing.stripe.com/p/session/test_a1b2c3d4..."
  }
  ```
</ResponseExample>

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

<ResponseField name="url" type="string">
  ユーザーがアクセスする URL。Stripe がホストする Billing Portal です。
</ResponseField>

## ポータルでできること

Stripe Customer Portal ではユーザーは次が行えます。

| 機能        | 説明                   |
| --------- | -------------------- |
| **支払い方法** | カードの追加・更新・削除         |
| **請求書**   | 過去の請求書の閲覧とダウンロード     |
| **プラン変更** | サブスクのアップグレード／ダウングレード |
| **解約**    | サブスクの解約              |
| **請求履歴**  | すべての請求・取引の閲覧         |

## エラーレスポンス

<ResponseField name="400">
  不正なリクエスト — 請求用アカウントが見つからない場合。先に購入が必要です。
</ResponseField>

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

<Note>
  Billing Portal は、過去に購入済みまたはアクティブな契約があるユーザーのみ利用できます。新規ユーザーは Checkout エンドポイントを利用してください。
</Note>

***

## POST /subscriptions/portal/api

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

### 認証

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

```
Authorization: Bearer llmgen_your_api_key_here
```

### リクエストボディ

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

### レスポンス

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

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/portal/api \
    -H "Authorization: Bearer llmgen_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
      "returnUrl": "https://yourapp.com/settings/billing"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/portal/api', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer llmgen_your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      returnUrl: 'https://yourapp.com/settings/billing'
    })
  });

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

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

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/portal/api',
      headers={
          'Authorization': 'Bearer llmgen_your_api_key_here',
          'Content-Type': 'application/json'
      },
      json={
          'returnUrl': 'https://yourapp.com/settings/billing'
      }
  )
  data = response.json()
  print(f"Redirect to: {data['url']}")
  ```
</RequestExample>

## 運用上のヒント

* **設定へのリンク**: アプリ設定に「請求を管理」ボタンを置く
* **Checkout 後**: 契約購入直後はポータルへ誘導するとよい
* **サポート**: 請求に関するセルフサービスとしてポータルを案内する
