Skip to main content

Getting started with Express

Start accepting x402 payments in your Express server in 2 minutes.
You can find the full code for this example here.

Step 1: Create a new server from the starter template

Use your favorite package manager:
npm (npx)
pnpm
bun
The starter mirrors the upstream example and bootstraps a ready-to-run Express server.

Step 2: Set your environment variables

Open your generated project’s .env and set:
The starter uses @payai/facilitator which automatically connects to the PayAI facilitator — no URL configuration needed.

Step 3: Preview the server code

This is the index.ts the starter generates. It loads your env, applies the x402 payment middleware, defines example routes, and logs the server URL.

Step 4: Run the server

Your server is now accepting 402 payments!

Step 5: Test the server

You can test payments against your server locally by following the fetch example or the axios example.

Going to production

The starter works on the free tier out of the box — no API keys required. When you’re ready for production, create a merchant account at merchant.payai.network, get your API keys, and add them to your .env:
The @payai/facilitator package automatically detects these environment variables and authenticates your requests to the facilitator. See Facilitator Pricing for tier details and Facilitator Authentication for the full protocol reference.

x402 reference

For a deeper dive into message shapes, headers, verification and settlement responses, see the x402 Reference.

Need help?

Join our Community

Have questions or want to connect with other developers? Join our Discord server.