Rayley Documentation

httpx

Use a static IP address from Python using httpx.

httpx is a modern HTTP client for Python with async support. It supports proxy configuration through the proxy parameter, with the token embedded in the proxy URL.

Prerequisites

  • A Rayley account with a Proxy Token (rpt_*). Create one in the dashboard.

Installation

pip install httpx

Quick start

Create an httpx.Client with the proxy parameter to route all requests through your static IP address. Embed the token in the proxy URL as basic auth credentials:

proxy_request.py
import httpx

proxy_url = "https://token:rpt_your_token_here@proxy.rayley.com"

with httpx.Client(proxy=proxy_url, timeout=30) as client:
    response = client.get("https://api.example.com/data")

print(response.json())

async client

If you're using asyncio or another async framework, swap Client for AsyncClient. The proxy configuration is identical. httpx handles the rest:

proxy_async.py
import httpx

proxy_url = "https://token:rpt_your_token_here@proxy.rayley.com"

async with httpx.AsyncClient(proxy=proxy_url, timeout=30) as client:
    response = await client.get("https://api.example.com/data")
    print(response.json())

Full example

Below is a command-line script you can use to test your Proxy Token. It reads the token from the RAYLEY_TOKEN environment variable and takes an optional URL argument (defaulting to https://example.com) to make a request using your static IP Address.

example.py
import os
import sys
import httpx

proxy_token = os.environ.get("RAYLEY_TOKEN")
target_url = sys.argv[1] if len(sys.argv) > 1 else "https://example.com/"

if not proxy_token:
    print("Set RAYLEY_TOKEN to your Proxy Token.", file=sys.stderr)
    sys.exit(1)

proxy_url = f"https://token:{proxy_token}@proxy.rayley.com"

with httpx.Client(proxy=proxy_url, timeout=30) as client:
    response = client.get(target_url)

if response.status_code == 407:
    print("Authentication failed. Check your RAYLEY_TOKEN.", file=sys.stderr)
    sys.exit(1)

response.raise_for_status()
print(f"Status: {response.status_code}")
print(response.json())

Run it:

RAYLEY_TOKEN=rpt_your_token python example.py https://example.com/

On this page