Bearer Token Authentication
All server-side Grain API requests require your API key passed as a Bearer token in theAuthorization header.
Credential Types
Grain uses two types of credentials for different contexts:| Credential | Use | Scope |
|---|---|---|
API Key (CUBEPAY_API_KEY) | Server-side only | Create payment sessions, authenticate with the Grain API |
Merchant ID (NEXT_PUBLIC_CUBEPAY_MERCHANT_ID) | Client-side | Initialize the SDK, identify your merchant account |
Session Token (paymentSessionToken) | Client-side, per session | Authorize SDK operations and session retrieval for a specific payment |
Session Token Authentication
When you create a payment session, the API returns apaymentSessionToken. This short-lived token is scoped to a single session and used for:
- Opening the SDK payment modal (
sdk.open()) - Retrieving session details (
GET /api/payment-sessions)
Session tokens expire automatically and cannot be reused across sessions. Each
POST /payment-sessions call returns a fresh token.Rotate API Keys
Generate a new key
In the Grain Dashboard, go to Settings > API Keys and generate a new key.