4οΈβ£API Flow
Table of Contents
Architecture
Flow Diagrams
Detailed Step-by-Step Flow Architecture
βββββββββββββββ β Client β ββββββββ¬βββββββ β βΌ βββββββββββββββββββββββββββββββββββ β POST /api/process β β (x402Middleware) β ββββββββ¬βββββββββββββββββββββββββββ β ββββ Has X-PAYMENT Header? βββΊ handlePaymentVerification() β β β βββΊ Verify with Facilitator β βββΊ Retrieve Transaction β βββΊ Return for Execution β ββββ Has operationId? βββββββββββΊ handlePaymentRequest() β β β βββΊ Retrieve Operation from DB β βββΊ Return 402 Payment Required β ββββ Neither? ββββββββββββββββββΊ X402Controller.handleQuery() β βββΊ Validate Input βββΊ Process with AI βββΊ Generate Transaction βββΊ Save to DB & Return operationIdFlow DiagramsFlow 1: Initial Chat/Query RequestClient API AI Service Database β β β β βββPOST {messages}βββββββΊβ β β β wallet, chain β β β β βββValidate InputββββββββββββΊβ β β β β β β βββProcess QueryβββββββββββββΊβ β β β β β β βββAI Responseββββββββββββββββ€ β β β (transaction data) β β β β β β β βββSave OperationββββββββββββββββββββββββββββββββββββΊβ β β β β βββResponse withββββββββββ€ β β β operationId β β β β β β βFlow 2: Payment RequestClient API Database Facilitator β β β β βββPOST {operationId}ββββΊβ β β β β β β β βββGet OperationββββββββββΊβ β β β β β β βββOperation Dataββββββββββ€ β β β β β βββ402 Payment Requiredβββ€ β β β (payment config) β β β β β β βFlow 3: Payment Verification & ExecutionClient API Database Facilitator β β β β βββPOST {operationId}ββββΊβ β β β X-PAYMENT: xxx β β β β βββVerify PaymentβββββββββββββββββββββββββββββββββΊβ β β β β β βββPayment Validβββββββββββββββββββββββββββββββββββ€ β β β β β βββMark as PaidβββββββββββΊβ β β β β β β βββGet TransactionβββββββββΊβ β β β β β βββTransaction Dataβββββββ€ β β β (ready to execute) β β β β β β βDetailed Step-by-Step FlowPhase 1: Initial Request (Chat/Query)Step 1: Client Sends Natural Language Query
Endpoint:
POST /api/x402/processRequest Body:
{ "messages": [ { "role": "user", "content": "Send 10 USDC to 0xRecipientAddress" } ], "wallet": "0xYourWalletAddress", "chain": "base" }Step 2: Middleware Check
The
x402Middlewarechecks the request:β No
X-PAYMENTheader β Continueβ No
operationIdβ Continueβ Pass to controller
Step 3: Input Validation
The controller validates:
Messages Array: Must be non-empty array with role and content
Chain: Must be one of
base,polygon,avalanche, orsolanaWallet Address: Must match the chain type format
EVM chains:
0x+ 40 hex charactersSolana: Base58, 32-44 characters
Step 4: AI Processing
The
X402Serviceprocesses the query:Sends messages to AI (OpenAI)
AI analyzes the intent
Generates appropriate blockchain transaction(s)
Step 5: Save to Database
Creates a new
OperationrecordStores: messages, wallet, chain, transactions, status
Generates unique
operationId
Step 6: Response
{ "status": "success", "operationId": "clxxx123456789", "message": "Transaction prepared", "requiresPayment": true }Phase 2: Payment RequestStep 1: Client Requests Payment Info
Endpoint:
POST api.hashhunter.app/api/processRequest Body:
{ "operationId": "clxxx123456789" }Step 2: Middleware Routing
The
x402Middlewaredetects:β Has
operationIdβ No
X-PAYMENTheaderβ Routes to
handlePaymentRequest()
Step 3: Retrieve Operation
Queries database for operation by ID
Validates operation exists
Extracts chain information
Step 4: Generate Payment Configuration
Creates X402 payment requirements:
{ scheme: "exact", network: "base", // or polygon, avalanche, solana resource: "https://api.hashhunter.app/api/process", payTo: "0x5bc818f625cad773d35c6b81e944cc820a00bfc0", asset: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC maxAmountRequired: "0", maxTimeoutSeconds: 300 }Step 5: Return 402 Response
{ "x402Version": 1, "error": "Token payment required", "accepts": [ { /* payment config */ } ], "operationId": "clxxx123456789" }Phase 3: Payment Verification & Transaction RetrievalStep 1: Client Submits Payment Proof
Endpoint:
POST api.hashhunter.app/api/processHeaders:
X-PAYMENT: <payment_proof_string>Request Body:
{ "operationId": "clxxx123456789" }Step 2: Middleware Routing
The
x402Middlewaredetects:β Has
X-PAYMENTheaderβ Routes to
handlePaymentVerification()
Step 3: Payment Verification
Decode Payment: Parses X-PAYMENT header
Retrieve Operation: Gets operation from database
Build Payment Config: Creates verification requirements
Verify with Facilitator: Calls facilitator service at
https://facilitator.payai.networkconst settleResponse = await settle(payment, paymentConfig);Check Result: Validates
settleResponse.success
Step 4: Update Database
If payment is valid:
Marks operation as
paidStores payment transaction hash
Timestamps the payment
Step 5: Format Transactions
Prepares transactions for execution:
// EVM Transaction { type: "evm", data: { to: "0x...", data: "0x...", value: "0", chainId: 8453 } } // Solana Transaction { type: "svm", data: "base64_encoded_transaction" }Step 6: Return Transaction
{ "status": "success", "message": "Payment verified. Transaction ready for execution.", "operationId": "clxxx123456789", "transactions": [ { "type": "evm", "data": { /* transaction object */ } } ] }Step 7: Client Executes Transaction
The client now has the transaction data and can:
Sign the transaction with their wallet
Broadcast to the blockchain
Wait for confirmation
Last updated