x402v2 protocol on Starknet

Pay for APIs with
Circle's USDC on Starknet.

No unlimited approvals needed. Secure one-time payments with gas sponsorship.

npm install starknet-x402

Protocol Flow

  Client                          Server                       Starknet
    |                                |                              |
    |── GET /api/data ──────────────>|                              |
    |                                |                              |
    |<── 402 ────────────────────────|                              |
    |    PAYMENT-REQUIRED: {         |                              |
    |      amount, asset, payTo      |                              |
    |    }                           |                              |
    |                                |                              |
    |── sign OutsideExecution ──────>|  (AVNU paymaster)            |
    |   (SNIP-9, off-chain)          |                              |
    |                                |                              |
    |── GET /api/data ──────────────>|                              |
    |   PAYMENT-SIGNATURE: {...}     |                              |
    |                                |── verify call contents ─────>|
    |                                |── execute_from_outside_v2 ──>|
    |                                |   (AVNU pays gas)            |
    |                                |<── tx confirmed ─────────────|
    |                                |                              |
    |<── 200 + data ─────────────────|                              |
    |    PAYMENT-RESPONSE: {tx}      |                              |

Server

import { paymentMiddleware } from 'starknet-x402';

export const middleware = paymentMiddleware(
  RECIPIENT,
  {
    '/api/data': {
      price: '10000',
      tokenAddress: USDC,
      network: 'sepolia',
    },
  },
  { url: FACILITATOR_URL }
);

Client

import { x402axios } from 'starknet-x402';

const result = await x402axios.get(
  'https://api.example.com/data',
  {
    account,
    network: 'starknet-sepolia',
  }
);

// Done! Payment handled
console.log(result.data);

starknet x402 v2