Getting Started

Prerequisites

Before using the SDK, you'll need:

  1. API Credentials - Client ID and Client Secret from the Partner Dashboard (for your backend)

  2. A backend endpoint - To generate checkout tokens for your frontend

  3. Invoice ID - The invoice the customer will be paying

  4. A container element - An HTML element where the SDK will render

circle-exclamation

Step-by-Step Setup

1

Install the SDK

npm install @getalternative/partner-sdk
2

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 });
});
3

Add a Container Element

<div id="payment-container"></div>
4

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;
  },
});
5

Create the Payment Flow

const flow = client.createPaymentFlow({
  containerId: 'payment-container',
  onPaymentSuccess: (payment) => {
    console.log('Payment completed:', payment.id);
  },
  onPaymentError: (error) => {
    console.error('Payment failed:', error.message);
  },
});

Configuration Options

AlternativeClientConfig

Property
Type
Required
Description

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

Environment
Description

production

Live environment for real payments

staging

Test environment for development

triangle-exclamation

Full Example


React Integration


Next Steps

Authenticationchevron-rightPayment Flowchevron-rightThemingchevron-right

Last updated

Was this helpful?