Skip to main content

Python client for the Gatefare x402 payment marketplace. Pay any Gatefare-listed API in USDC on Base.

Project description

gatefare

PyPI version Python versions CI License: MIT

Python client for the Gatefare x402 payment marketplace. Pay any Gatefare-listed API in USDC on Base with a few lines of code. Non-custodial. No platform account required for paying; your wallet signs every authorization.

Install

pip install gatefare

Requires Python 3.10+.

Quick start

from gatefare import Gatefare

gf = Gatefare(
    wallet_private_key=os.environ["WALLET_PRIVATE_KEY"],
    spend_caps={"per_call_usdc": 0.50, "per_day_usdc": 5.00},
)

# Search the catalog
apis = gf.list_catalog(price_limit_usdc=0.10, limit=5)

# Pay and call
result = gf.call_api(apis[0].slug, query={"city": "Berlin"})
print(result.status, result.data)   # 200 { ... }

# Check balance
balance = gf.check_balance()
print(balance.usdc)   # 12.345

What the SDK does for you

  • Speaks the x402 v2 protocol: handles the 402 challenge, signs an EIP-3009 USDC transferWithAuthorization with your wallet, resends with the X-Payment header, decodes the response.
  • Enforces SDK-local spend caps before signing any authorization so a compromised resource server cannot drain the wallet past whatever you configured.
  • Cross-checks the server's quoted price against the catalog listing and refuses to sign if they diverge by more than 1%.
  • Retries failed claims automatically through Gatefare's 24h / 10-attempt budget via the /p/_claim/<id> endpoint.
  • Decodes JSON / text responses without you having to handle the raw stream.

What the SDK does NOT do

  • Custody your funds. The private key never leaves your process; we do not run a hosted wallet. Pass wallet_private_key from an env var, KMS, or hardware signer (whatever returns the 0x... hex).
  • Charge you for failed settles. If the on-chain settle reverts, no spend is recorded against your daily cap.
  • Connect to the chain for catalog reads. Catalog endpoints are pure HTTP and work without a wallet — useful for read-only discovery.

Spend caps

Two layers of guardrail. Both are enforced locally, before any wire activity that costs money:

gf = Gatefare(
    wallet_private_key=...,
    spend_caps={
        "per_call_usdc": 1.00,   # default 1.00
        "per_day_usdc": 10.00,   # default 10.00
    },
)

Per-call cap can be overridden per call:

gf.call_api("expensive-listing", per_call_cap_usdc=5.00)

Per-day cap resets at midnight UTC. Backed by an in-process dict; provide a custom SpendStorage (any class with read(day_key) and add(day_key, delta)) to make it crash-safe across restarts.

Framework adapters

All optional. Each subpackage exposes plain dicts that match the host framework's expected shape — we do not import any framework as a runtime dependency.

LangChain

from langchain.tools import Tool
from gatefare.adapters.langchain import (
    gatefare_langchain_tool, gatefare_catalog_tools,
)

# One specific Gatefare API as a tool
d = gatefare_langchain_tool(gf, slug="weather-now")
tool = Tool(name=d["name"], description=d["description"], func=d["func"])

# Or expose the whole filtered catalog as a toolbelt
descs = gatefare_catalog_tools(gf, price_limit_usdc=0.10)
tools = [Tool(name=d["name"], description=d["description"], func=d["func"]) for d in descs]

LlamaIndex

from llama_index.core.tools import FunctionTool
from gatefare.adapters.llamaindex import gatefare_llamaindex_tool

d = gatefare_llamaindex_tool(gf, slug="weather-now")
tool = FunctionTool.from_defaults(fn=d["fn"], name=d["name"], description=d["description"])

OpenAI function-calling

from openai import OpenAI
from gatefare.adapters.openai_tools import gatefare_openai_tools, gatefare_openai_dispatch

client = OpenAI()
tools = gatefare_openai_tools(gf, price_limit_usdc=0.05)

resp = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Berlin?"}],
    tools=tools,
)

tool_call = resp.choices[0].message.tool_calls[0]
result = gatefare_openai_dispatch(gf, tool_call)
# Feed `result` back as a tool-role message and re-call the model.

Anthropic tool-use

import anthropic
from gatefare.adapters.anthropic_tools import gatefare_anthropic_tools, gatefare_anthropic_dispatch

client = anthropic.Anthropic()
tools = gatefare_anthropic_tools(gf, price_limit_usdc=0.05)

message = client.messages.create(
    model="claude-3-5-sonnet-latest",
    max_tokens=1024,
    messages=[{"role": "user", "content": "What's the weather in Berlin?"}],
    tools=tools,
)
for block in message.content:
    if block.type == "tool_use":
        result = gatefare_anthropic_dispatch(gf, block)

Errors

Two named exceptions:

  • SpendCapError — your call was refused locally because it would exceed a configured spend cap. The wallet never produced a signature.
  • GatefareApiError — Gatefare returned a non-2xx response that the SDK cannot recover from (unknown slug, exhausted claim, malformed quote).

Non-2xx responses from the upstream API (404 from a paid endpoint, 500 after a successful settle that exhausted retries) are surfaced as the status field on CallApiResult rather than raised.

Configuration reference

gf = Gatefare(
    wallet_private_key=...,             # required for paid calls
    base_url="https://gatefare.io",     # override for staging
    spend_caps={"per_call_usdc": 1.0, "per_day_usdc": 10.0},
    personal_access_token="gfpat_...",  # optional, raises rate limits
    http=httpx.Client(timeout=30),      # optional, for tests / custom timeouts
)

Use with Gatefare(...) as gf: to auto-close the underlying httpx client when done.

License

MIT. See LICENSE.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gatefare-0.1.1.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gatefare-0.1.1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file gatefare-0.1.1.tar.gz.

File metadata

  • Download URL: gatefare-0.1.1.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gatefare-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e44571c210c6b636e40b5168300cbfcc769e79c46e849a11baf4bc57a4a29820
MD5 2deed55bb176a833a35951b4c8eb3735
BLAKE2b-256 3ce59446f64ac15fd89671978a92c64e2bfea9c253726e6445670920b4877b6b

See more details on using hashes here.

File details

Details for the file gatefare-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: gatefare-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for gatefare-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0129b6007810a272994770da6722e4b64ec4fec88edba06b681cadc913933bb3
MD5 a4112b9d20527183b7dbf0b144b4a0a9
BLAKE2b-256 d5e43abc42e08a3cf57b51e63288d3527589952c663d7744830df532cf0b5692

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page