What are session tokens?
Session tokens are short-lived credentials that identify one of your customers to BillingOS. They’re created by your server and used by the React SDK to make authenticated API calls. Why tokens instead of API keys? API keys give full access to your organization’s data. Session tokens are scoped to a single customer and expire after a set time — making them safe to use in the browser.How the flow works
SDK requests a token
The
BillingOSProvider calls your server endpoint (e.g., /api/billingos-session) to get a token.Your server creates the token
Using the Node SDK and your secret key, your server creates a session token tied to the user’s ID.
SDK uses the token
All subsequent API calls from the SDK include this token. No extra login for the user.
Creating session tokens
Server-side (Node SDK)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
externalUserId | string | Yes | Your user’s unique ID from your database |
externalOrganizationId | string | No | Organization ID for B2B scenarios |
expiresIn | number | No | Token lifetime in seconds (60–86400, default: 3600) |
metadata | Record<string, any> | No | Additional metadata (IP, user agent, etc.) |
Response
Token prefixes and auto-routing
Session tokens are automatically prefixed based on your API key environment:| API key prefix | Token prefix | Routes to |
|---|---|---|
sk_test_* | bos_session_test_* | Sandbox API |
sk_live_* | bos_session_live_* | Production API |
Using tokens in the React SDK
Automatic mode (recommended)
Pass asessionTokenUrl and the SDK handles fetching and refreshing:
- Call your endpoint when the provider mounts
- Store the token in memory
- Automatically refresh before expiry
Manual mode
If you need more control, pass the token directly:Security best practices
- Always authenticate the user before creating a session token. Don’t create tokens for anonymous users.
- Use short expiry times. The default 1-hour expiry is a good balance between security and UX.
- Pass your real user ID as
externalUserId. This ensures each customer only sees their own billing data.