Skip to main content
Unified request interface that handles 402 Payment Required responses automatically. First attempts a direct request to the target URL. If it returns 402, automatically handles payment through the appropriate flow (relay or proxy) and returns the resource.

Signature

client.request(method: str, url: str, simulate: bool = False, response_mode: Optional = None, network: Optional = None, kwargs)

Parameters

  • method: HTTP method (GET, POST, PUT, DELETE, etc.).
  • url: Target API URL to access.
  • simulate: If True, only simulate payment without executing (dry-run). Defaults to False.
  • response_mode: Override response format (“text” or “json”). Uses instance default if not specified.
  • network: Override network for relay mode payments.
  • **kwargs: Additional arguments passed to requests (json, params, headers, timeout, etc.).

Returns

Response from the target API. If 402 is encountered, returns response after payment.

Raises

  • ValueError: If network is incompatible with wallet type or not supported.

Examples

>>> client = HTTPayerClient()
>>> # Basic request - auto-handles 402
>>> response = client.request("GET", "https://api.example.com/data")
>>> print(response.json())
>>> # Simulate first to check cost
>>> sim = client.request("GET", "https://api.example.com/data", simulate=True)
>>> print(f"Cost: {sim.json()['relayFeeBreakdown']['totalAmount']}")
>>> # POST with payload
>>> response = client.request(
...     "POST",
...     "https://api.example.com/process",
...     json={"input": "data"}
... )
>>> # Override network for this request
>>> response = client.request("GET", url, network="solana-devnet")