Python SDK for the Space Router residential proxy network
Project description
SpaceRouter Python SDK
Python SDK for routing HTTP requests through the Space Router residential proxy network.
v1.5 testnet — payment is now done by depositing SPACE into the on-chain
TokenPaymentEscrowand signing per-request EIP-712 receipts. The legacysr_live_*API key flow is still supported on production until v1.5 ships there; on testnet it has been retired. See the migration appendix at the bottom of this document.
Installation
pip install spacerouter
pip install spacerouter-cli # bundles the `spacerouter` command
The SDK depends on eth-account and web3 for EIP-712 signing and
RPC calls; both are pulled in automatically.
Quickstart (testnet, escrow flow)
This six-step path takes you from a fresh wallet to a paid proxied
request and back to a fully settled Leg 1 receipt. Every step is a
runnable shell snippet using only the bundled spacerouter CLI and
the SDK.
1. Set environment
# Testnet defaults — see internal-docs/v1.5-consumer-protocol.md §1.
export SR_GATEWAY_URL="https://your-gateway.example.com"
export SR_GATEWAY_MANAGEMENT_URL="https://your-gateway.example.com"
export SR_ESCROW_CHAIN_RPC="https://rpc.cc3-testnet.creditcoin.network"
export SR_ESCROW_CONTRACT_ADDRESS="0xC5740e4e9175301a24FB6d22bA184b8ec0762852"
export SR_ESCROW_CHAIN_ID="102031"
# Wallet — generate or import. Never commit this.
export SR_ESCROW_PRIVATE_KEY="0x..."
2. Fund a testnet wallet
You need both:
- native CTC on Creditcoin testnet (for gas) — use the team faucet.
- mock SPACE tokens (the ERC-20 the escrow charges in) — minted from
0x7395953AfBD4F33F05dBadCf32e045B3dd1a62FA. Ask in Slack to be topped up if your balance reads zero.
# Confirm balances.
spacerouter escrow token-balance "$(python -c 'import os; from eth_account import Account; \
print(Account.from_key(os.environ["SR_ESCROW_PRIVATE_KEY"]).address)')"
3. Approve the escrow as ERC-20 spender
A one-shot allowance covers many deposits.
# Allow the escrow to pull up to 100 SPACE on your behalf.
spacerouter escrow approve 100000000000000000000
escrow deposit will auto-approve when allowance is short, but
splitting the two transactions is cleaner for hardware-wallet
workflows or when you want a single large approve(2**256-1).
4. Deposit SPACE into escrow
# 10 SPACE in wei.
spacerouter escrow deposit 10000000000000000000
# Verify.
spacerouter escrow balance "$(python -c 'import os; from eth_account import Account; \
print(Account.from_key(os.environ["SR_ESCROW_PRIVATE_KEY"]).address)')"
5. Make a paid proxy request
spacerouter request get https://httpbin.org/ip --pay
--pay swaps the legacy API-key flow for the escrow-signed flow:
the CLI pulls a fresh challenge from
{gateway}/auth/challenge, attaches the four
X-SpaceRouter-Payment-* / X-SpaceRouter-Challenge-* headers, and
proxies the request. Add --region US etc. as before.
6. Sync Leg 1 receipts
After each paid request the gateway parks an unsigned Leg 1 receipt addressed to your wallet. You sign it with EIP-712 and submit it back via the broker.
# One-shot: list, sign, submit.
spacerouter receipts sync
# Or in one step alongside the request:
spacerouter request get https://httpbin.org/ip --pay --auto-settle
# Long-running settler that drains the queue every 30 s.
spacerouter receipts sync --watch 30
# Just look at what's pending without signing.
spacerouter receipts pending --json
Programmatic SDK
import asyncio
from spacerouter import SpaceRouter
from spacerouter.payment import SpaceRouterSPACE
PROXY = "https://your-gateway.example.com"
GATEWAY_MGMT = PROXY # same host, separate routes
ESCROW = "0xC5740e4e9175301a24FB6d22bA184b8ec0762852"
async def main(private_key: str):
consumer = SpaceRouterSPACE(
gateway_url=GATEWAY_MGMT,
proxy_url=PROXY,
private_key=private_key,
chain_id=102031,
escrow_contract=ESCROW,
)
challenge = await consumer.request_challenge()
headers = consumer.build_auth_headers(challenge)
with SpaceRouter(consumer.address.lower(), gateway_url=PROXY) as cli:
resp = cli.get("https://httpbin.org/ip", headers=headers)
print(resp.json())
# Settle the Leg 1 receipt the gateway just parked.
print(await consumer.sync_receipts())
asyncio.run(main("0x..."))
The SpaceRouterSPACE client validates received receipts against your
local byte count, signs only after validation
(sign_receipt_after_validation), and exposes
sync_receipts() as a convenience wrapper around the Leg 1 broker.
CLI cheat sheet
| Command | What it does |
|---|---|
spacerouter escrow balance <addr> |
Read on-chain escrow balance. |
spacerouter escrow token-balance <addr> |
Read undeposited SPACE balance. |
spacerouter escrow approve <wei> [--token ADDR] |
One-shot ERC-20 allowance for the escrow. |
spacerouter escrow deposit <wei> |
Deposit SPACE; auto-approves if needed. |
spacerouter escrow initiate-withdrawal <wei> |
Start the 5-day withdrawal timer. |
spacerouter escrow execute-withdrawal |
Pull funds out after the delay. |
spacerouter receipts pending [--json] [--limit N] |
List unsigned Leg 1 receipts. |
spacerouter receipts sync [--json] [--watch SECS] |
Sign all and submit. |
spacerouter receipts list [--client ADDR] [--json] |
Group pending receipts by tunnel. |
spacerouter receipts is-settled <client> <uuid> |
Check on-chain claim state. |
spacerouter request get <url> --pay |
Paid proxied GET. |
spacerouter request get <url> --pay --auto-settle |
Pay + settle Leg 1 in one step. |
For deeper troubleshooting (chain ID mismatch, allowance bugs,
EIP-712 signer mismatch, NTP clock skew, etc.) see
docs/consumer-quickstart.md.
Region Targeting
client = SpaceRouter(payer_address, region="US")
jp_client = client.with_routing(region="JP")
SOCKS5 Proxy
client = SpaceRouter(
payer_address,
protocol="socks5",
gateway_url="socks5://gateway:1080",
)
Requires the socks extra: pip install spacerouter[socks].
Error Handling
from spacerouter.exceptions import (
AuthenticationError, # 407 - bad payment auth
RateLimitError, # 429
NoNodesAvailableError, # 503
UpstreamError, # 502
)
try:
response = client.get("https://example.com")
except RateLimitError as e:
print(f"Rate limited, retry after {e.retry_after}s")
HTTP errors from the target website (404, 500, etc.) are not raised as exceptions — only proxy-layer errors are.
Configuration
| Parameter | Default | Description |
|---|---|---|
gateway_url |
https://gateway.spacerouter.org |
Proxy gateway URL (CONNECT) |
protocol |
http |
http or socks5 |
region |
None |
2-letter country code (ISO 3166-1 alpha-2) |
timeout |
30.0 |
Request timeout in seconds |
Escrow-mode payment is configured via env vars:
SR_ESCROW_PRIVATE_KEY, SR_ESCROW_CONTRACT_ADDRESS,
SR_ESCROW_CHAIN_RPC, SR_ESCROW_CHAIN_ID,
SR_GATEWAY_MANAGEMENT_URL.
Migration from v1.4 (api-key)
The legacy API-key flow (sr_live_* keys passed via --api-key /
SR_API_KEY / the SpaceRouter("sr_live_…") positional argument) is
dead on testnet as of v1.5. On production it still works until v1.5
ships there. Plan your migration:
- Generate a wallet keypair (any Ethereum-style 32-byte secp256k1 key) and fund it with native CTC + mock SPACE on testnet.
- Replace
SpaceRouter("sr_live_…")with the escrow-signed flow shown above. The CLI flag is--pay; theSpaceRouterSDK is happy to accept the wallet address (lowercase 0x-hex) in place of an API key provided the request carries the fourX-SpaceRouter-*headers thatSpaceRouterSPACE.build_auth_headers()produces. - Once v1.5 ships to production, the API-key flow on prod will be retired the same way. Until then you can keep two code paths or use the wallet flow on testnet only.
The v1.5 protocol contract (EIP-712 domain, broker auth message, wire
formats) is locked at
internal-docs/v1.5-consumer-protocol.md. SDKs MUST produce
byte-identical signatures for the canonical test vector in §7.
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 spacerouter-1.5.0rc5.tar.gz.
File metadata
- Download URL: spacerouter-1.5.0rc5.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae9ae5fdd8b043644f67d960a6177a522fda2e628e34eced1f3fa56ace213c7f
|
|
| MD5 |
04ec71e9c97e8cdcde7021e5ad367ee8
|
|
| BLAKE2b-256 |
505a67f7f5b99b51b8397a7ae421310db9fc5c107340b2f79b2f8030ac2412c0
|
Provenance
The following attestation bundles were made for spacerouter-1.5.0rc5.tar.gz:
Publisher:
publish.yml on space-labs/space-router-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spacerouter-1.5.0rc5.tar.gz -
Subject digest:
ae9ae5fdd8b043644f67d960a6177a522fda2e628e34eced1f3fa56ace213c7f - Sigstore transparency entry: 1439238174
- Sigstore integration time:
-
Permalink:
space-labs/space-router-sdk@a1ce00f7db91119870a9118943bab1f3beed14cd -
Branch / Tag:
refs/tags/v1.5.0-rc.5 - Owner: https://github.com/space-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a1ce00f7db91119870a9118943bab1f3beed14cd -
Trigger Event:
push
-
Statement type:
File details
Details for the file spacerouter-1.5.0rc5-py3-none-any.whl.
File metadata
- Download URL: spacerouter-1.5.0rc5-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5423cefbe4f3d4ff732ccf0fc029f7b11a7e96c36ac624b5db37b4c3930b563d
|
|
| MD5 |
80cc5ee4d3185d3d39146efcc22533e6
|
|
| BLAKE2b-256 |
40eeb8398655174290dd0ed9b66b6d36eaa0552bb3352790d5756ee306c2971b
|
Provenance
The following attestation bundles were made for spacerouter-1.5.0rc5-py3-none-any.whl:
Publisher:
publish.yml on space-labs/space-router-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spacerouter-1.5.0rc5-py3-none-any.whl -
Subject digest:
5423cefbe4f3d4ff732ccf0fc029f7b11a7e96c36ac624b5db37b4c3930b563d - Sigstore transparency entry: 1439238186
- Sigstore integration time:
-
Permalink:
space-labs/space-router-sdk@a1ce00f7db91119870a9118943bab1f3beed14cd -
Branch / Tag:
refs/tags/v1.5.0-rc.5 - Owner: https://github.com/space-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a1ce00f7db91119870a9118943bab1f3beed14cd -
Trigger Event:
push
-
Statement type: