# Install using pip install "x402[httpx,svm]>=2.29,<2.30" # Run without arguments to review the live quote. --pay-to authorizes one 0.002 USDC call. import argparse import asyncio import json import os import httpx from x402 import x402Client from x402.http import x402HTTPClient from x402.mechanisms.svm import KeypairSigner from x402.mechanisms.svm.exact import ExactSvmScheme from x402.schemas import PaymentRequired ENDPOINT = "https://apiacre.com/v1/data/timezone-convert" SOLANA = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp" USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" ATOMIC_AMOUNT = "2000" REQUEST = { "timestamp": "2026-11-23T09:00:00", "source_timezone": "America/New_York", "target_timezone": "Asia/Tokyo", } async def main(pay_to: str | None) -> None: client = x402Client() protocol = x402HTTPClient(client) async with httpx.AsyncClient(follow_redirects=False) as http: response = await http.post(ENDPOINT, json=REQUEST) if response.status_code != 402: raise RuntimeError(f"Expected an unpaid quote, got HTTP {response.status_code}") quote = protocol.get_payment_required_response(response.headers.get) if not isinstance(quote, PaymentRequired) or quote.x402_version != 2: raise RuntimeError("Expected an x402 v2 quote") if quote.resource is None or quote.resource.url != ENDPOINT: raise RuntimeError("The quote identifies a different resource") options = [ option for option in quote.accepts if option.network == SOLANA and option.scheme == "exact" and option.asset == USDC and option.amount == ATOMIC_AMOUNT ] if len(options) != 1: raise RuntimeError("Expected one Solana quote for 0.002 USDC") option = options[0] if pay_to is None: print(json.dumps({ "endpoint": ENDPOINT, "network": SOLANA, "asset": USDC, "recipient": option.pay_to, "priceUsdc": "0.002", }, indent=2)) return if pay_to != option.pay_to: raise RuntimeError("The live recipient differs from the address you approved") signer = KeypairSigner.from_base58(os.environ["SVM_PRIVATE_KEY"]) client.register(SOLANA, ExactSvmScheme(signer)) client.set_spend_controls({"max_amount_per_payment": "$0.002"}) selected = quote.model_copy(update={"accepts": [option]}) payload = await client.create_payment_payload(selected) response = await http.post( ENDPOINT, json=REQUEST, headers=protocol.encode_payment_signature_header(payload), ) response.raise_for_status() receipt = protocol.get_payment_settle_response(response.headers.get) if not receipt.success or receipt.network != SOLANA or not receipt.transaction: raise RuntimeError("The response did not confirm settlement on Solana") if receipt.amount is not None and receipt.amount != ATOMIC_AMOUNT: raise RuntimeError("The receipt amount differs from the approved quote") print(response.json()) print(receipt) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Try API Acre timezone conversion on Solana") parser.add_argument("--pay-to", metavar="RECIPIENT", help="Authorize 0.002 USDC to this recipient") asyncio.run(main(parser.parse_args().pay_to))