Skip to main content

Redirect Functions

Functions for redirecting users to the checkout page.

getCheckoutUrl

Returns the checkout URL for a session without redirecting.

Signature

function getCheckoutUrl(
sessionId: string,
opts: CheckoutClientOptions,
sessionToken: string
): string

Parameters

ParameterTypeDescription
sessionIdstringThe checkout session ID
optsCheckoutClientOptionsConfiguration options
sessionTokenstringThe session token from createCheckoutSession

Returns

A URL string pointing to the checkout page with authentication.

Example

import { createCheckoutSession, getCheckoutUrl } from '@zkp2p-pay/sdk';

// First, create a session to get the sessionToken
const response = await createCheckoutSession(params, opts);

// Then generate the checkout URL
const url = getCheckoutUrl(
response.session.id,
{
apiBaseUrl: 'https://api.pay.zkp2p.xyz',
checkoutBaseUrl: 'https://pay.zkp2p.xyz',
apiKey: 'your_api_key',
},
response.sessionToken
);

console.log(url);
// https://pay.zkp2p.xyz/checkout?session=sess_abc123&token=xxxxx
tip

In most cases, you can simply use response.checkoutUrl from createCheckoutSession instead of calling getCheckoutUrl directly.


redirectToCheckout

Redirects the browser to the checkout page.

Signature

function redirectToCheckout(
sessionId: string,
sessionToken: string,
opts: CheckoutClientOptions
): void

Parameters

ParameterTypeDescription
sessionIdstringThe checkout session ID
sessionTokenstringThe session token from createCheckoutSession
optsCheckoutClientOptionsConfiguration options

Example

import { createCheckoutSession, redirectToCheckout } from '@zkp2p-pay/sdk';

// Create session on your backend, return sessionId and sessionToken to frontend
const { session, sessionToken } = await createCheckoutSession(params, opts);

// On the frontend, redirect the user
function handleCheckout(sessionId: string, sessionToken: string) {
redirectToCheckout(sessionId, sessionToken, {
apiBaseUrl: 'https://api.pay.zkp2p.xyz',
checkoutBaseUrl: 'https://pay.zkp2p.xyz',
apiKey: 'your_api_key',
});
}

Notes

  • This function calls window.location.assign() internally
  • Only works in browser environments
  • The redirect is immediate and uninterruptible

createSessionAndRedirect

Convenience function that creates a session and immediately redirects.

Signature

function createSessionAndRedirect(
params: CreateSessionRequest,
opts: CheckoutClientOptions
): Promise<CreateSessionResponse>

Parameters

Same as createCheckoutSession.

Returns

Returns the session response, but also triggers a redirect.

Example

import { createSessionAndRedirect } from '@zkp2p-pay/sdk';

// This will create the session AND redirect
await createSessionAndRedirect(
{
merchantId: 'merchant_123',
amountUsdc: '50.00',
destinationChainId: 8453,
destinationToken: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
recipientAddress: '0xYourWallet',
},
{
apiBaseUrl: 'https://api.pay.zkp2p.xyz',
checkoutBaseUrl: 'https://pay.zkp2p.xyz',
apiKey: 'your_api_key',
}
);
// User is now on the checkout page

When to Use

Use createSessionAndRedirect when:

  • You want a simple one-liner for checkout
  • You don't need to do anything between session creation and redirect

Use createCheckoutSession + redirectToCheckout separately when:

  • You need to store the session ID before redirecting
  • You want to show a loading state during session creation
  • You need to handle errors before redirecting

Using the Pre-built Checkout URL

The simplest approach is to use the checkoutUrl returned from createCheckoutSession:

const response = await createCheckoutSession(params, opts);

// Option 1: Simply redirect
window.location.href = response.checkoutUrl;

// Option 2: Open in new tab
window.open(response.checkoutUrl, '_blank');

// Option 3: Return to frontend for display
return { checkoutUrl: response.checkoutUrl };

This is the recommended approach as it includes the session token automatically.