Trigger a Checkout Session from Your Website
This guide shows you how to create a checkout session from any website using JavaScript and redirect your customer to your TagadaPay hosted checkout page.
No authentication required. The checkout initialization endpoint is completely public. You only need your storeId and product variant IDs.
Overview
Your Website → TagadaPay Init Endpoint → Your Checkout Page
(customer clicks) (creates session) (hosted on your domain)
When a customer clicks “Buy Now” on your website, you redirect them to the TagadaPay initialization endpoint with the product details. TagadaPay creates a checkout session and redirects the customer to your checkout page.
Step 1: Add the Helper Function
Add this function to your website’s JavaScript:
function createCheckoutUrl({
baseApiUrl,
checkoutUrl,
storeId,
currency = 'USD',
items = [],
defaultItem = null,
log = false,
} = {}) {
if (!baseApiUrl) throw new Error('baseApiUrl is required');
if (!storeId) throw new Error('storeId is required');
if (!checkoutUrl) throw new Error('checkoutUrl is required');
const lineItems =
items.length > 0
? items
: defaultItem
? [defaultItem]
: [];
if (lineItems.length === 0) {
throw new Error('At least one line item is required');
}
const params = new URLSearchParams({
storeId,
currency,
checkoutUrl,
items: JSON.stringify(lineItems),
});
const url = `${baseApiUrl}?${params.toString()}`;
if (log) {
console.info('Checkout URL:', url);
}
return url;
}
Step 2: Build the Checkout URL
const checkoutUrl = createCheckoutUrl({
baseApiUrl: 'https://app.tagadapay.com/api/public/v1/checkout/init',
checkoutUrl: 'https://secure.yourdomain.com/checkout',
storeId: 'store_your_store_id',
currency: 'USD',
items: [
{
variantId: 'variant_abc123',
priceId: 'price_xyz789', // optional
quantity: 1,
},
],
log: true,
});
Step 3: Redirect the Customer
// On button click
document.getElementById('buy-now').addEventListener('click', () => {
window.location.href = checkoutUrl;
});
Parameters
Required
| Parameter | Type | Description |
|---|
baseApiUrl | string | Always https://app.tagadapay.com/api/public/v1/checkout/init |
storeId | string | Your TagadaPay store ID (e.g., store_019dd15cdb5b) |
checkoutUrl | string | Your hosted checkout page URL (e.g., https://secure.yourdomain.com/checkout) |
items | array | Array of line items to add to the cart |
Line Item Fields
| Field | Type | Required | Description |
|---|
variantId | string | Yes | Product variant ID |
priceId | string | No | Specific price ID (for subscriptions) |
quantity | number | Yes | Quantity |
Optional
| Parameter | Type | Description |
|---|
currency | string | Currency code, defaults to USD |
defaultItem | object | A fallback line item used when items is empty |
log | boolean | Log the generated URL to the console (defaults to false) |
Examples
Single Product
const url = createCheckoutUrl({
baseApiUrl: 'https://app.tagadapay.com/api/public/v1/checkout/init',
checkoutUrl: 'https://secure.mystore.com/checkout',
storeId: 'store_abc123',
currency: 'USD',
items: [
{ variantId: 'variant_main_product', quantity: 1 },
],
});
Bundle (Multiple Items)
const url = createCheckoutUrl({
baseApiUrl: 'https://app.tagadapay.com/api/public/v1/checkout/init',
checkoutUrl: 'https://secure.mystore.com/checkout',
storeId: 'store_abc123',
currency: 'USD',
items: [
{ variantId: 'variant_product_a', quantity: 2 },
{ variantId: 'variant_product_b', quantity: 1 },
],
});
Subscription
const url = createCheckoutUrl({
baseApiUrl: 'https://app.tagadapay.com/api/public/v1/checkout/init',
checkoutUrl: 'https://secure.mystore.com/checkout',
storeId: 'store_abc123',
currency: 'USD',
items: [
{
variantId: 'variant_subscription',
priceId: 'price_monthly_plan',
quantity: 1,
},
],
});
Next Steps