X402 Tutorial
The Problem: Accessing Paywalled Data in Oracles
What We're Building
┌─────────────────────────────────────────────────────────────────────────────┐
│ X402 Oracle Flow │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ YOUR APP ORACLE (TEE) PAYWALLED API │
│ ──────── ──────────── ───────────── │
│ │
│ 1. Derive X402 payment ─────┐ │
│ headers (USDC auth) │ │
│ ▼ │
│ 2. Define feed with ┌─────────┐ │
│ ${PLACEHOLDER} ───►│Crossbar │ │
│ variables └────┬────┘ │
│ │ │
│ 3. Pass headers as │ 4. Oracle makes ┌────────┐ │
│ variable overrides ──────┼─────► authenticated ────────────►│ Helius │ │
│ │ HTTP request │ RPC │ │
│ │ with X-PAYMENT └───┬────┘ │
│ │ header │ │
│ │ │ │
│ │ 5. Paywalled data ◄───────────────┘ │
│ │ returned │
│ ▼ │
│ 6. Signed oracle data ┌─────────┐ │
│ in quote account ◄──│ Oracle │ │
│ │Response │ │
│ └─────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘Prerequisites
Installation
Dependencies
How X402 Authentication Works
Implementation
Step 1: Set Up Imports and Constants
Step 2: Define the Oracle Feed with Placeholders
Step 3: Initialize Payment Infrastructure
Step 4: Check USDC Balance
Step 5: Derive X402 Payment Headers
Step 6: Fetch Managed Update with Variable Overrides
Step 7: Build and Send Transaction
Important Constraints
Why numSignatures Must Be 1
Simulation Warning: Don't Pay Twice
Why Inline Feeds?
Running the Example
Next Steps
Last updated