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
- Your code calls
client.get(url)(or any HTTP method). - 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>.
- The server captures the payment and returns HTTP 200 with a
Payment-Receiptheader. - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pinelabs_online_mpp_client_sdk-0.1.1.tar.gz.
File metadata
- Download URL: pinelabs_online_mpp_client_sdk-0.1.1.tar.gz
- Upload date:
- Size: 29.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b726c5a4f006db28614ef4812f1c44c5784af261874c3aca9420cebbc05a9877
|
|
| MD5 |
c560d502dc3b6ae5c959b30566ece116
|
|
| BLAKE2b-256 |
4ba5b5ea05891beb44b8871b76cea8bd1a5db4f6f835483f7741eee5f8e4a515
|
File details
Details for the file pinelabs_online_mpp_client_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pinelabs_online_mpp_client_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 32.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc60f3038645eabaa187947aa58bedf072dde9550bd8d3d7bafcfc1ae15bd933
|
|
| MD5 |
800c1b9103acdc3aeb7d7ae4843a41ca
|
|
| BLAKE2b-256 |
5a024708b44e64b6be04045a17d98e84a685cf27a12d01cf2954619abf9a0698
|