Skip to main content

Pine Labs Online MPP Buyer SDK — x402 Machine Payments Protocol client for AI agents (Python)

Project description

pinelabs-online-mpp-client-sdk (Python)

Python port of @pinelabs-online/mpp-client-sdk. x402 Machine Payments Protocol client for AI agents.

Automatically intercepts HTTP 402 Payment Required responses, constructs UPI SBMD credentials, and completes the payment flow — zero manual payment handling required.

Installation

pip install pinelabs-online-mpp-client-sdk
# or from source
cd mpp-client-sdk-python
pip install -e .

Requires Python ≥ 3.9. Depends on httpx and PyJWT[crypto].

Quick Start

from pinelabs-online_mpp_client import pinelabs-onlineclient, pinelabs-onlineclientConfig, MppEnvironment

client = pinelabs-onlineclient.create(pinelabs-onlineclientConfig(
    clientId="your-client-id",
    clientSecret="your-client-secret",
    baseUrl=MppEnvironment.SANDBOX,  # or MppEnvironment.PRODUCTION
))

# `client.get` / `client.post` / `client.request` intercept 402s automatically.
response = client.get("https://api.example.com/paid-resource")
print(response.json())

client.close()

pinelabs-onlineclient.create(...) supports context-manager usage to release the underlying HTTP client:

with pinelabs-onlineclient.create(config) as client:
    response = client.get(url)

Configuration

from pinelabs-online_mpp_client import (
    pinelabs-onlineclient, pinelabs-onlineclientConfig, TokenDefaults,
    GrantexConfig, JwksConfig, MppEnvironment,
)

client = pinelabs-onlineclient.create(pinelabs-onlineclientConfig(
    clientId="…", clientSecret="…",

    baseUrl=MppEnvironment.SANDBOX,
    autoHandlePayment=True,

    requestTimeoutMs=30_000,
    maxRetries=3,
    initialRetryDelayMs=500,

    onChallenge=lambda challenge: None,
    onPaymentComplete=lambda receipt: None,

    tokenDefaults=TokenDefaults(maxCharges=10, ttlSeconds=3600),

    grantex=GrantexConfig(
        grantToken="eyJ…",
        jwks=JwksConfig(jwksUrl="https://grantex.dev/.well-known/jwks.json"),
        agentId="my-agent",
        enforceSpendingLimits=True,
    ),
))

How the 402 flow works

  1. Your code calls client.get(url) (or any HTTP method).
  2. If the server returns HTTP 402 with a WWW-Authenticate: Payment <challenge> header, the SDK:
    • decodes the challenge,
    • creates a one-time UPI SBMD payment token,
    • builds a credential,
    • retries the request with Authorization: Payment <credential>.
  3. The server captures the payment and returns HTTP 200 with a Payment-Receipt header.
  4. Your code receives the final 200 response transparently.

API

pinelabs-onlineclient.create(config) / pinelabs-onlineclient.create_verified(config)

create_verified additionally verifies the Grantex grant token before returning.

pinelabs-onlineclientInstance

Attribute Description
get, post, put, delete, patch, request, fetch HTTP methods with 402 interception
raw_http Underlying httpx.client (no interception)
methods.create_mandate(...) / .get_mandate(...) / .create_token(...) Direct MPP API ops
create_credential(challenge) Manually build a credential
grant_claims / verify_grant() Grantex helpers
close() / context manager Close the HTTP client

Utilities

from pinelabs-online_mpp_client import decode_challenge, decode_receipt, validate_challenge

challenge = decode_challenge(www_authenticate_header)
validate_challenge(challenge)
receipt = decode_receipt(payment_receipt_header)

Error handling

from pinelabs-online_mpp_client import MppError, MppNetworkError, MppChallengeError

try:
    response = client.get(url)
except MppChallengeError as err:
    ...
except MppNetworkError as err:
    ...
except MppError as err:
    print(err.code, err.http_status, err.details)

Grantex (AI Agent Authorization)

from pinelabs-online_mpp_client import (
    pinelabs-onlineclient, pinelabs-onlineclientConfig, GrantexConfig, JwksConfig,
    check_payment_authorization, extract_spending_limit, has_scope, parse_scope,
)

client = pinelabs-onlineclient.create_verified(pinelabs-onlineclientConfig(
    clientId="…", clientSecret="…",
    grantex=GrantexConfig(
        grantToken=grant_token,
        jwks=JwksConfig(jwksUrl="https://grantex.dev/.well-known/jwks.json"),
    ),
))

print(client.grant_claims)  # GrantTokenClaims(...)

License

MIT

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

pinelabs_online_mpp_client_sdk-0.1.1.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

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

pinelabs_online_mpp_client_sdk-0.1.1-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pinelabs_online_mpp_client_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b726c5a4f006db28614ef4812f1c44c5784af261874c3aca9420cebbc05a9877
MD5 c560d502dc3b6ae5c959b30566ece116
BLAKE2b-256 4ba5b5ea05891beb44b8871b76cea8bd1a5db4f6f835483f7741eee5f8e4a515

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pinelabs_online_mpp_client_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc60f3038645eabaa187947aa58bedf072dde9550bd8d3d7bafcfc1ae15bd933
MD5 800c1b9103acdc3aeb7d7ae4843a41ca
BLAKE2b-256 5a024708b44e64b6be04045a17d98e84a685cf27a12d01cf2954619abf9a0698

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