Skip to content
Cipay
Esc
navigateopen⌘Jpreview
On this page

Get started

Create a sandbox integration with a server-held API key and a same-origin buyer endpoint.

This guide lists a published product, creates gateway-hosted checkout, and shows where payment confirmation belongs. You need a Cipay sandbox API key, a storefront locator, and one published sandbox product with a price.

Install the SDK

bun add @cipay/client-sdk
pnpm add @cipay/client-sdk
npm install @cipay/client-sdk
yarn add @cipay/client-sdk

Connect your backend

Cipay ships two package entry points. Import each one only in its intended runtime:

Entry point Runs in Use it for
@cipay/client-sdk/api Your trusted server API keys, catalog management, checkout orchestration, invoices, and webhook verification.
@cipay/client-sdk/react Your React client Buyer hooks and components that call your same-origin /api/cipay route.

The /api suffix above is a server SDK import. /api/cipay is the HTTP route you mount for browser-safe operations. The /react entry point calls that route and never receives your Cipay API key.

What is the storefront locator?

The locator is a public, non-secret identifier for the storefront whose published products and checkout settings you want to use. A sandbox locator looks like sandbox.<merchant-id>.<public-slug>.

Copy it from Cipay Platform → Settings → Developer → Storefront integration. Follow the three screenshot steps if you have not configured it yet.

  • The API key authenticates your server.
  • The locator selects the Cipay environment, merchant, and public storefront.
  • Cipay uses that selection to load the published catalog and approved checkout origins.

Keep it in server configuration so the browser cannot switch to another merchant or environment.

Create the server client

Keep the API key in a server-only environment variable. Sandbox is the default mode.

import { createCipayClient } from "@cipay/client-sdk/api";

export const cipay = createCipayClient({
  apiKey: process.env.CIPAY_SANDBOX_API_KEY!,
  mode: "sandbox"
});

Create the buyer handler

The handler exposes fixed catalog, checkout, subscription, order, and invoice operations. Resolve customer identity from your verified session and stored Cipay customer mapping.

readVerifiedSession below is your application-owned auth adapter, not a Cipay export. The framework guides show where to define it and how to connect it to your server auth.

import { createCipayBuyerHandler } from "@cipay/client-sdk/api";
import { cipay } from "./cipay";
import { readVerifiedSession } from "./cipay-session";

export const cipayBuyerHandler = createCipayBuyerHandler({
  client: cipay,
  storefront: {
    locator: process.env.CIPAY_STOREFRONT_LOCATOR!
  },
  resolveCustomer: async (request) => {
    const session = await readVerifiedSession(request);
    return session
      ? { subject: session.userId, customerId: session.cipayCustomerId }
      : null;
  }
});

The handler derives your application origin from each incoming request, rejects cross-origin browser writes, and sends that origin to Cipay. Cipay then checks it against the approved origins configured for the selected storefront.

Mount the handler

Choose your framework. Each guide adds authentication and rate limiting before the same Request → Response handler.

Connect React

CipayProvider receives your same-origin endpoint and a non-secret cache partition. It never receives an API key.

"use client";

import { CipayProvider } from "@cipay/client-sdk/react";

export function Providers({ userId, children }: {
  userId?: string;
  children: React.ReactNode;
}) {
  return (
    <CipayProvider endpoint="/api/cipay" sessionKey={userId}>
      {children}
    </CipayProvider>
  );
}
"use client";

import { ProductsList } from "@cipay/client-sdk/react";

export function Products() {
  return (
    <ProductsList
      heading="Available plans"
      input={{ pageSize: 12 }}
      className="products-grid"
      empty={() => <p>No published products yet.</p>}
    />
  );
}

Confirm the result

Create hosted checkout

Select a published offer and create checkout. Redirect only to the returned checkoutUrl.

Verify the webhook

Verify the exact raw body, timestamp, signature, and event ID before parsing or processing it.

Persist before side effects

Insert the event ID and enqueue durable work in one transaction so retries cannot create duplicate effects.

Read the order

After the verified event, retrieve the owned order or invoice through the authenticated buyer endpoint.