Inspiration

AI agents are rapidly writing our code, managing cloud infrastructure, and researching complex data. Yet the second an autonomous agent needs to purchase a high-value API call, specialized dataset, or GPU compute time it hits a brick wall.

Credit cards, 2FA SMS codes, and CAPTCHAs were designed exclusively for human shoppers with thumbs—they completely break autonomous workflows. While Web3 recently revived interest in HTTP 402 with crypto tokens, volatile token pricing, high gas spikes, and corporate accounting headaches make crypto unviable for traditional businesses.

We asked ourselves: What if autonomous AI agents had access to the world’s most trusted payment network: PayPal - powered by real USD fiat currency and governed by strict mathematical spending policies? That question led directly to Haggles.

What it does

Haggles is an HTTP 402 payment and budgeting protocol that brings autonomous machine-to-machine commerce to PayPal rails:

  1. For AI Agents & Buyers: Developers fund a prepaid USD balance using PayPal Orders v2 and issue scoped Agent Keys. Each key comes with gateway-enforced guardrails (per_call_max, daily_max, and domain allowlists). When an agent requests a paywalled API, @haggles/client intercepts the HTTP 402 Payment Required challenge, signs an authorized micro-payment token, and delivers the data in sub-100ms. If an agent loops or gets prompt-injected, Haggles cuts off spending at the gateway level before extra money is lost.
  2. For API Developers & Sellers: Developers can monetize any Express, Hono, or FastAPI route in two lines of middleware. Endpoints auto-register in the dashboard upon their first paid request—zero manual form filling.
  3. Instant Real-World Payouts: When sellers want their revenue, they click "Request Payout" in the portal. Haggles sweeps the ledger balance and triggers an automated fiat transfer directly to their PayPal account via the PayPal Payouts API.
  4. Audit-Ready Financials: Every single micro-payment generates an immutable Ed25519 tamper-proof cryptographic receipt and is recorded in an append-only double-entry micro-dollar ledger.

How we built it

Haggles is structured as a high-performance TypeScript monorepo powered by Bun:

• Payment Rails & Identity: Integrated PayPal Orders v2 for prepaid wallet deposits, Log in with PayPal (OIDC) for developer identity and automatic payer_id linking, and the PayPal Payouts v1 API for automated seller cashouts with cryptographic webhook verification.

• Core Ledger & Protocol Engine (apps/api): Built with Fastify, PostgreSQL, and Drizzle ORM. Implements an append-only, double-entry accounting ledger tracking integer micro-dollars (1 USD = 1,000,000 micros) with zero floating-point rounding errors.

• Autonomous AI Agent (examples/buyer-research-agent): Powered by Google Gemini (Gemini 1.5/2.0 Flash) using tool calling (list_sources and buy_source), orchestrated via the Vercel AI SDK to query paid vendors, negotiate HTTP 402 challenges, and synthesize comparative analysis.

• Developer SDKs (packages/core, client, server): Lightweight middleware adapters for Hono and Express alongside an HTTP fetch wrapper that transparently handles HTTP 402 challenge/response cycles.

• Interactive Portal (apps/portal): Built with Next.js 15, Tailwind CSS, Space Grotesk typography, and AG Grid Community for high-frequency financial telemetry, payment receipt inspection, and endpoint analytics.

• Deployment & Docs: Deployed on Render with PostgreSQL, paired with published interactive documentation at https://haggles.mintlify.site.

Challenges we ran into

• The Sub-Dollar Micropayment Dilemma: Traditional credit cards and payment gateways charge fixed processing fees (~$0.30–$0.49 per transaction), making a $0.01 or $0.05 API call economically impossible. We solved this by decoupling prepaid wallet funding (batched via PayPal Orders v2) from real-time micro-settlements (sub-cent double-entry ledger entries).

• Preventing Runaway LLM Loops: Autonomous agents can easily get caught in recursive function calls. We designed the Haggles Policy Engine, which validates daily maximums, per-call thresholds, and vendor allowlists at the protocol level in sub-5ms before any payment token is signed.

• Sub-Cent Fee Math: Allocating small fractions of a cent between platform fees and seller balances without losing money required integer micro-dollar arithmetic with exact integer flooring rules proven by property-based test suites.

Accomplishments that we're proud of

• End-to-End Autonomous Roundtrip in Sub-100ms: Watching a Google Gemini agent autonomously encounter an HTTP 402 challenge, negotiate the payment token, verify the cryptographic receipt, and parse the premium dataset—all in under 100ms with zero human intervention.

• Zero Floating-Point Financial Invariants: Proved 100% balance integrity across thousands of simulated concurrent micro-transactions using rigorous property-based and concurrency tests.

• Two-Line Developer Experience: Achieving our goal where any backend developer can turn an unpaid route into a metered, revenue-generating endpoint in just two lines of code with zero dashboard configuration.

• Complete AG Grid Enterprise UI & Mintlify Docs: A polished, responsive portal with real-time financial telemetry and complete published documentation.

What we learned

• Agentic Commerce Demands Real Fiat Rails: While crypto pioneered autonomous wallet concepts, enterprise developers and mainstream businesses need predictable pricing in real fiat currency (USD) and trusted compliance rails that integrate with existing corporate accounts.

• HTTP 402 is Ready for Primetime: The HTTP 402 specification has sat reserved since 1997. When combined with modern Ed25519 cryptographic signatures and PayPal rails, it creates an elegant, friction-free alternative to API keys and subscription paywalls.

• Double-Entry Discipline: Financial ledger design requires strict atomicity. Treating micro-dollars as integers with explicit debit/credit balancing prevented entire classes of edge-case concurrency bugs.

What's next for Haggles Protocol

• Strategic Partnership with PayPal: We are actively looking to partner directly with PayPal to integrate Haggles into the official PayPal Developer Platform. By embedding Haggles as a first-party PayPal product (e.g. PayPal Agent Pay), millions of existing PayPal merchants and developers could enable autonomous agent commerce and HTTP 402 monetization with a single toggle in their PayPal Developer Dashboard.

• No-Code Monetization for Everyone: We believe agentic commerce shouldn't be limited to backend software engineers. Down the line, we plan to launch a No-Code Haggles Gateway—a drag-and-drop link protector and proxy service where creators, writers, researchers, and small businesses can monetize proprietary PDFs, premium newsletters, datasets, or webhooks without writing a single line of code. Non-technical users will be able to paste any URL, set a USD price per access, and instantly start receiving autonomous agent micro-payments directly to their personal PayPal accounts.

• Official Drop-In Agent SDKs (LangChain, CrewAI & AutoGen): Releasing official pre-packaged tools and plugins across all major AI agent frameworks, allowing developers to equip their multi-agent workflows with pre-funded PayPal wallets in one line of configuration.

• Dynamic Algorithmic & Token-Based Pricing: Expanding beyond fixed-price calls to support dynamic metering—charging agents precisely per LLM output token generated, bytes streamed, or GPU compute seconds consumed.

• Google for Startups Scale & Multi-Currency Expansion: Scaling our cryptographic settlement infrastructure on Google Cloud Run and Cloud SQL via the Google for Startups Cloud Program, while expanding beyond USD into EUR, GBP, and global fiat currencies through PayPal's multi-currency clearing network.

Built With

Share this project:

Updates

Submission history