4οΈβ£API Flow
Flow Diagrams
Flow 1: Initial Chat/Query Request
Client API AI Service Database
β β β β
βββPOST {messages}βββββββΊβ β β
β wallet, chain β β β
β βββValidate InputββββββββββββΊβ β
β β β β
β βββProcess QueryβββββββββββββΊβ β
β β β β
β βββAI Responseββββββββββββββββ€ β
β β (transaction data) β β
β β β β
β βββSave OperationββββββββββββββββββββββββββββββββββββΊβ
β β β β
βββResponse withββββββββββ€ β β
β operationId β β β
β β β βFlow 2: Payment Request
Flow 3: Payment Verification & Execution
Detailed Step-by-Step Flow
Phase 1: Initial Request (Chat/Query)
Step 1: Client Sends Natural Language Query
Endpoint: POST /api/x402/process
Request Body:
Step 2: Middleware Check
The x402Middleware checks 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 X402Service processes 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
Phase 2: Payment Request
Step 1: Client Requests Payment Info
Endpoint: POST api.hashhunter.app/api/process
Request Body:
Step 2: Middleware Routing
The x402Middleware detects:
β 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:
Step 5: Return 402 Response
Phase 3: Payment Verification & Transaction Retrieval
Step 1: Client Submits Payment Proof
Endpoint: POST api.hashhunter.app/api/process
Headers:
Request Body:
Step 2: Middleware Routing
The x402Middleware detects:
β 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.networkCheck 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:
Step 6: Return Transaction
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