---
title: Express.js Integration
description: Start accepting x402 payments in your Express server in 2 minutes
keywords:
  [x402, Express, Node.js, TypeScript, blockchain payments, API monetization]
last_updated: 2026-08-19
---

# Getting Started with Express

Start accepting x402 payments in your Express server in 2 minutes.

:::info Example Code
You can find the full code for this example on [GitHub](https://github.com/BlockEdenHQ/x402-examples/tree/main/typescript/servers/express).
:::

## Step 1: Install Dependencies

Install the required packages for your Express server:

```bash
npm install x402 express dotenv
```

Or with other package managers:

```bash
# pnpm
pnpm add x402 express dotenv

# yarn
yarn add x402 express dotenv

# bun
bun add x402 express dotenv
```

## Step 2: Set Your Environment Variables

Open your generated project's `.env` and set:

- `FACILITATOR_URL`: Facilitator base URL (defaults to: `https://x402.bex.co`)
- `NETWORK`: Network to use for the facilitator (default: `sui`)
- `ADDRESS`: Wallet public address to receive payments to
- `BEX_API_KEY`: Your bex.co API key from [dashboard](https://bex.co/dash/)

```env
FACILITATOR_URL=https://x402.bex.co
NETWORK=sui
ADDRESS=0x... # wallet public address you want to receive payments to
BEX_API_KEY=your_api_key_here
```

:::tip Network Options
bex.co supports multiple networks:

- `sui` (recommended for fastest settlement)
- `ethereum`
- `base`
- `polygon`
- `avalanche`
  :::

## Step 3: Create Your Express Server

Create an `index.ts` file with the following code:

```typescript
import { config } from "dotenv";
import express from "express";
import { paymentMiddleware, Network, Resource } from "x402-express";

config();

const facilitatorUrl = process.env.FACILITATOR_URL as Resource;
const payTo = process.env.ADDRESS as `0x${string}`;
const network = process.env.NETWORK as Network;
const apiKey = process.env.BEX_API_KEY;

if (!facilitatorUrl || !payTo || !apiKey || !network) {
  console.error("Missing required environment variables");
  process.exit(1);
}

const app = express();

app.use(
  paymentMiddleware(
    payTo,
    {
      "GET /weather": {
        // USDC amount in dollars
        price: "$0.001",
        network,
      },
      "/premium/*": {
        // Define atomic amounts in any supported token
        price: {
          amount: "100000",
          asset: {
            address: "0xabc",
            decimals: 18,
            name: "USDC",
          },
        },
        network,
      },
    },
    {
      url: facilitatorUrl,
      apiKey: apiKey,
    },
  ),
);

app.get("/weather", (req, res) => {
  res.send({
    report: {
      weather: "sunny",
      temperature: 70,
    },
  });
});

app.get("/premium/content", (req, res) => {
  res.send({
    content: "This is premium content",
  });
});

app.listen(4021, () => {
  console.log(`Server listening at http://localhost:4021`);
});
```

## Step 4: Run the Server

```bash
npx tsx index.ts
```

Or add a script to your `package.json`:

```json
{
  "scripts": {
    "dev": "tsx watch index.ts",
    "start": "node dist/index.js"
  }
}
```

Then run:

```bash
npm run dev
```

<div style={{padding: '1rem', background: '#d1fae5', borderRadius: '0.5rem', color: '#065f46', marginTop: '1rem', marginBottom: '1rem'}}>
  <ion-icon name="checkmark-circle" style={{fontSize: '1.5rem', verticalAlign: 'middle', marginRight: '0.5rem'}}></ion-icon>
  <strong>Your server is now accepting 402 payments!</strong>
</div>

## Step 5: Test the Server

You can test payments against your server locally using HTTP clients like `curl`, Postman, or by building a client application.

:::note Coming Soon
Client implementation guides for fetch API and axios will be available soon.
:::

## Payment Configuration Options

The `paymentMiddleware` accepts flexible payment configurations:

### Simple Dollar Amount

```typescript
"GET /weather": {
  price: "$0.001",
  network: "sui",
}
```

### Atomic Token Amount

```typescript
"/premium/*": {
  price: {
    amount: "100000",
    asset: {
      address: "0x...",
      decimals: 6,
      name: "USDC",
    },
  },
  network: "sui",
}
```

### Route Patterns

You can use wildcards and specific HTTP methods:

```typescript
{
  "GET /weather": { /* ... */ },      // Exact GET route
  "/premium/*": { /* ... */ },        // Any method, wildcard path
  "POST /api/analyze": { /* ... */ }, // Exact POST route
}
```

## Advanced Features

### Custom Payment Verification

```typescript
app.use(
  paymentMiddleware(payTo, paymentConfig, {
    url: facilitatorUrl,
    apiKey: apiKey,
    onPaymentVerified: (req, paymentData) => {
      console.log("Payment verified:", paymentData.txHash);
      // Custom logging or analytics
    },
  }),
);
```

### Error Handling

```typescript
app.use((err, req, res, next) => {
  if (err.name === "X402PaymentError") {
    res.status(402).json({
      error: "Payment required",
      details: err.message,
    });
  } else {
    next(err);
  }
});
```

## Production Deployment

Before deploying to production:

1.  Switch to production facilitator endpoint
2.  Use production network (e.g., `sui` instead of `sui-testnet`)
3.  Enable HTTPS for your server
4.  Set up proper error logging
5.  Configure CORS if serving cross-origin clients

```typescript
// Production configuration
const app = express();

