Getting Started
Prerequisites
Before using the SDK, you'll need:
API Credentials - Client ID and Client Secret from the Partner Dashboard (for your backend)
A backend endpoint - To generate checkout tokens for your frontend
Invoice ID - The invoice the customer will be paying
A container element - An HTML element where the SDK will render
Important: API credentials (Client ID and Secret) should only be used on your backend server. The frontend SDK uses access tokens generated by your backend to keep credentials secure.
Step-by-Step Setup
Create a Backend Token Endpoint
Your backend generates checkout tokens using your API credentials:
// backend/routes/checkout.ts (example with Express)
import express from 'express';
const router = express.Router();
router.post('/api/checkout-token', async (req, res) => {
const { customerId, invoiceId } = req.body;
// 1. Get OAuth token using client credentials
const oauthResponse = await fetch('https://public-api.alternativepayments.io/oauth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64')}`,
},
body: 'grant_type=client_credentials',
});
const { access_token } = await oauthResponse.json();
// 2. Generate checkout token
const checkoutResponse = await fetch('https://public-api.alternativepayments.io/v1/checkout-auth/init', {
method: 'POST',
headers: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ customer_id: customerId, invoice_id: invoiceId }),
});
const { token, expires_at } = await checkoutResponse.json();
res.json({ token, expiresAt: expires_at });
});Initialize the SDK
import { AlternativeClient } from '@getalternative/partner-sdk';
// Fetch token from your backend
const { token } = await fetch('/api/checkout-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ customerId: 'cus_xxx', invoiceId: 'inv_xxx' }),
}).then(res => res.json());
// Initialize SDK
const client = await AlternativeClient.create({
accessToken: token,
environment: 'production',
onAccessTokenExpired: async () => {
// Re-fetch token when expired
const response = await fetch('/api/checkout-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ customerId: 'cus_xxx', invoiceId: 'inv_xxx' }),
});
const { token } = await response.json();
return token;
},
});Configuration Options
AlternativeClientConfig
accessToken
string
Yes
JWT access token from your backend
environment
'production' | 'staging'
No
API environment (default: production)
baseUrl
string
No
Custom API base URL (overrides environment)
timeout
number
No
Request timeout in ms (default: 30000)
retries
number
No
Number of retry attempts (default: 3)
onAccessTokenExpired
() => Promise<string>
No
Callback to fetch new token when expired
theme
ThemeConfig
No
Default theme for all components
Environments
production
Live environment for real payments
staging
Test environment for development
Important: Use separate API credentials for each environment. Production credentials will not work in staging and vice versa.
Full Example
React Integration
Next Steps
AuthenticationPayment FlowThemingLast updated
Was this helpful?
