Skip to main content

SDK Reference

@zkp2p/pay-sdk creates checkout orders and payouts from your backend. For a checkout order you pass an amount and a payout destination, and get back the order and a hosted checkout URL that already carries the order token.

SDK version

This reference covers @zkp2p/pay-sdk 9.x, versioned in lockstep with @zkp2p/pay-shared; 9.1.0 is the first 9.x release on npm. Coming from 8.x, the SDK functions are unchanged. The shared types add CheckoutPayment.payout (payout status and timing, see deferred payment payouts), the xmoney (X Money) payment platform, and the PAYMENT_PAYOUT_RELEASED webhook event. See the v8 to v9 migration guide. 9.1.0 adds checkout grants and per-checkout expiry; nothing existing changes.

Install​

npm install @zkp2p/pay-sdk@9
# only if you import from it directly; the SDK already depends on it
npm install @zkp2p/pay-shared@9

@9 installs the latest published 9.x. On an earlier major version, work through the migration guides in order first.

  • Node.js 18+, or any runtime with fetch.
  • ESM only. From CommonJS, use const { createCheckout } = await import('@zkp2p/pay-sdk').
  • The embedded-checkout helpers live on the @zkp2p/pay-sdk/embedded subpath, which TypeScript resolves only with moduleResolution set to node16, nodenext or bundler.

Create orders on your backend. Your merchant API key must never reach a browser.

Quick example​

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

const checkout = await createCheckout(
{
requestedFiatAmount: '100.00',
requestedFiatCurrency: 'EUR',
destinationChainId: 8453,
destinationToken: 'USDC',
successUrl: 'https://example.com/success',
},
{
apiBaseUrl: 'https://api.pay.peer.xyz',
checkoutBaseUrl: 'https://pay.peer.xyz',
apiKey: process.env.ZKPAY_API_KEY!,
}
);

console.log(checkout.checkoutUrl);
// https://pay.peer.xyz/?order=...&token=...

Each order takes one amount mode: USDC, fiat, or an open amount the customer types. See createCheckout for the parameters and Configuration for the options, including why checkoutBaseUrl must be set. Integration options covers the redirect, a new tab, and the embedded iframe.

TypeScript​

The SDK re-exports the order, payment, payout, dispute and webhook types from @zkp2p/pay-shared.

import {
isDisputeWebhook,
isPayoutWebhook,
type CheckoutClientOptions,
type CheckoutOrder,
type CreateOrderRequest,
type WebhookPayload,
} from '@zkp2p/pay-sdk';

const options: CheckoutClientOptions = {
apiBaseUrl: 'https://api.pay.peer.xyz',
checkoutBaseUrl: 'https://pay.peer.xyz',
apiKey: process.env.ZKPAY_API_KEY,
};

const request: CreateOrderRequest = {
requestedUsdcAmount: '50.00',
destinationChainId: 8453,
destinationToken: 'USDC',
};

// A webhook body, after verifying its signature: narrow before reading data.
function orderFromWebhook(payload: WebhookPayload): CheckoutOrder | null {
if (isPayoutWebhook(payload) || isDisputeWebhook(payload)) return null;
return payload.data.order;
}

Available Functions​

FunctionDescription
createCheckoutCreate an order and its checkout URL
getCheckoutUrlBuild a checkout URL
redirectToCheckoutRedirect the browser to checkout
createCheckoutAndRedirectCreate an order and redirect in one call
getMerchantFetch your merchant profile
checkQuoteAvailabilityCheck whether an amount can be filled, and nearby amounts that can, before creating an order
createPayoutCreate a payout and get its funding address
getPayoutFetch one payout
listPayoutsList payouts, newest first, with filters
cancelPayoutCancel a payout that is awaiting funding and has received nothing
createCheckoutGrantLet one user create a payout or an on-ramp order on Peer's page, with the money details set by them
buildCheckoutGrantUrlBuild a grant's link in your app

getMerchant​

function getMerchant(opts: CheckoutClientOptions): Promise<MerchantInfo>

Returns the merchant that owns the API key (GET /api/v1/merchants/me).

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

const merchant = await getMerchant({
apiBaseUrl: 'https://api.pay.peer.xyz',
apiKey: process.env.ZKPAY_API_KEY!,
});

console.log(merchant.name);
console.log(merchant.tier); // 'BASE' | 'PRO' | 'CONCIERGE', or null with no plan; new accounts start on 'PRO', not yet activated
console.log(merchant.planActivatedAt); // when the live plan was activated, or null; live orders need it
console.log(merchant.merchantConfig?.enabledRails);

Error handling​

Every call that reaches the API throws PayApiError when the API answers with a non-2xx status or a success: false envelope:

class PayApiError extends Error {
readonly name: string; // 'PayApiError'
readonly statusCode: number;
readonly errorCode?: string;
readonly responseObject?: unknown;
readonly fieldErrors?: Readonly<Record<string, readonly string[]>>;
readonly formErrors?: readonly string[];
}

message is the API's message. For a validation failure the SDK appends a short per-field summary, such as Invalid request: notes: Expected object, received string; the full detail is on fieldErrors and formErrors. Branch on errorCode, not on the message.

import { createCheckout, PayApiError } from '@zkp2p/pay-sdk';

try {
await createCheckout(params, opts);
} catch (error) {
if (error instanceof PayApiError) {
console.error(error.statusCode, error.errorCode, error.message);
}
throw error;
}

Order-creation codes, including the plan and monthly cap codes, are listed under createCheckout errors.

These checks fail before any request is sent and are not PayApiError:

InputThrown byError and message
Missing apiKeyEvery function that calls the APIError: API key is required. Pass apiKey in options.
Not exactly one amount modecreateCheckout, createCheckoutAndRedirectError: Provide exactly one of requestedUsdcAmount, requestedFiatAmount + requestedFiatCurrency, or openAmount.
Invalid preselectedMethodcreateCheckout, createCheckoutAndRedirect, getCheckoutUrl, redirectToCheckoutTypeError: Invalid preselectedMethod: …
Invalid destinationChainIdcheckQuoteAvailabilityTypeError: Invalid destinationChainId: …
Invalid or missing payout idempotencyKeycreatePayoutTypeError: idempotencyKey must be 8-128 characters of letters, digits, "_" or "-"
Empty payoutIdgetPayout, cancelPayoutTypeError: payoutId must be a non-empty string

A network failure or an aborted signal rejects with the error fetch raised. A 2xx response without the expected envelope throws a plain Error.