---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://truto.one/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Create Stripe Checkout session to buy credits

> Source: https://truto.one/docs/api-reference/admin/billing/create-checkout-top-up-session/

`POST /billing/checkout-top-up-session`

Resource: **Billing**

**Returns a Stripe Checkout session to buy credits now** (hosted `url`, or with `ui_mode` `elements` a `client_secret` for Stripe's Elements in the app). For teams whose usage is paid from a prepaid balance (`usage_payment` `prepaid`). The Checkout takes `amount_cents` in `payment` mode for the team's Stripe customer; the card paid with is saved for later top-ups and becomes the default. Once paid, the balance is credited with the amount, once, and the payment gets its own Stripe invoice. Credits bought never count toward the spend limit, and change no plan.

## Request body

- **`team_id`** _(string)_
  The team; or send it as the `team_id` query parameter.
- **`amount_cents`** _(integer)_
  What to buy, in whole dollars ($50 to $10,000).
- **`ui_mode`** _(string)_
  `hosted` returns `url`, Stripe's own page. `elements` returns `client_secret`, `publishable_key` and `consent_text`, for the app to show Stripe's Payment and Express Checkout Elements itself (Stripe.js `initCheckoutElementsSdk`) and confirm in place; it leaves the app only for a payment method that must redirect, to `return_url`.
  Allowed: `hosted`, `elements`
- **`success_url`** _(string)_
  `hosted` only. Must be on the app origin. Defaults to the Billing settings page.
- **`cancel_url`** _(string)_
  `hosted` only. Must be on the app origin. Defaults to the Billing settings page.
- **`return_url`** _(string)_
  `elements` only. Must be on the app origin. Defaults to the Billing settings page.

## Response body

- **`url`** _(string)_
  Stripe's hosted page (`ui_mode` `hosted`); `null` for `elements`.
- **`client_secret`** _(string)_
  Starts Stripe's Elements (`ui_mode` `elements`, Stripe.js `initCheckoutElementsSdk`); `null` when hosted.
- **`publishable_key`** _(string)_
  The Stripe publishable key to start Stripe.js with; `null` when hosted.
- **`consent_text`** _(string)_
  What the payer agrees to. Stripe shows it on its hosted page; with `elements` the app must show it by its Pay button.
- **`session_id`** _(string)_
- **`amount_cents`** _(integer)_
  What the Checkout charges, in cents.

## Errors

| Status | Description |
| :----- | :---------- |
| `400` | `amount_cents` missing, out of range or not whole dollars, or a redirect URL is not on the app origin or is for the other `ui_mode` |
| `403` | Forbidden if the caller is not an admin of the team |
| `409` | The team does not pay usage from a prepaid balance (or has no billing account or Stripe customer), is on the Free plan (it adds a card by paying, with `POST /billing/checkout-setup-session`), or an earlier credits Checkout of this team is still being confirmed |
| `503` | Billing is not configured on this deployment (`STRIPE_SECRET_KEY` unset), or `ui_mode` `elements` was asked for and `STRIPE_PUBLISHABLE_KEY` is unset |

## Code examples

### curl

```bash
curl -X POST 'https://api.truto.one/billing/checkout-top-up-session' \
  -H 'Authorization: Bearer <your_api_token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "team_id": "your_team_id",
  "amount_cents": 0,
  "ui_mode": "hosted",
  "success_url": "your_success_url",
  "cancel_url": "your_cancel_url",
  "return_url": "your_return_url"
}'
```

### JavaScript

```javascript
const body = {
  "team_id": "your_team_id",
  "amount_cents": 0,
  "ui_mode": "hosted",
  "success_url": "your_success_url",
  "cancel_url": "your_cancel_url",
  "return_url": "your_return_url"
};

const response = await fetch('https://api.truto.one/billing/checkout-top-up-session', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <your_api_token>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(body),
});

const data = await response.json();
console.log(data);
```

### Python

```python
import requests

url = "https://api.truto.one/billing/checkout-top-up-session"
headers = {
    "Authorization": "Bearer <your_api_token>",
    "Content-Type": "application/json",
}
params = {
}
payload = {
    "team_id": "your_team_id",
    "amount_cents": 0,
    "ui_mode": "hosted",
    "success_url": "your_success_url",
    "cancel_url": "your_cancel_url",
    "return_url": "your_return_url"
}

response = requests.post(url, headers=headers, params=params, json=payload)
print(response.json())
```