// Enable CORS
app.use(
  cors({
    origin: process.env.ALLOWED_ORIGINS?.split(","),
    credentials: true,
  }),
);

// Use production network
app.use(
  paymentMiddleware(
    payTo,
    {
      "GET /weather": {
        price: "$0.01",
        network: "sui", // Production Sui network
      },
    },
    {
      url: "https://x402.bex.co",
      apiKey: process.env.BEX_API_KEY,
    },
  ),
);
```

## Troubleshooting

### Common Issues

**Payment verification fails**

- Verify your `BEX_API_KEY` is correct
- Check that the wallet address format matches the network
- Ensure the facilitator URL is accessible

**CORS errors**

- Add CORS middleware before payment middleware
- Configure allowed origins properly

**Network mismatch**

- Ensure client and server use the same network
- Check that the token address is valid for the network

## Next Steps

- <ion-icon name="arrow-forward-circle"></ion-icon> [Implement a crypto paywall](./implement-a-crypto-paywall-with-x402.md)
- <ion-icon name="arrow-forward-circle"></ion-icon> [View the merchant introduction](./x402-for-merchants.md)
- <ion-icon name="arrow-forward-circle"></ion-icon> [Read the Quick Start Guide](./quickstart.md)

## Need Help?

<div style={{padding: '1.5rem', background: '#f3f4f6', borderRadius: '0.5rem', marginTop: '2rem'}}>
  <div style={{display: 'flex', alignItems: 'flex-start', marginBottom: '1rem'}}>
    <ion-icon name="people" style={{fontSize: '1.5rem', marginRight: '0.5rem', color: '#374151'}}></ion-icon>
    <div>
      <h4 style={{margin: 0, fontSize: '1.125rem', fontWeight: 600, color: '#111827'}}>Join Our Community</h4>
      <p style={{margin: '0.25rem 0 0 0', color: '#6b7280'}}>Have questions or want to connect with other developers?</p>
    </div>
  </div>
  <a
    href="https://discord.gg/4Yfvs2HWey"
    target="_blank"
    rel="noopener noreferrer"
    style={{
      display: 'inline-flex',
      alignItems: 'center',
      gap: '0.5rem',
      padding: '0.625rem 1.25rem',
      background: '#5865F2',
      color: 'white',
      borderRadius: '0.5rem',
      textDecoration: 'none',
      fontWeight: 600,
      fontSize: '0.9375rem',
      transition: 'background 0.2s',
      border: 'none'
    }}
    onMouseOver={(e) => e.currentTarget.style.background = '#4752C4'}
    onMouseOut={(e) => e.currentTarget.style.background = '#5865F2'}
  >
    <ion-icon name="logo-discord" style={{fontSize: '1.25rem'}}></ion-icon>
    <span>Join Discord</span>
  </a>
</div>
