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.
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/embeddedsubpath, which TypeScript resolves only withmoduleResolutionset tonode16,nodenextorbundler.
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
| Function | Description |
|---|---|
createCheckout | Create an order and its checkout URL |
getCheckoutUrl | Build a checkout URL |
redirectToCheckout | Redirect the browser to checkout |
createCheckoutAndRedirect | Create an order and redirect in one call |
getMerchant | Fetch your merchant profile |
checkQuoteAvailability | Check whether an amount can be filled, and nearby amounts that can, before creating an order |
createPayout | Create a payout and get its funding address |
getPayout | Fetch one payout |
listPayouts | List payouts, newest first, with filters |
cancelPayout | Cancel a payout that is awaiting funding and has received nothing |
createCheckoutGrant | Let one user create a payout or an on-ramp order on Peer's page, with the money details set by them |
buildCheckoutGrantUrl | Build 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:
| Input | Thrown by | Error and message |
|---|---|---|
Missing apiKey | Every function that calls the API | Error: API key is required. Pass apiKey in options. |
| Not exactly one amount mode | createCheckout, createCheckoutAndRedirect | Error: Provide exactly one of requestedUsdcAmount, requestedFiatAmount + requestedFiatCurrency, or openAmount. |
Invalid preselectedMethod | createCheckout, createCheckoutAndRedirect, getCheckoutUrl, redirectToCheckout | TypeError: Invalid preselectedMethod: … |
Invalid destinationChainId | checkQuoteAvailability | TypeError: Invalid destinationChainId: … |
Invalid or missing payout idempotencyKey | createPayout | TypeError: idempotencyKey must be 8-128 characters of letters, digits, "_" or "-" |
Empty payoutId | getPayout, cancelPayout | TypeError: 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.