Skip to main content

Invoice Middleware

Invoica operates as transparent middleware in the x402 payment flow. It intercepts payment requests to automatically generate invoices, calculate taxes, enforce budgets, and monitor settlements.

Architecture

The middleware consists of three core components:

1. Proxy Layer

The proxy intercepts HTTP requests between AI agents and resource servers:
  • Detects 402 Payment Required responses
  • Injects invoice generation into the payment flow
  • Passes through non-payment requests transparently
  • Adds invoice metadata to successful responses

2. Invoice Engine

When a payment is detected, the invoice engine:
  • Generates a unique invoice number
  • Calculates line items from the payment amount
  • Applies tax rules based on buyer/seller jurisdictions
  • Creates a PDF invoice document
  • Stores the invoice in the database

3. Settlement Monitor

After payment is sent, the settlement monitor:
  • Poll-based detection — Periodically checks the blockchain for transaction confirmation
  • Event-driven detection — Listens for on-chain events via WebSocket
  • Updates invoice status when settlement is confirmed
  • Triggers webhook notifications

Invoice Lifecycle

Dual Settlement Detection

Invoica uses two independent detection methods for reliability:

Poll-Based Detection

A background worker periodically queries the blockchain:

Event-Driven Detection

WebSocket listeners monitor on-chain events in real-time:
The dual approach ensures that if one method fails (e.g., WebSocket disconnects), the other catches the settlement.

Configuration

Configure the middleware via environment variables: