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

# Create Billing Portal Session

> Create a Stripe billing portal session for managing subscription and payment methods.

## Overview

Creates a Stripe Customer Portal session where users can:

* Update payment methods
* View billing history
* Change subscription plans
* Download invoices
* Cancel subscription

Requires session-based authentication (JWT access token).

<Note>
  **For third-party integrations:** Use `POST /subscriptions/portal/api` with API Key authentication instead.
</Note>

## Authentication

<ParamField header="Authorization" type="string" required>
  Your JWT access token. Format: `Bearer YOUR_ACCESS_TOKEN`
</ParamField>

## Request Body

<ParamField body="returnUrl" type="string">
  URL to redirect to when user exits the portal. Defaults to application dashboard.
</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 Response theme={null}
  {
    "url": "https://billing.stripe.com/p/session/test_a1b2c3d4..."
  }
  ```
</ResponseExample>

## Response Fields

<ResponseField name="url" type="string">
  URL to redirect the user to. This is a Stripe-hosted billing portal.
</ResponseField>

## Portal Capabilities

The Stripe Customer Portal allows users to:

| Feature             | Description                         |
| ------------------- | ----------------------------------- |
| **Payment Methods** | Add, update, or remove credit cards |
| **Invoices**        | View and download past invoices     |
| **Plan Changes**    | Upgrade or downgrade subscription   |
| **Cancellation**    | Cancel subscription                 |
| **Billing History** | View all past transactions          |

## Error Responses

<ResponseField name="400">
  Bad Request - No billing account found. User must make a purchase first.
</ResponseField>

<ResponseField name="401">
  Unauthorized - Invalid or missing token.
</ResponseField>

<Note>
  The billing portal is only available for users who have previously made a purchase or have an active subscription. New users should use the checkout endpoint instead.
</Note>

***

## POST /subscriptions/portal/api

Creates a Stripe Customer Portal session using API Key authentication. Ideal for third-party integrations.

### Authentication

Requires API Key authentication. Include your API key in the `Authorization` header:

```
Authorization: Bearer llmgen_your_api_key_here
```

### Request Body

Same as `/subscriptions/portal` above.

### Response

Same as `/subscriptions/portal` above.

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

## Best Practices

* **Link in settings**: Add a "Manage Billing" button in your app's settings page
* **After checkout**: Redirect users to the portal after subscription purchase
* **Support**: Provide portal access as a self-service option for billing issues
