> ## Documentation Index
> Fetch the complete documentation index at: https://docs.payai.network/llms.txt
> Use this file to discover all available pages before exploring further.

> Add x402 payment middleware to a TypeScript Hono server using the PayAI facilitator.

# Hono

## Getting started with Hono

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

<Note>You can find the full code for this example [here](https://github.com/x402-foundation/x402/tree/main/examples/typescript/servers/hono).</Note>

### Step 1: Create a project and install dependencies

Use your favorite package manager:

##### npm

```bash theme={null}
mkdir my-server && cd my-server
npm init -y && npm pkg set type=module
npm install hono @hono/node-server dotenv @payai/facilitator @x402/core @x402/evm @x402/extensions @x402/hono @x402/svm
npm install -D typescript tsx
```

##### pnpm

```bash theme={null}
mkdir my-server && cd my-server
pnpm init && pnpm pkg set type=module
pnpm add hono @hono/node-server dotenv @payai/facilitator @x402/core @x402/evm @x402/extensions @x402/hono @x402/svm
pnpm add -D typescript tsx
```

##### bun

```bash theme={null}
mkdir my-server && cd my-server
bun init -y
bun add hono @hono/node-server dotenv @payai/facilitator @x402/core @x402/evm @x402/extensions @x402/hono @x402/svm
bun add -d typescript
```

This is the same dependency set as the [upstream Hono example](https://github.com/x402-foundation/x402/tree/main/examples/typescript/servers/hono).

### Step 2: Set your environment variables

Create a `.env` file in the project root and set:

```env theme={null}
EVM_ADDRESS=0x...   # EVM wallet address to receive payments
SVM_ADDRESS=...    # Solana wallet address to receive payments
```

<Tip>`@payai/facilitator` automatically connects to the PayAI facilitator — no URL configuration needed.</Tip>

### Step 3: Preview the server code

Create `index.ts`: It loads your env, applies the x402 payment middleware, defines example routes, and logs the server URL.

```ts theme={null}
import { config } from "dotenv";
import { paymentMiddleware, x402ResourceServer } from "@x402/hono";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { ExactSvmScheme } from "@x402/svm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";
import { declareDiscoveryExtension } from "@x402/extensions/bazaar";
import { facilitator } from "@payai/facilitator";
import { Hono } from "hono";
import { serve } from "@hono/node-server";

config();

const evmAddress = process.env.EVM_ADDRESS as `0x${string}`;
const svmAddress = process.env.SVM_ADDRESS;
if (!evmAddress || !svmAddress) {
  console.error("Missing required environment variables");
  process.exit(1);
}

const facilitatorClient = new HTTPFacilitatorClient(facilitator);

const app = new Hono();

app.use(
  paymentMiddleware(
    {
      "GET /weather": {
        accepts: [
          {
            scheme: "exact",
            price: "$0.001",
            network: "eip155:84532",
            payTo: evmAddress,
          },
          {
            scheme: "exact",
            price: "$0.001",
            network: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
            payTo: svmAddress,
          },
        ],
        description: "Weather data",
        mimeType: "application/json",
        serviceName: "Weather API",
        tags: ["weather", "api"],
        extensions: {
          ...declareDiscoveryExtension({
            output: {
              example: {
                report: {
                  weather: "sunny",
                  temperature: 70,
                },
              },
            },
          }),
        },
      },
    },
    new x402ResourceServer(facilitatorClient)
      .register("eip155:84532", new ExactEvmScheme())
      .register("solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", new ExactSvmScheme()),
  ),
);

app.get("/weather", c => {
  return c.json({
    report: {
      weather: "sunny",
      temperature: 70,
    },
  });
});

serve({
  fetch: app.fetch,
  port: 4021,
});

console.log(`Server listening at http://localhost:4021`);
```

### Step 4: Run the server

```bash theme={null}
npx tsx index.ts
```

<Check>
  Your server is now accepting x402 payments!
</Check>

### Step 5: Test the server

You can test payments against your server locally by:

* Following the [fetch example](/x402/clients/typescript/fetch) or the [axios example](/x402/clients/typescript/axios)
* Creating a paywall using `@x402/paywall` for EVM chains or `x402-solana-react` for Solana

## Going to production

The examples above use testnets. For Mainnet, update the route's network and any explicit scheme registrations together, use the matching asset and funded buyer, and verify the paid path. The [Solana Mainnet example](/x402/solana-mainnet-express) provides a complete tested configuration; adding credentials alone does not switch networks.

This setup can serve ordinary exact mainnet payments in production within the [free allowance](/x402/facilitators/pricing) — no API key or portal signup is required. Production readiness still includes normal controls such as secret management, monitoring, and failure handling.

When you need paid capacity beyond the allowance, an autonomous agent can [buy credits and receive an API key over x402](/x402/facilitators/agent-api-keys); PayAI creates a wallet-owned agent account programmatically. You can also use the optional [merchant dashboard](https://merchant.payai.network). Add the resulting credentials to your `.env`:

```env theme={null}
PAYAI_API_KEY_ID=your-key-id
PAYAI_API_KEY_SECRET=your-key-secret
```

The `@payai/facilitator` package automatically detects these environment variables and authenticates your requests to the facilitator. See [Facilitator Pricing](/x402/facilitators/pricing) for tier details and [Facilitator Authentication](/x402/facilitators/authentication) for the full protocol reference.

## x402 reference

For a deeper dive into message shapes, headers, verification and settlement responses, see the [x402 Reference](/x402/reference).

## Need help?

<Card title="Join our Community" icon="discord" href="https://discord.gg/eWJRwMpebQ">
  Have questions or want to connect with other developers? Join our Discord server.
</Card>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.