Skip to main content

x402-trinity

Let a Python process pay for things by itself — with hard spending limits, no hosted wallet service, and no changes to the code doing the fetching.

Standard library only. No dependencies, at all.

pip install x402-trinity

Why

When a script hits a paid resource it gets 402 Payment Required. Without something to handle that, the request simply fails.

x402-trinity handles it: reads the challenge, checks it against limits you set, signs, retries. All of it locally — no hosted wallet service, no third-party API, and the key never leaves your process.

Gas exists but the payer doesn't pay it — under EIP-3009 the facilitator submits the transfer, so the wallet only ever needs USDC.

Use

import os
from x402_trinity import X402Client, Policy

client = X402Client(
    private_key=os.environ["X402_PRIVATE_KEY"],
    policy=Policy(
        max_amount_per_request=5000,      # 0.005 USDC per call   (REQUIRED)
        total_budget=1_000_000,           # 1.00 USDC lifetime    (REQUIRED)
        allow_hosts=["api.example.com"],
        allow_pay_to=["0x..."],
        # Without allow_assets the caps are denominated in WHATEVER TOKEN the seller names.
        # 5000 then means 5000 units of that token, not 0.005 USDC. Pin the asset.
        allow_assets=["0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"],   # USDC on Base
    ),
)

body = client.urlopen("https://api.example.com/data").read()

Or as a decorator, so payment-unaware code just works:

from x402_trinity import x402_telemetry, Policy

@x402_telemetry(private_key=KEY, policy=Policy(max_amount_per_request=5000,
                                               total_budget=1_000_000))
def harvest():
    return urllib.request.urlopen("https://sensor.local/v1/lidar").read()

Inside the decorated function urllib.request.urlopen pays 402s automatically. Existing code is untouched.

Scope

Buyer only. To charge for a resource, use the TypeScript seller in the same project.

Base mainnet + USDC. Other EVM chains work by passing custom_chains to X402Client. Check any entry against the deployed contract first: call DOMAIN_SEPARATOR() and confirm it equals what this library computes. A wrong name or version produces a signature that looks valid and the contract rejects.

Limits are not optional

max_amount_per_request and total_budget are required arguments. An auto-payer without caps is a money leak controlled by whoever runs the server on the other end. Set allow_hosts and nothing else can be paid, however convincing the challenge looks.

Before you use it with real money

This software signs payment authorizations. You are responsible for the funds in any wallet you configure it with, for the limits you set, and for the behaviour of any autonomous process you connect to it. Use a dedicated wallet holding only what you would accept losing.

Links

License

Business Source License 1.1 — source-available, converts to MIT on 2029-08-25. See the LICENSE file in the repository.

Download files

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

Source Distribution

x402_trinity-0.1.7.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

x402_trinity-0.1.7-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file x402_trinity-0.1.7.tar.gz.

File metadata

  • Download URL: x402_trinity-0.1.7.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for x402_trinity-0.1.7.tar.gz
Algorithm Hash digest
SHA256 cb0fcfa7791121efa8429c9ff592fcfc7a26d38c9a3bba126a18c53ca9213ac6
MD5 01b1cb25b1632e6936699fe750588b38
BLAKE2b-256 806d9a0191bc8c938d1f7be501135f07d036cd7e6d1e6d0519d47227ddf68cb9

See more details on using hashes here.

File details

Details for the file x402_trinity-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: x402_trinity-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.11

File hashes

Hashes for x402_trinity-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b5b5b7987a87f9233849c149694844cab29e38d6bcc1816575e9a0bcad6613b6
MD5 531a05030c877dfc54dc22930b6b533b
BLAKE2b-256 be11bf3aac6fc0f43b96c6a1d23d9246d445f7ca9f566f3f774664efdb9dece0

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.9

2 files

0.1.8

2 files

This release

0.1.7 This release

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page