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-PAYMENT header β†’ 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, or solana

  • Wallet Address: Must match the chain type format

    • EVM chains: 0x + 40 hex characters

    • Solana: Base58, 32-44 characters

Step 4: AI Processing

The X402Service processes the query:

  1. Sends messages to AI (OpenAI)

  2. AI analyzes the intent

  3. Generates appropriate blockchain transaction(s)

Step 5: Save to Database

  • Creates a new Operation record

  • Stores: 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-PAYMENT header

  • β†’ 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-PAYMENT header

  • β†’ Routes to handlePaymentVerification()

Step 3: Payment Verification

  1. Decode Payment: Parses X-PAYMENT header

  2. Retrieve Operation: Gets operation from database

  3. Build Payment Config: Creates verification requirements

  4. Verify with Facilitator: Calls facilitator service at https://facilitator.payai.network

  5. Check Result: Validates settleResponse.success

Step 4: Update Database

If payment is valid:

  • Marks operation as paid

  • Stores 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:

  1. Sign the transaction with their wallet

  2. Broadcast to the blockchain

  3. Wait for confirmation

Last updated