Skip to content

[SDK + Soroban] Build x402 autonomous payment flow integration demo #8

Description

@IyanuOluwaJesuloba

Summary

StellarMind's README and architecture docs describe x402 compatibility as a core feature. x402 is Stellar's standard for machine-to-machine payments — an agent requests a resource, the server responds with a price, the agent pays, and the resource is delivered. This issue builds a complete working demo that shows this flow end-to-end using StellarMind's SpendingPolicy contract.

x402 protocol overview: https://x402.org Stellar x402 docs: https://developers.stellar.org/docs/build/apps/x402

What x402 Looks Like Without StellarMind

Today, an x402 flow requires a human to sign every payment:

Agent → GET /resource
Server → 402 Payment Required { price: "0.01 USDC", address: "GABC..." }
Agent → ??? needs human to sign a payment transaction
Human → signs Stellar transaction
Agent → GET /resource + payment proof
Server → 200 OK + resource

The "needs human to sign" step is what breaks autonomous agent workflows.

What StellarMind Enables

With a SpendingPolicy, the agent can pay autonomously:

Owner creates policy: agent GBKR... can spend up to $0.50/tx, $10/day
Agent → GET /resource
Server → 402 Payment Required { price: "0.01 USDC", address: "GABC..." }
Agent → mind.policy.executePayment({ policyId, recipient: "GABC...", amount: "0.01" })
Stellar settles in <5 seconds — NO human needed
Agent → GET /resource + payment proof (tx hash)
Server → 200 OK + resource

What Needs to Be Built

Create a self-contained demo at sdk/examples/x402-demo/:

File 1: sdk/examples/x402-demo/server.ts

A simple Express.js server that:

Has a single paid endpoint: GET /api/weather

Returns 402 Payment Required with price 0.01 USDC and a server wallet address if no payment proof is in the request header

If the request includes X-Payment-Tx: header, verifies the transaction on Stellar (check it paid the right amount to the right address) and returns mock weather data

ts

// Example server response on 402:
{
  "status": 402,
  "price": "0.01",
  "asset": "USDC",
  "payTo": "GABC...SERVER_ADDRESS",
  "memo": "weather-api-call"
}

File 2: sdk/examples/x402-demo/agent.ts

An autonomous agent script that:

  1. Reads a POLICY_ID and AGENT_SECRET_KEY from environment variables
  2. Attempts GET /api/weather
  3. Receives 402 response, parses the price and recipient
  4. Calls mind.policy.executePayment() to pay autonomously (no human involvement)
  5. Retries GET /api/weather with the transaction hash in the X-Payment-Tx header
  6. Prints the weather data received
  7. Does this in a loop 5 times to demonstrate repeated autonomous payments

File 3: sdk/examples/x402-demo/setup.ts

A setup script that:

  1. Creates a testnet account for the server
  2. Creates a testnet account for the agent
  3. Creates a spending policy: agent can spend up to $0.10/tx, $1.00/day
  4. Prints all addresses and the policy ID for use in the demo

File 4: sdk/examples/x402-demo/README.md

Step-by-step instructions to run the demo:

bash

# 1. Run setup
npx ts-node setup.ts

# 2. Start server
npx ts-node server.ts

# 3. Run agent (in another terminal)
POLICY_ID=1 AGENT_SECRET_KEY=S... npx ts-node agent.ts

Relevant Files

FileRolesdk/src/policy.ts — executePayment(): Core function the agent calls
sdk/src/stellar.ts — toStroops(), fromStroops(): Price conversion helpers
sdk/src/types.ts — ExecutePaymentParams: Types for the payment call

Acceptance Criteria

All 4 files created under sdk/examples/x402-demo/

  • setup.ts runs without error and prints policy ID + addresses

  • server.ts starts and responds with 402 on unauthenticated requests

  • server.ts returns data when valid X-Payment-Tx header is provided

  • agent.ts completes 5 autonomous payment + data retrieval cycles

  • Zero human interaction required after setup.ts runs

  • Each payment visible on Stellar Testnet explorer (stellarchain.io)

  • README.md is clear enough for a developer to run the demo from scratch in under 10 minutes

  • Demo works on Stellar Testnet

Dependencies to Add

bash

cd sdk
npm install express @types/express

Estimated Effort

Large (8–14 hours)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions