Billing API Overview
Complete guide to the Stripe-based billing API for managing subscriptions, usage, and customer portals.
Billing API
The Billing API provides a complete Stripe integration for managing subscriptions, metered usage, and customer billing portals.
Authentication
All billing endpoints require authentication. Include your session token in the request headers:
Authorization: Bearer <session_token>Base URL
/api/billingEndpoints Overview
| Endpoint | Method | Description |
|---|---|---|
/checkout | POST | Create a Stripe checkout session for subscription |
/subscription | GET | Retrieve current subscription status |
/usage | GET | Get metered usage for the current billing period |
/portal | POST | Create a Stripe customer portal session |
Response Format
All responses follow a consistent JSON structure:
{
"success": true,
"data": { ... },
"error": null
}Error responses:
{
"success": false,
"data": null,
"error": {
"code": "UNAUTHORIZED",
"message": "Authentication required"
}
}Error Codes
| Code | HTTP Status | Description |
|---|---|---|
UNAUTHORIZED | 401 | Missing or invalid authentication |
FORBIDDEN | 403 | Insufficient permissions |
NOT_FOUND | 404 | Resource not found |
BAD_REQUEST | 400 | Invalid request parameters |
INTERNAL_ERROR | 500 | Server error |
Webhook Events
The billing system listens to Stripe webhooks for:
checkout.session.completed- Subscription createdinvoice.payment_succeeded- Payment receivedinvoice.payment_failed- Payment failedcustomer.subscription.updated- Subscription changedcustomer.subscription.deleted- Subscription cancelled
Rate Limits
- 100 requests per minute per user
- 1000 requests per hour per organization
SDK Example
import { createBillingClient } from '@/lib/billing/client';
const billing = createBillingClient();
// Get subscription status
const subscription = await billing.getSubscription();
// Create checkout session
const session = await billing.createCheckout({
priceId: 'price_123',
successUrl: '/dashboard?success=true',
cancelUrl: '/pricing?canceled=true'
});