Prerequisites
Before you begin, ensure you have the following:Node.js & Package Management
Node.js & Package Management
Required for running x402 applications:
- Node.js (v18 or higher) - Required for running x402 packages
- npm, yarn, or pnpm - For package management
Verify your installation:
node --version
should output v18.0.0 or higherSei Wallet & Test Funds
Sei Wallet & Test Funds
Required for testing payments:
- MetaMask or Compass Wallet - For signing transactions
- Sei Testnet configured - Add Sei testnet to your wallet
- Test SEI tokens - Get from Sei Faucet
Sei testnet details: Chain ID
1328
, RPC https://evm-rpc-testnet.sei-apis.com
Environment Variables
Environment Variables
Required configuration for your paid API:
FACILITATOR_URL
- URL of your x402 facilitator serviceADDRESS
- Your Sei wallet address (0x format) to receive payments
Keep your environment variables secure and never commit them to version control
Create Your First Paid API
1
Install x402 packages
Create a new project and install the required packages:
Expected outcome: Project directory created with x402 dependencies installed.
2
Configure ES Modules
Update your
package.json
to enable ES modules:Expected outcome: Package.json configured for ES modules.
3
Create environment configuration
Create a
.env
file in your project root: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 ExampleExpected outcome: Environment variables configured for your API.
4
Create a simple paid API server
Create
server.js
with a protected API endpoint:Expected outcome: Express server configured with x402 payment middleware.
5
Start and test your paid API
Start your server:In another terminal, test the protected endpoint:Example successful 402 response:
Expected outcome: You should receive a
402 Payment Required
response with payment details, or an error if the facilitator is not running.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.