Skip to main content
This guide walks through a full ZendFi integration in a Next.js 14+ application using the App Router. By the end, you will have server-side payment creation, a checkout redirect flow, webhook handling, and order confirmation.

Prerequisites

  • Next.js 14+ with App Router
  • Node.js 18+
  • A ZendFi account with test API keys

Project Setup

1

Install the SDK

Or use the CLI to set everything up automatically:
2

Configure environment variables

Create a .env.local file in your project root:
.env.local
Next.js automatically loads .env.local and keeps these variables server-side only (no NEXT_PUBLIC_ prefix means they are never exposed to the browser).
3

Create the ZendFi client

lib/zendfi.ts
The client auto-detects ZENDFI_API_KEY from your environment. No configuration needed.

Create a Payment

Build a Server Action that creates a payment and redirects to checkout:
app/actions/checkout.ts
Use it in a client component:
app/checkout/page.tsx

Handle Webhooks

Create a webhook handler route to process payment events:
app/api/webhooks/zendfi/route.ts
Do not add export const runtime = 'edge' to this route. The webhook handler needs the Node.js runtime for crypto operations used in signature verification.

Order Confirmation Page

Create a page to show after successful payment:
app/order/[paymentId]/page.tsx
If you prefer a no-code approach, create a payment link and share it:
app/actions/payment-link.ts

Embedded Checkout

For an inline checkout experience without redirects, use the Embedded Checkout component:
app/checkout/embedded/page.tsx
See the Embedded Checkout guide for the full implementation.

Middleware (Optional)

Protect your checkout routes with Next.js middleware:
middleware.ts

Architecture Overview

Testing

Use your test API key and the CLI to verify everything works: