diff --git a/docs/docs.json b/docs/docs.json
index 0bdf102a9..4463fc077 100644
--- a/docs/docs.json
+++ b/docs/docs.json
@@ -136,7 +136,7 @@
"pages": ["ledger/introduction"]
},
{
- "dropdown": "@sei-js/x402",
+ "dropdown": "x402",
"description": "HTTP micro payments",
"icon": "credit-card",
"pages": [
diff --git a/docs/x402/clients/fetch.mdx b/docs/x402/clients/fetch.mdx
index 3238d8942..c063fcdc6 100644
--- a/docs/x402/clients/fetch.mdx
+++ b/docs/x402/clients/fetch.mdx
@@ -4,80 +4,160 @@ description: "Make paid HTTP requests with the native Fetch API and x402"
icon: "desktop"
---
-## Fetch Client Integration
+## Create Your First Fetch Client
Use x402 with the standard Fetch API to make paid HTTP requests. Perfect for both browser and Node.js environments.
-## Installation
-
-```bash
-npm install @sei-js/x402-fetch viem dotenv
-```
-
-## Basic Usage
-
-```typescript
-import { config } from "dotenv";
-import { Hex } from "viem";
-import { privateKeyToAccount } from "viem/accounts";
-import {
- wrapFetchWithPayment,
- decodeXPaymentResponse,
-} from "@sei-js/x402-fetch";
-
-config();
-
-const privateKey = process.env.PRIVATE_KEY as Hex;
-const baseURL = process.env.RESOURCE_SERVER_URL as string; // e.g. http://localhost:4021
-const endpointPath = process.env.ENDPOINT_PATH as string; // e.g. /weather
-const url = `${baseURL}${endpointPath}`;
-
-if (!baseURL || !privateKey || !endpointPath) {
- console.error("Missing required environment variables");
- process.exit(1);
-}
-
-// Create account from private key
-const account = privateKeyToAccount(privateKey);
-
-// Wrap fetch with payment handling
-const fetchWithPayment = wrapFetchWithPayment(fetch, account);
-
-// Make a paid request
-fetchWithPayment(url, {
- method: "GET",
-})
- .then(async (response) => {
- const body = await response.json();
- console.log("Response:", body);
-
- // Decode the payment response
- const paymentResponse = decodeXPaymentResponse(
- response.headers.get("x-payment-response")!
- );
- console.log("Payment details:", paymentResponse);
- })
- .catch((error) => {
- console.error("Error:", error.response?.data?.error);
- });
-```
-
-## Environment Setup
-
-Create a `.env` file with the required configuration:
-
-```env
-# Required: Your private key for making payments
-PRIVATE_KEY=0xYourPrivateKeyHere
-
-# Required: The server URL hosting the paid API
-RESOURCE_SERVER_URL=http://localhost:4021
-
-# Required: The endpoint path to access
-ENDPOINT_PATH=/weather
-```
-
-
- **Security Note**: Never commit private keys to version control. Use
- environment variables or secure key management services in production.
-
+
+
+ Create a new project and install the required packages:
+
+
+ ```bash pnpm
+ mkdir my-fetch-client && cd my-fetch-client
+ pnpm init -y
+ pnpm add x402-fetch @coinbase/x402 viem dotenv tsx
+ ```
+
+ ```bash yarn
+ mkdir my-fetch-client && cd my-fetch-client
+ yarn init -y
+ yarn add x402-fetch @coinbase/x402 viem dotenv tsx
+ ```
+
+ ```bash npm
+ mkdir my-fetch-client && cd my-fetch-client
+ npm init -y
+ npm install x402-fetch @coinbase/x402 viem dotenv tsx
+ ```
+
+
+
+ **Expected outcome:** Project directory created with x402 fetch client dependencies installed.
+
+
+
+
+ Create a `.env` file in your project root:
+
+ ```env
+ # Required: Your private key for making payments
+ PRIVATE_KEY=0xYourPrivateKeyHere
+
+ # Required: The server URL hosting the paid API
+ RESOURCE_SERVER_URL=http://localhost:4021
+
+ # Required: The endpoint path to access
+ ENDPOINT_PATH=/weather
+ ```
+
+
+ **Security Note**: Never commit private keys to version control. Use environment variables or secure key management services in production. For testing, you can use a dummy private key.
+
+
+
+ **Expected outcome:** Environment variables configured for your fetch client.
+
+
+
+
+ Create `fetch-client.ts` with the payment-enabled fetch implementation:
+
+ ```typescript
+ import { config } from "dotenv";
+ import { Hex } from "viem";
+ import { privateKeyToAccount } from "viem/accounts";
+ import {
+ wrapFetchWithPayment,
+ decodeXPaymentResponse,
+ } from "x402-fetch";
+
+ config();
+
+ const privateKey = process.env.PRIVATE_KEY as Hex;
+ const baseURL = process.env.RESOURCE_SERVER_URL as string;
+ const endpointPath = process.env.ENDPOINT_PATH as string;
+ const url = `${baseURL}${endpointPath}`;
+
+ if (!baseURL || !privateKey || !endpointPath) {
+ console.error("Missing required environment variables");
+ process.exit(1);
+ }
+
+ // Create account from private key
+ const account = privateKeyToAccount(privateKey);
+
+ // Wrap fetch with payment handling
+ const fetchWithPayment = wrapFetchWithPayment(fetch, account);
+
+ // Make a paid request
+ fetchWithPayment(url, {
+ method: "GET",
+ })
+ .then(async (response) => {
+ const body = await response.json();
+ console.log("Response:", body);
+
+ // Decode the payment response
+ const paymentResponse = decodeXPaymentResponse(
+ response.headers.get("x-payment-response")!
+ );
+ console.log("Payment details:", paymentResponse);
+ })
+ .catch((error) => {
+ console.error("Error:", error.message);
+ });
+ ```
+
+
+ **Expected outcome:** Fetch client code created with automatic payment handling.
+
+
+
+
+ Execute your fetch client to make paid requests:
+
+
+ ```bash pnpm
+ pnpx tsx fetch-client.ts
+ ```
+
+ ```bash yarn
+ yarn tsx fetch-client.ts
+ ```
+
+ ```bash npm
+ npx tsx fetch-client.ts
+ ```
+
+ ```bash direct
+ npx tsx fetch-client.ts
+ ```
+
+
+
+ **Expected outcome:** The client should automatically handle the 402 Payment Required response, make the payment, and receive the protected content.
+
+
+
+ Make sure your paid API server is running on the configured URL. The fetch client will automatically detect 402 responses, handle payment processing, and retry the request with payment proof.
+
+
+ Example successful output:
+ ```bash
+ Response: {
+ report: {
+ weather: 'sunny',
+ temperature: 70
+ }
+ }
+ Payment details: {
+ success: true,
+ transaction: '0x1234567890abcdef...',
+ network: 'sei-testnet',
+ payer: '0xYourWalletAddress...'
+ }
+ ```
+
+
+
diff --git a/docs/x402/facilitators/example.mdx b/docs/x402/facilitators/example.mdx
index d0de7e459..120e7a198 100644
--- a/docs/x402/facilitators/example.mdx
+++ b/docs/x402/facilitators/example.mdx
@@ -15,46 +15,71 @@ Before setting up a facilitator, ensure you have:
- **Sei wallet** with some SEI for gas fees
- **Private key** for your facilitator wallet (testnet recommended for development)
-## Quick Setup
-
-### 1. Install Dependencies
-
-```bash
-npm install @sei-js/x402 express dotenv
-```
-
-### 2. Environment Configuration
-
-Create a `.env` file:
-
-```env
-# Required: Your facilitator private key
-PRIVATE_KEY=0xYourPrivateKeyHere
-
-# Optional: Server port (defaults to 3000)
-PORT=3002
-```
-
-
- **Security Note**: Never commit private keys to version control. Use
- environment variables or secure key management services in production.
-
-
-### 3. Basic Facilitator Implementation
-
-Create `index.ts` with the following structure:
+## Create Your First Facilitator
+
+
+
+ Create a new project and install the required packages:
+
+
+ ```bash pnpm
+ mkdir my-facilitator && cd my-facilitator
+ pnpm init -y
+ pnpm add x402 @coinbase/x402 express dotenv tsx
+ ```
+
+ ```bash yarn
+ mkdir my-facilitator && cd my-facilitator
+ yarn init -y
+ yarn add x402 @coinbase/x402 express dotenv tsx
+ ```
+
+ ```bash npm
+ mkdir my-facilitator && cd my-facilitator
+ npm init -y
+ npm install x402 @coinbase/x402 express dotenv tsx
+ ```
+
+
+
+ **Expected outcome:** Project directory created with facilitator dependencies installed.
+
+
+
+
+ Create a `.env` file in your project root:
+
+ ```env
+ # Required: Your facilitator private key
+ PRIVATE_KEY=0xYourPrivateKeyHere
+
+ # Optional: Server port (defaults to 3000)
+ PORT=3000
+ ```
+
+
+ **Security Note**: Never commit private keys to version control. Use environment variables or secure key management services in production. For testing, you can use a dummy private key.
+
+
+
+ **Expected outcome:** Environment variables configured for your facilitator.
+
+
+
+
+ Create `index.ts` with the facilitator implementation:
```typescript
import { config } from "dotenv";
import express from "express";
-import { verify, settle } from "@sei-js/x402/facilitator";
+import { verify, settle } from "x402/facilitator";
import {
PaymentRequirementsSchema,
PaymentRequirements,
evm,
PaymentPayload,
PaymentPayloadSchema,
-} from "@sei-js/x402/types";
+} from "x402/types";
config();
@@ -133,16 +158,61 @@ app.listen(process.env.PORT || 3000, () => {
});
```
-### 4. Run Your Facilitator
-
-```bash
-npx tsx index.ts
-```
-
-
- Your facilitator server should start and display: `Server listening at
- http://localhost:3002`
-
+
+ **Expected outcome:** Facilitator server code created with verification and settlement endpoints.
+
+
+
+
+ Start your facilitator server:
+
+
+ ```bash pnpm
+ pnpx tsx index.ts
+ ```
+
+ ```bash yarn
+ yarn tsx index.ts
+ ```
+
+ ```bash npm
+ npx tsx index.ts
+ ```
+
+ ```bash direct
+ npx tsx index.ts
+ ```
+
+
+ In another terminal, test the supported schemes endpoint:
+
+ ```bash
+ curl http://localhost:3000/supported
+ ```
+
+
+ **Expected outcome:** Your facilitator server should start and display: `Server listening at http://localhost:3000`
+
+
+
+ The facilitator provides three endpoints: `/verify` for payment verification, `/settle` for transaction settlement, and `/supported` for listing supported payment schemes.
+
+
+ Example response from `/supported`:
+ ```json
+ {
+ "kinds": [
+ {
+ "x402Version": 1,
+ "scheme": "exact",
+ "network": "sei-testnet"
+ }
+ ]
+ }
+ ```
+
+
+
## API Endpoints
diff --git a/docs/x402/packages/x402-axios.mdx b/docs/x402/packages/x402-axios.mdx
index 24440e179..fdc3bf76b 100644
--- a/docs/x402/packages/x402-axios.mdx
+++ b/docs/x402/packages/x402-axios.mdx
@@ -1,16 +1,16 @@
---
-title: "@sei-js/x402-axios"
+title: "x402-axios"
description: Axios interceptor with automatic x402 payment handling
---
-# @sei-js/x402-axios
+# x402-axios
A utility package that extends Axios to automatically handle 402 Payment Required responses using the x402 payment protocol. This package enables seamless integration of payment functionality into your applications when making HTTP requests with Axios.
## Installation
```bash
-npm install @sei-js/x402-axios
+npm install x402-axios @coinbase/x402
```
## Quick Start
@@ -18,7 +18,7 @@ npm install @sei-js/x402-axios
```typescript
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
-import { withPaymentInterceptor } from "@sei-js/x402-axios";
+import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";
import { seiTestnet } from "viem/chains";
@@ -74,7 +74,7 @@ The same Axios instance with payment interceptors added. The interceptors will:
```typescript
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
-import { withPaymentInterceptor } from "@sei-js/x402-axios";
+import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";
import { seiTestnet } from "viem/chains";
diff --git a/docs/x402/packages/x402-express.mdx b/docs/x402/packages/x402-express.mdx
index 523d55a6c..2bdea30c6 100644
--- a/docs/x402/packages/x402-express.mdx
+++ b/docs/x402/packages/x402-express.mdx
@@ -1,23 +1,23 @@
---
-title: "@sei-js/x402-express"
+title: "x402-express"
description: Express middleware integration for x402 Payment Protocol
---
-# @sei-js/x402-express
+# x402-express
Express middleware integration for the x402 Payment Protocol. This package allows you to easily add paywall functionality to your Express.js applications using the x402 protocol.
## Installation
```bash
-npm install @sei-js/x402-express
+npm install x402-express @coinbase/x402
```
## Quick Start
```typescript
import express from "express";
-import { paymentMiddleware, Network } from "@sei-js/x402-express";
+import { paymentMiddleware, Network } from "x402-express";
const app = express();
@@ -27,7 +27,7 @@ app.use(paymentMiddleware(
{
"/protected-route": {
price: "$0.10",
- network: "sei",
+ network: "sei-testnet",
config: {
description: "Access to premium content",
}
@@ -110,8 +110,7 @@ type PaywallConfig = {
```typescript
import express from "express";
-import { paymentMiddleware } from "@sei-js/x402-express";
-import { facilitator } from "@sei-js/x402";
+import { paymentMiddleware } from "x402-express";
const app = express();
@@ -120,7 +119,7 @@ app.use(paymentMiddleware(
{
"/premium-api": {
price: "$0.05",
- network: "sei",
+ network: "sei-testnet",
config: {
description: "Premium API access",
maxTimeoutSeconds: 120,
@@ -128,14 +127,16 @@ app.use(paymentMiddleware(
},
"/data-feed": {
price: "$0.01",
- network: "sei",
+ network: "sei-testnet",
config: {
description: "Real-time data feed",
mimeType: "application/json",
}
}
},
- facilitator, // Use facilitator
+ {
+ url: "https://x402.org/facilitator" // Use test facilitator
+ },
{
appName: "My Premium API",
appLogo: "https://example.com/logo.png"
diff --git a/docs/x402/packages/x402-fetch.mdx b/docs/x402/packages/x402-fetch.mdx
index c1d907a25..4730be665 100644
--- a/docs/x402/packages/x402-fetch.mdx
+++ b/docs/x402/packages/x402-fetch.mdx
@@ -1,16 +1,16 @@
---
-title: "@sei-js/x402-fetch"
+title: "x402-fetch"
description: Fetch API wrapper with automatic x402 payment handling
---
-# @sei-js/x402-fetch
+# x402-fetch
A utility package that extends the native `fetch` API to automatically handle 402 Payment Required responses using the x402 payment protocol. This package enables seamless integration of payment functionality into your applications when making HTTP requests.
## Installation
```bash
-npm install @sei-js/x402-fetch
+npm install x402-fetch @coinbase/x402
```
## Quick Start
@@ -18,7 +18,7 @@ npm install @sei-js/x402-fetch
```typescript
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
-import { wrapFetchWithPayment } from "@sei-js/x402-fetch";
+import { wrapFetchWithPayment } from "x402-fetch";
import { seiTestnet } from "viem/chains";
// Create a wallet client
@@ -68,7 +68,7 @@ A wrapped fetch function that automatically handles 402 responses by:
import { config } from "dotenv";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
-import { wrapFetchWithPayment } from "@sei-js/x402-fetch";
+import { wrapFetchWithPayment } from "x402-fetch";
import { seiTestnet } from "viem/chains";
config();
diff --git a/docs/x402/packages/x402-hono.mdx b/docs/x402/packages/x402-hono.mdx
index 79656ae6a..3f5a7c1ea 100644
--- a/docs/x402/packages/x402-hono.mdx
+++ b/docs/x402/packages/x402-hono.mdx
@@ -1,23 +1,23 @@
---
-title: "@sei-js/x402-hono"
+title: "x402-hono"
description: Hono middleware integration for x402 Payment Protocol
---
-# @sei-js/x402-hono
+# x402-hono
Hono middleware integration for the x402 Payment Protocol. This package allows you to easily add paywall functionality to your Hono applications using the x402 protocol.
## Installation
```bash
-npm install @sei-js/x402-hono
+npm install x402-hono @coinbase/x402
```
## Quick Start
```typescript
import { Hono } from "hono";
-import { paymentMiddleware, Network } from "@sei-js/x402-hono";
+import { paymentMiddleware, Network } from "x402-hono";
const app = new Hono();
@@ -27,7 +27,7 @@ app.use(paymentMiddleware(
{
"/protected-route": {
price: "$0.10",
- network: "sei",
+ network: "sei-testnet",
config: {
description: "Access to premium content",
}
@@ -88,7 +88,7 @@ interface PaymentMiddlewareConfig {
```typescript
import { Hono } from "hono";
-import { paymentMiddleware } from "@sei-js/x402-hono";
+import { paymentMiddleware } from "x402-hono";
const app = new Hono();
@@ -97,7 +97,7 @@ app.use(paymentMiddleware(
{
"/api/premium": {
price: "$0.02",
- network: "sei",
+ network: "sei-testnet",
config: {
description: "Premium API endpoint",
maxTimeoutSeconds: 60,
diff --git a/docs/x402/packages/x402-next.mdx b/docs/x402/packages/x402-next.mdx
index 8be8d6eac..aea17c2fb 100644
--- a/docs/x402/packages/x402-next.mdx
+++ b/docs/x402/packages/x402-next.mdx
@@ -1,16 +1,16 @@
---
-title: "@sei-js/x402-next"
+title: "x402-next"
description: Next.js middleware integration for x402 Payment Protocol
---
-# @sei-js/x402-next
+# x402-next
Next.js middleware integration for the x402 Payment Protocol. This package allows you to easily add paywall functionality to your Next.js applications using the x402 protocol.
## Installation
```bash
-npm install @sei-js/x402-next
+npm install x402-next @coinbase/x402
```
## Quick Start
@@ -18,14 +18,14 @@ npm install @sei-js/x402-next
Create a middleware file in your Next.js project (e.g., `middleware.ts`):
```typescript
-import { paymentMiddleware } from '@sei-js/x402-next';
+import { paymentMiddleware } from 'x402-next';
export const middleware = paymentMiddleware(
"0xYourAddress",
{
'/protected': {
price: '$0.01',
- network: "sei",
+ network: "sei-testnet",
config: {
description: 'Access to protected content'
}
@@ -85,15 +85,15 @@ Create protected API routes in your Next.js application:
```typescript
// middleware.ts
-import { paymentMiddleware } from '@sei-js/x402-next';
-import { facilitator } from '@sei-js/x402';
+import { paymentMiddleware } from 'x402-next';
+// Use facilitator URL instead of import
export const middleware = paymentMiddleware(
"0xYourAddress",
{
'/api/premium': {
price: '$0.05',
- network: "sei",
+ network: "sei-testnet",
config: {
description: 'Premium API access',
maxTimeoutSeconds: 120,
@@ -101,7 +101,7 @@ export const middleware = paymentMiddleware(
},
'/api/data-feed': {
price: '$0.01',
- network: "sei",
+ network: "sei-testnet",
config: {
description: 'Real-time data feed',
mimeType: 'application/json',
@@ -144,14 +144,14 @@ The middleware works with both Pages Router and App Router:
```typescript
// middleware.ts
-import { paymentMiddleware } from '@sei-js/x402-next';
+import { paymentMiddleware } from 'x402-next';
export const middleware = paymentMiddleware(
"0xYourAddress",
{
'/dashboard': {
price: '$0.10',
- network: "sei",
+ network: "sei-testnet",
config: {
description: 'Premium dashboard access'
}
@@ -168,14 +168,14 @@ export const config = {
```typescript
// middleware.ts
-import { paymentMiddleware } from '@sei-js/x402-next';
+import { paymentMiddleware } from 'x402-next';
export const middleware = paymentMiddleware(
"0xYourAddress",
{
'/premium': {
price: '$0.05',
- network: "sei",
+ network: "sei-testnet",
config: {
description: 'Premium page access'
}
diff --git a/docs/x402/packages/x402.mdx b/docs/x402/packages/x402.mdx
index 32826d99b..18bfc6ada 100644
--- a/docs/x402/packages/x402.mdx
+++ b/docs/x402/packages/x402.mdx
@@ -1,21 +1,21 @@
---
-title: "@sei-js/x402"
+title: "x402"
description: Core TypeScript implementation of the x402 Payment Protocol
---
-# @sei-js/x402
+# x402
Core TypeScript implementation of the x402 Payment Protocol. This package provides the foundational types, schemas, and utilities that power all x402 integrations.
## Installation
```bash
-npm install @sei-js/x402
+npm install x402 @coinbase/x402
```
## Overview
-The @sei-js/x402 package provides the core building blocks for implementing the x402 Payment Protocol in TypeScript. It's designed to be used by:
+The x402 package provides the core building blocks for implementing the x402 Payment Protocol in TypeScript. It's designed to be used by:
- Middleware implementations (Express, Hono, Next.js)
- Client-side payment handlers (fetch wrapper)
@@ -26,11 +26,11 @@ The @sei-js/x402 package provides the core building blocks for implementing the
This core package is used by the following integration packages:
-- `@sei-js/x402-express`: Express.js middleware
-- `@sei-js/x402-hono`: Hono middleware
-- `@sei-js/x402-next`: Next.js middleware
-- `@sei-js/x402-fetch`: Fetch API wrapper
-- `@sei-js/x402-axios`: Axios interceptor
+- `x402-express`: Express.js middleware
+- `x402-hono`: Hono middleware
+- `x402-next`: Next.js middleware
+- `x402-fetch`: Fetch API wrapper
+- `x402-axios`: Axios interceptor
## Manual Server Integration
@@ -43,7 +43,7 @@ If you're not using one of our server middleware packages, you can implement the
## Manual Client Integration
-If you're not using our `@sei-js/x402-fetch` or `@sei-js/x402-axios` packages, you can manually integrate the x402 protocol in your client application. Here's how:
+If you're not using our `x402-fetch` or `x402-axios` packages, you can manually integrate the x402 protocol in your client application. Here's how:
1. Make a request to a x402-protected endpoint. The server will respond with a 402 status code and a JSON object containing:
- `x402Version`: The version of the x402 protocol being used
@@ -55,4 +55,4 @@ If you're not using our `@sei-js/x402-fetch` or `@sei-js/x402-axios` packages, y
4. Retry your network call with:
- The payment header assigned to the `X-PAYMENT` field
- - The `Access-Control-Expose-Headers` field set to `"X-PAYMENT-RESPONSE"` to receive the server's transaction response
\ No newline at end of file
+ - The `Access-Control-Expose-Headers` field set to `"X-PAYMENT-RESPONSE"` to receive the server's transaction response
diff --git a/docs/x402/quickstart.mdx b/docs/x402/quickstart.mdx
index 5c2e8741f..658c3dcb7 100644
--- a/docs/x402/quickstart.mdx
+++ b/docs/x402/quickstart.mdx
@@ -1,6 +1,6 @@
---
title: Quickstart Guide
-description: Build your first paid API on Sei in under 10 minutes by using @sei-js/x402
+description: Build your first paid API on Sei in under 10 minutes by using x402
icon: "bolt"
---
@@ -49,22 +49,22 @@ Before you begin, ensure you have the following:
Create a new project and install the required packages:
- ```bash npm
+ ```bash pnpm
mkdir my-paid-api && cd my-paid-api
- npm init -y
- npm install @sei-js/x402-express express dotenv
+ pnpm init -y
+ pnpm add x402-express @coinbase/x402 express dotenv
```
```bash yarn
mkdir my-paid-api && cd my-paid-api
yarn init -y
- yarn add @sei-js/x402-express express dotenv
+ yarn add x402-express @coinbase/x402 express dotenv
```
- ```bash pnpm
+ ```bash npm
mkdir my-paid-api && cd my-paid-api
- pnpm init -y
- pnpm add @sei-js/x402-express express dotenv
+ npm init -y
+ npm install x402-express @coinbase/x402 express dotenv
```
@@ -73,23 +73,65 @@ Before you begin, ensure you have the following:
+
+ Update your `package.json` to enable ES modules:
+
+ ```json
+ {
+ "name": "my-paid-api",
+ "version": "1.0.0",
+ "type": "module",
+ "main": "server.js",
+ "scripts": {
+ "start": "node server.js"
+ },
+ "dependencies": {
+ "x402-express": "^0.6.0",
+ "@coinbase/x402": "^0.6.0",
+ "express": "^5.1.0",
+ "dotenv": "^17.2.2"
+ }
+ }
+ ```
+
+
+ **Expected outcome:** Package.json configured for ES modules.
+
+
+
+
+ Create a `.env` file in your project root:
+
+ ```env
+ FACILITATOR_URL=http://localhost:3000/facilitator
+ ADDRESS=0x1234567890123456789012345678901234567890
+ ```
+
+
+ Replace `ADDRESS` with your testnet Sei wallet address. The `FACILITATOR_URL` should point to your facilitator
+ service, but having a valid one is not required for this tutorial. If you want to
+ test with a facilitator, you can run one locally. See the [Facilitator Example](/facilitators/example)
+
+
+
+ **Expected outcome:** Environment variables configured for your API.
+
+
+
-
- **Facilitator Might be Required**: The code references `facilitatorUrl` environment variable that need to be configured. You'll need to set up a facilitator to process payments or use a public facilitator. See the [Facilitator Example](/facilitators/example) here.
-
Create `server.js` with a protected API endpoint:
```javascript
import {config} from "dotenv";
import express from "express";
- import {paymentMiddleware, Resource} from "@sei-js/x402-express";
+ import {paymentMiddleware} from "x402-express";
config();
- const facilitatorUrl = process.env.FACILITATOR_URL as Resource;
- const payTo = process.env.ADDRESS as `0x${string}`;
+ const facilitatorUrl = process.env.FACILITATOR_URL;
+ const payTo = process.env.ADDRESS;
if (!facilitatorUrl || !payTo) {
- console.error("Missing required environment variables");
+ console.error("Missing required environment variables FACILITATOR_URL and ADDRESS");
process.exit(1);
}
@@ -131,32 +173,71 @@ Before you begin, ensure you have the following:
-
- Start your server and test the payment requirement:
+
+ Start your server:
- ```bash
- node server.js
- ```
+
+ ```bash pnpm
+ pnpm start
+ ```
- In another terminal, try accessing the protected endpoint:
+ ```bash yarn
+ yarn start
+ ```
+
+ ```bash npm
+ npm start
+ ```
+
+ ```bash direct
+ node server.js
+ ```
+
+
+ In another terminal, test the protected endpoint:
```bash
curl http://localhost:4021/weather
```
- **Expected outcome:** You should receive a `402 Payment Required` response with payment details.
+ **Expected outcome:** You should receive a `402 Payment Required` response with payment details, or an error if the facilitator is not running.
- Example response:
+
+ If you get a connection error, make sure your facilitator service is running at the configured URL, or set up a local facilitator using the [Facilitator Example](/x402/facilitators/example).
+
+
+ Example successful 402 response:
```json
- {
- "error": "Payment Required",
- "amount": "0.01",
- "currency": "SEI",
- "recipient": "YOUR_WALLET_ADDRESS",
- "network": "sei-testnet"
- }
+ {
+ "x402Version": 1,
+ "error": "X-PAYMENT header is required",
+ "accepts": [
+ {
+ "scheme": "exact",
+ "network": "sei-testnet",
+ "maxAmountRequired": "1000",
+ "resource": "http://localhost:4021/weather",
+ "description": "",
+ "mimeType": "",
+ "payTo": "0x1234567890123456789012345678901234567890",
+ "maxTimeoutSeconds": 60,
+ "asset": "0x4fCF1784B31630811181f670Aea7A7bEF803eaED",
+ "outputSchema": {
+ "input": {
+ "type": "http",
+ "method": "GET",
+ "discoverable": true
+ }
+ },
+ "extra": {
+ "name": "USDC",
+ "version": "2"
+ }
+ }
+ ]
+ }
```