Create A Project
Create a new folder for the tutorial and initialize a Node.js project:type=module setting lets you run the import syntax used in the example files below.
Install the npm packages used by the server and client examples:
server.js and client.js.
Create server.js
Create a file named server.js and paste in the following code:
server.js
PAY_TO with the Stellar address that should receive USDC funds whenever a client pays for your service. Your wallet will need a testnet trustline setup for USDC. More information here: Setting up a testnet wallet
FACILITATOR_URL points to a managed facilitator endpoint that handles verification and settlement. In this example, the facilitator is Coinbase’s testnet facilitator.
Start the API locally:
402 Payment Required status. The response includes headers that describe the payment requirements so that a compliant client can build a signed payment payload and retry the request.
Behind the scenes, the facilitator handles verification and onchain settlement. You do not need to run your own blockchain nodes or build raw transactions yourself. A managed facilitator service takes the signed payment payload from the client, verifies it against the specified network and asset, settles the transaction, and returns verification to your server so it can complete the request.
The client generates a signed payment payload that authorizes a stablecoin transfer, the facilitator verifies and settles that payload on the Stellar network, and the server returns a 200 response with the protected data.
The funds settle directly to the wallet address in PAY_TO.
Create client.js
With your Express server running, create a file named client.js and paste in the following code:
client.js
Handling payment responses
When integrating x402 into your own client, handle the key response states:Setting up a testnet wallet
Next, create a fresh account and fund it with testnet XLM and testnet USDC using Stellar Lab:- Create a new keypair using Stellar Lab or the CLI:
- Fund with testnet XLM (Native Stellar Token): https://lab.stellar.org/account/fund
- Create the USDC trustline (there’s a button on the fund page above), sign and submit that transaction (for detailed instructions, see below)
- Visit the Circle faucet, select Stellar Testnet from the networks and add your public key for the wallet address input: https://faucet.circle.com
.env
.env and replace S... with the secret key for the account that will sign transactions and pay for the resource.
Step 3 (expanded): Establish USDC trustline
A trustline tells Stellar your account accepts USDC. Without it, you cannot receive payments. Testnet USDC Issuer:GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5
Using Stellar Lab
- Go to Transaction Builder
- Enter your public key as source account
- Click “Fetch next sequence number”
- Add Operation → “Change Trust”
- Asset Code:
USDC - Issuer:
GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5 - Sign and submit
Using code
Run The Client
Once the account is funded and the secret key is in.env, run the client in a second terminal:
$0.01 of testnet USDC from the client account to the server account.
The client then receives the protected JSON data: { secret: "valuable content" }
The server is set up as an MVP for machine-to-machine payments. If you want to enable human payments, the next step is to build a paywall. Take a look at the demo code for examples: https://github.com/stellar/x402-stellar/tree/main/examples/simple-paywall
Additional Documentation
- OpenZeppelin x402 Facilitator Plugin Source Code
- OpenZeppelin x402 Facilitator Docs
- OpenZeppelin Stellar Relayer SDK
- OpenZeppelin Stellar Relayer Docs
- OpenZeppelin Smart Account
Learn more
- x402 on Stellar (Landing page) - Stellar x402 overview and resources
- x402 on Stellar (Blog) - Foundation news and announcement
- x402 protocol (Coinbase Developer Platform) - Official x402 protocol overview and spec
- x402 protocol specification - x402 Specification and Whitepaper
- x402 GitHub - Official x402 Protocol GitHub Repo
- @x402/stellar (npm) - npm package for x402 on Stellar
- x402-stellar (repo) - Tools, examples, and references for x402 on Stellar
- Signing Soroban invocations - Auth-entry signing and transaction signing on Stellar