Build and demo paywalled experiences on Hedera testnet in minutes. This fork is in alpha.
This repo packages everything you need to stand up the x402 payment flow on Hedera: a facilitator, a token-gated resource server, and a sample client that pays for access.
- Node.js 18+ and
pnpm10.7+ (corepack enable pnpmif you need to install it) gitand a POSIX shell (bash/zsh)- Hedera ECDSA testnet account funded with HBAR and associated with USDC
0.0.429274 - Basic familiarity with editing
.envfiles on your machine
Need help preparing the Hedera account? Jump to Prepare your Hedera testnet wallet.
git clone https://github.com/hedera-dev/x402-hedera.git
cd x402-hedera
chmod +x setup.sh
./setup.shThen open three terminals, all from the repo root:
- Facilitator –
cd examples/typescript/facilitator && cp .env-local .env→ fillHEDERA_ACCOUNT_ID+HEDERA_PRIVATE_KEY→pnpm dev - Resource server –
cd examples/typescript/servers/express && cp .env-local .env→ setADDRESSto the account that receives funds →pnpm dev - Client –
cd examples/typescript/clients/axios && cp .env-local .env→ setPRIVATE_KEYfor the paying account →pnpm dev
You should see successful payments logged in all three terminals as the client purchases /hedera-usdc and /hedera-native.
- Create or log into the Hedera Developer Portal and generate an ECDSA testnet account.
- Fund the account with testnet HBAR from the portal faucet.
- Associate USDC token
0.0.429274with the wallet. Most wallets have an “Associate Token” action where you paste the token ID, or you can run this Hedera Portal Script. - Visit the Circle testnet faucet, choose Hedera Testnet, enter your account ID, and request USDC. The facilitator will spend from this account when verifying payments.
Keep both the account ID (0.0.x) and the private key handy; you will need them in the next steps.
The setup.sh script installs and builds the monorepo packages and the TypeScript examples.
chmod +x setup.sh
./setup.shIf you prefer to do it manually:
cd typescript && pnpm install && pnpm build
cd ../examples/typescript && pnpm install && pnpm buildTerminal 1:
cd examples/typescript/facilitator
cp .env-local .envUpdate .env with your fee payer credentials:
HEDERA_ACCOUNT_ID– account that signs and pays feesHEDERA_PRIVATE_KEY– private key for the account (ECDSA)- Optional
PORT(defaults to3002)
OR
Use our hosted testnet facilitator https://x402-hedera-production.up.railway.app/
You will need to update the .env in the next step
Start the facilitator:
pnpm devYou should see Server listening on http://localhost:3002.
Terminal 2:
cd examples/typescript/servers/express
cp .env-local .envConfigure .env:
FACILITATOR_URL– typicallyhttp://localhost:3002NETWORK– leave ashedera-testnetADDRESS– Hedera account that will receive the payment (can match the facilitator account or be different)
Start the server:
pnpm devThe server exposes:
GET /weather– free endpointGET /hedera-usdc– pay $0.001 USDC on HederaGET /hedera-native– pay 0.5 HBAR natively
Terminal 3:
cd examples/typescript/clients/axios
cp .env-local .envSet RESOURCE_SERVER_URL (default http://localhost:4021), PRIVATE_KEY for the paying account, and leave ENDPOINT_PATH as /hedera-native to exercise native HBAR payments. You can switch it to /hedera-usdc to test USDC payments instead.
Run the client:
pnpm devThe script will:
- Request the protected endpoint
- Receive a
402 Payment Required - Pay through the facilitator
- Retry and receive the gated response
Expect to see logs showing the transaction hash and the JSON payload returned from the server.
- The facilitator terminal prints
verifyandsettlecalls as they succeed. - The resource server logs incoming payments and successful responses.
- The client prints the final response body, for example:
{
"message": "You paid 0.5 HBAR natively!",
"data": {
"premium_content": "Exclusive Hedera network data with native payment",
"paid_with": "HBAR",
"amount_hbar": "0.5"
}
}You can also replay the flow manually with curl:
curl -i http://localhost:4021/hedera-usdcThe first response will be 402 Payment Required. Use the client to handle the payment, or craft your own request with the X-PAYMENT header if you are exploring deeper integrations.
TOKEN_NOT_ASSOCIATED– make sure the paying account associated USDC0.0.429274.INVALID_SIGNATURE– confirm you are using an ECDSA account and the private key matches the ID.insufficient payer balance– top up HBAR via the Hedera portal and USDC via the Circle faucet..envnot loading – double-check you renamed.env-localto.envin each directory.- Ports busy – adjust
PORTin the facilitator.envor the server script.
setup.sh– installs and builds the TypeScript packages and examples.examples/typescript/facilitator– Hedera facilitator reference implementation.examples/typescript/servers/express– paywalled Express server with Hedera endpoints.examples/typescript/clients/axios– sample client that pays via x402.specs/– protocol specs if you want to dig into the internals.
Run pnpm test from typescript/ if you want to execute the library test suite.
- x402.org – protocol overview and ecosystem
- Protocol specs – full request/response formats
- ROADMAP.md – upcoming protocol milestones
Questions during the hackathon? Drop into the event Discord or open an issue, and share what you are building!