Skip to main content
This guide covers building a complete payment backend with Express.js. You will create payment endpoints, handle webhooks, and implement error handling.

Prerequisites

  • Node.js 18+
  • Express.js 4.x or 5.x
  • A ZendFi account with test API keys

Project Setup

1

Create the project

Or use the CLI scaffolding tool:
2

Configure TypeScript

tsconfig.json
3

Set up environment variables

.env
4

Create the ZendFi client

src/lib/zendfi.ts

Application Structure

Entry Point

src/index.ts
Register the webhook route before express.json(). The webhook handler needs the raw request body to verify the HMAC signature. If Express parses the body first, verification will fail.

Payment Routes

src/routes/payments.ts

Webhook Handler

src/routes/webhooks.ts

Error Handler

src/middleware/error-handler.ts

Idempotency

For payment creation, pass an idempotency key to prevent duplicate charges:
The client passes Idempotency-Key in the request header. If the same key is sent again within 24 hours, ZendFi returns the original response without creating a new payment.

Testing

1

Start the server

2

Start webhook forwarding

3

Create a payment

4

Check the response

Production Checklist

Before going live, verify:
  • Switch to live API key (zfi_live_)
  • Webhook secret is set in production environment
  • Webhook endpoint is registered in ZendFi Dashboard
  • Error handling covers all SDK error types
  • Idempotency keys are used for payment creation
  • CORS is restricted to your frontend domain
  • Rate limiting is configured on your Express server
  • HTTPS is enabled (required for webhook delivery)