{"id":27557,"library":"x402","title":"x402 Payment Protocol SDK","description":"The x402 Python SDK implements the x402 Payment Protocol, enabling micropayments for HTTP resources. Version 2.9.0 supports Python 3.10+ and provides client and server utilities. Active development under the x402-foundation.","status":"active","version":"2.9.0","language":"python","source_language":"en","source_url":"https://github.com/x402-foundation/x402","tags":["x402","payment-protocol","micropayments","sdk","python3"],"install":[{"cmd":"pip install x402","lang":"bash","label":"Install x402 SDK"}],"dependencies":[{"reason":"Required for HTTP client functionality.","package":"httpx","optional":false},{"reason":"Required for cryptographic operations.","package":"cryptography","optional":false},{"reason":"Used for data validation and settings management.","package":"pydantic","optional":false}],"imports":[{"note":"Client is a class for making paywalled requests.","symbol":"Client","correct":"from x402.client import Client"},{"note":"Server is a class for handling payment verification.","symbol":"Server","correct":"from x402.server import Server"},{"note":"PaymentError is the base exception for payment failures.","symbol":"PaymentError","correct":"from x402.exceptions import PaymentError"}],"quickstart":{"code":"import os\nfrom x402.client import Client\n\nurl = \"https://api.example.com/protected\"\n# In production, set X402_PRIVATE_KEY environment variable\nprivate_key = os.environ.get('X402_PRIVATE_KEY', '')\nclient = Client(private_key=private_key)\ntry:\n    response = client.get(url)\n    print(response.status_code)\n    print(response.text)\nexcept Exception as e:\n    print(f\"Error: {e}\")","lang":"python","description":"Initialize the x402 client and make a paywalled GET request. The private key is read from environment variable; leave empty for demo."},"warnings":[{"fix":"Always pass `private_key` to `Client()` or set `X402_PRIVATE_KEY` environment variable.","message":"In version 2.x, the `private_key` parameter is required for Client instantiation. In version 1.x it was optional and a default test key was used. Code without explicit key will break.","severity":"breaking","affected_versions":"<=1.x -> >=2.0"},{"fix":"Replace `from x402.payment import PaymentRequest` with `from x402.client import prepare_payment`.","message":"The `PaymentRequest` class was removed in version 2.0. Use `x402.client.prepare_payment()` instead.","severity":"breaking","affected_versions":"<2.0 -> >=2.0"},{"fix":"Ensure the private key is in PEM format, e.g., read from file like `open('key.pem').read()`.","message":"The SDK expects the private key in PEM format. Plain hex or raw bytes will raise a `cryptography` error.","severity":"gotcha","affected_versions":"all"},{"fix":"Call `client.get(url)` instead of `client.make_payment('GET', url)`.","message":"Method `Client.make_payment()` is deprecated since 2.8.0 and will be removed in 3.0. Use `Client.get()` or `Client.post()` directly.","severity":"deprecated","affected_versions":">=2.8.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run `pip install x402` to install the package.","cause":"The library is not installed or is installed in a different environment.","error":"ModuleNotFoundError: No module named 'x402'"},{"fix":"Use `from x402.client import Client` instead of `import x402; x402.client.Client`.","cause":"Importing incorrectly. `x402.client` is a submodule, not an attribute of the top-level package.","error":"AttributeError: module 'x402' has no attribute 'client'"},{"fix":"Provide a PEM-formatted private key string (begins with '-----BEGIN PRIVATE KEY-----').","cause":"The private key provided is not in valid PEM format.","error":"ValueError: Private key must be in PEM format"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}