Machine Integration Examples
Python Client
import requests
import hmac
import hashlib
import base64
from eth_account import Account
from eth_account.messages import encode_structured_data
class SigweiClient:
def __init__(self, private_key, base_url="https://402ify.com"):
self.account = Account.from_key(private_key)
self.base_url = base_url
self.access_token = None
def authenticate(self):
# Get message to sign
response = requests.get(
f"{self.base_url}/api/v1/auth/message",
params={"walletAddress": self.account.address}
)
message = response.json()["message"]
# Sign message
encoded_msg = encode_structured_data(primitive=message)
signature = self.account.sign_message(encoded_msg)
# Login
auth_response = requests.post(
f"{self.base_url}/api/v1/auth/login",
json={"message": message, "signature": signature.signature.hex()}
)
self.access_token = auth_response.json()["accessToken"]
def create_paygate(self, target_url, method="GET", price="0.01",
network="base-sepolia", require_auth=False, header_auth_mode="hmac", assume_valid=False):
if not self.access_token:
self.authenticate()
response = requests.post(
f"{self.base_url}/api/v1/paygates",
headers={"Authorization": f"Bearer {self.access_token}"},
json={
"targetUrl": target_url,
"method": method,
"price": price,
"network": network,
"paymentAddress": self.account.address,
"requireAuth": require_auth,
"headerAuthMode": header_auth_mode,
"assumeValid": assume_valid
}
)
return response.json()
def get_paygates(self):
if not self.access_token:
self.authenticate()
response = requests.get(
f"{self.base_url}/api/v1/paygates",
headers={"Authorization": f"Bearer {self.access_token}"}
)
return response.json()
def execute_transfer(self, to_address, amount_usdc):
# Generate EIP-712 signature for USDC transfer
nonce = "0x" + secrets.token_hex(32)
valid_before = int(time.time()) + 3600 # 1 hour validity
authorization = {
"from": self.account.address,
"to": to_address,
"value": str(int(amount_usdc * 1e6)), # Convert to USDC wei
"validAfter": "0",
"validBefore": str(valid_before),
"nonce": nonce
}
# Sign with EIP-712 (implementation depends on your signing library)
signature = self.sign_transfer_authorization(authorization)
response = requests.post(
f"{self.base_url}/api/v1/transfer",
json={
"network": "base-sepolia",
"signature": signature,
"authorization": authorization
}
)
return response.json()
# Usage example
client = SigweiClient("0xYOUR_PRIVATE_KEY")
# Create a PayGate
paygate = client.create_paygate(
target_url="https://api.example.com/data",
method="GET",
price="0.05",
require_auth=True,
header_auth_mode="hmac"
)
print(f"PayGate created: {paygate['accessUrl']}")JavaScript/Node.js Client
Go Client
cURL Examples
AI Agent Integration
For AI agents and automated systems, use x402-compatible HTTP libraries that handle payment flows automatically:
Last updated