@zkp2p/pay-sdk
v9.1.0
Published
ZKP2P Pay checkout SDK
Readme
@zkp2p/pay-sdk
TypeScript SDK for Peer Pay: create checkout orders and payouts from your backend, and embed the hosted checkout. Documentation: https://docs.pay.peer.xyz
Maintainers: use the GitHub OIDC publishing guide for releases.
Install
npm install @zkp2p/[email protected]ESM only, with TypeScript declarations. Calls that reach the API need fetch (Node.js 18+ or a
browser).
Recommended flow
Create the order on your backend and send the customer to checkoutUrl. Never put your merchant
API key in browser code.
import { createCheckout } from '@zkp2p/pay-sdk';
const checkout = await createCheckout(
{
requestedFiatAmount: '25.00',
requestedFiatCurrency: 'EUR',
idempotencyKey: 'cart_123',
successUrl: 'https://merchant.example/success',
notes: { cartId: 'cart_123' },
},
{
apiBaseUrl: 'https://api.pay.peer.xyz',
checkoutBaseUrl: 'https://pay.peer.xyz',
apiKey: process.env.ZKPAY_API_KEY!,
},
);
// null only when this call replayed an earlier one with the same idempotencyKey:
// reuse the checkoutUrl you saved the first time.
const checkoutUrl = checkout.checkoutUrl;Fulfill on the ORDER_FULFILLED webhook, never on the customer's return to your site.
Functions
| Function | Does |
|----------|------|
| createCheckout(params, options) | Creates an order (POST /api/v1/orders) and returns order, orderToken and checkoutUrl. params takes exactly one amount mode: requestedUsdcAmount, requestedFiatAmount + requestedFiatCurrency, or openAmount. |
| getCheckoutUrl(orderId, orderToken, options) | Builds a checkout URL without a request. |
| redirectToCheckout(orderId, orderToken, options) | Navigates the browser to checkout. |
| createCheckoutAndRedirect(params, options) | Creates an order and redirects. Runs in the browser, so it exposes your API key; prefer createCheckout on your backend. |
| getMerchant(options) | Reads the merchant that owns the API key (GET /api/v1/merchants/me). |
| checkQuoteAvailability(params, options) | Checks whether an amount can be filled before you create an order, with nearby amounts that can. |
| createPayout(params, { idempotencyKey }, options) | Creates a payout (POST /api/v1/payouts). |
| getPayout(payoutId, options) | Reads a payout. |
| listPayouts(params, options) | Lists payouts, newest first. |
| cancelPayout(payoutId, options) | Cancels a payout that is awaiting funding and has received nothing. |
| createCheckoutGrant(params, options) | Mints a single-use checkout grant (POST /api/v1/checkout-grants): permission for one user to create a payout or an on-ramp order on Peer's page, with the money details set by them and not by your server. |
| buildCheckoutGrantUrl(grant, prefills, options) | Builds a grant's link in your app, without a request. |
The SDK also re-exports the order, payment, payout, dispute and webhook types and constants of
@zkp2p/pay-shared. Parameters, idempotency, limits and errors:
createCheckout,
quote availability,
payouts,
checkout grants.
Client options
type CheckoutClientOptions = {
apiBaseUrl: string; // https://api.pay.peer.xyz
checkoutBaseUrl?: string; // https://pay.peer.xyz; defaults to apiBaseUrl, so always set it
apiKey?: string; // required by every function that calls the API
fetcher?: typeof fetch;
signal?: AbortSignal;
preselectedMethod?: CheckoutPreselectMethod; // e.g. 'venmo' or 'zelle-capitalone'; adds method=<value> to checkoutUrl
};
type PayoutClientOptions = {
apiBaseUrl: string;
apiKey: string;
fetcher?: typeof fetch;
signal?: AbortSignal;
};See Configuration.
Webhook payloads
WebhookPayload is a union of order, payout and dispute payloads. After verifying the
signature, narrow it before reading data:
import { isDisputeWebhook, isPayoutWebhook, type WebhookPayload } from '@zkp2p/pay-sdk';
function handleWebhook(payload: WebhookPayload) {
if (isPayoutWebhook(payload)) {
console.log(payload.data.payoutId, payload.data.status);
} else if (isDisputeWebhook(payload)) {
console.log(payload.data.disputeId, payload.data.status);
} else {
console.log(payload.data.order, payload.data.payment);
}
}See webhook verification.
Embedded checkout
@zkp2p/pay-sdk/embedded exports ensureEmbedModeUrl, EMBED_EVENT_CHANNEL,
isEmbeddedCheckout, buildEmbedCheckoutEvent, postEmbedCheckoutEvent and the
EmbedCheckoutEventType type. Load ensureEmbedModeUrl(checkoutUrl) in an iframe and listen for
messages on EMBED_EVENT_CHANNEL:
checkout.success: the payment completed.checkout.failed: the payment failed, was cancelled or was refunded.checkout.closed: the customer dismissed a checkout that had nothing payable. Close the iframe without failure handling. It does not prove nothing was paid; check the order.
If you set sandbox on the iframe, include allow-popups and allow-popups-to-escape-sandbox,
or payment apps cannot open. See
embedded iframe.
A checkout grant page posts grant.created
and grant.funding on the same channel to the window that opened it or the frame that holds it.
listenForCheckoutGrantEvents delivers them only when the origin, the source window, the grant id
and your nonce all match; resolveEmbedTarget is what the page itself uses to find that window.
Errors
When the API answers with a non-2xx status or success: false, the SDK throws PayApiError
with statusCode, errorCode, responseObject, and fieldErrors / formErrors for validation
failures. Input it can check locally (a missing apiKey, more or fewer than one amount mode, an
invalid preselectedMethod, payout idempotencyKey or payoutId) throws a plain Error or
TypeError before any request. See
error handling.
