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
): string

Parameters

ParameterTypeDescription
sessionIdstringThe checkout session ID
optsCheckoutClientOptionsConfiguration options

Returns

A URL string pointing to the checkout page.

Example

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

const url = getCheckoutUrl('sess_abc123', {
apiBaseUrl: 'https://api.zkp2p-pay.xyz',
checkoutBaseUrl: 'https://checkout.zkp2p-pay.xyz',
apiKey: 'your_api_key',
});

// Use the URL however you need
console.log(url);
// https://checkout.zkp2p-pay.xyz/checkout?session=sess_abc123

redirectToCheckout

Redirects the browser to the checkout page.

Signature

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

Parameters

ParameterTypeDescription
sessionIdstringThe checkout session ID
optsCheckoutClientOptionsConfiguration options

Example

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

// In a button click handler
function handleCheckout() {
redirectToCheckout(sessionId, {
apiBaseUrl: 'https://api.zkp2p-pay.xyz',
checkoutBaseUrl: 'https://checkout.zkp2p-pay.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.zkp2p-pay.xyz',
checkoutBaseUrl: 'https://checkout.zkp2p-pay.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