Skip to main content

Delegare Toolkit

The Delegare toolkit enables your LangChain and LangGraph agents to execute payments, check budgets, and handle x402-gated content automatically via the Delegare API. Unlike standard payment gateways, Delegare issues an Intent Mandate (SD-JWT-VC) rather than returning raw card details. The agent uses this mandate to request charges up to the user’s pre-authorized budget, ensuring payments are entirely separated from the LLM context.

Setup

First, you need to install the langchain-delegare package:
Second, sign up for a merchant account at Delegare and retrieve your API Key and Merchant ID. Set these as environment variables:

Initializing the Toolkit

You can instantiate the DelegareToolkit using environment variables or by passing your credentials explicitly. It’s recommended to define an allowed_amounts_cents whitelist for safety.

Available Tools

The toolkit provides 7 unique tools designed for distinct phases of the agent payment lifecycle:
  1. setup_spending_mandate: Generates a one-time browser link for the user to securely input their card/wallet.
  2. poll_setup_session: Checks if the user completed the setup. Once completed, returns the intentMandate string.
  3. check_mandate_balance: Retrieves the remaining budget of an active intent mandate.
  4. authorize_agent_payment: Executes a charge against a mandate (enforced server-side).
  5. delegare_fetch: Fetches URLs and automatically resolves HTTP 402 errors using the mandate.
  6. revoke_mandate: Cancels an active mandate.
  7. verify_receipt: Cryptographically verifies the X-PAYMENT-RESPONSE payload of a settled charge.

Usage in an Agent

Here is a full example showing how to initialize the tools and connect them to an agent built with LangGraph.

Agent with Budget Awareness

Delegare provides a custom DelegareBudgetCallbackHandler that intercepts tool completions and monitors mandate budgets automatically to stop LLMs from overspending before the Delegare API physically rejects the transaction.

LangGraph Idempotency

When dealing with payments, idempotent execution is critical. If a LangGraph workflow crashes and retries, you want to guarantee the agent doesn’t double-charge the user. The integration provides get_idempotency_key which securely hashes LangGraph thread states into deterministic UUIDs.

Runnables and LCEL

For seamless data retrieval requiring x402 payment headers, you can use the X402AutoPayRunnable directly in your LCEL chains. It catches 402 errors, executes the required micro-payment via the intent mandate, and retries the fetch autonomously.