Skip to main content

Client

from pytonapi.rest import TonapiRestClient
from pytonapi.types import Network

tonapi = TonapiRestClient("YOUR_API_KEY", Network.MAINNET)
ParameterDefaultDescription
api_keyOptionalAPI key or list of keys for rotation.
networkMainnetMainnet, Testnet, or Tetra.
base_urlOptionalCustom base URL (overrides network selection).
timeout10 secondsRequest timeout.
sessionOptionalExternal HTTP session to share across clients.
headersOptionalExtra HTTP headers sent with every request.
cookiesOptionalExtra cookies sent with every request.
rps_limitOptionalClient-side rate limit (requests per second).
rps_periodOptionalRate-limiter window in seconds.
retry_policyEnabledRetry policy. Disable by passing None.
Without an API key the SDK auto-limits to ~1 request per 4 seconds on the client side to match the server-side throttle. With a key, client-side limiting is off unless set explicitly. Get a key at tonconsole.com.

Session

async with TonapiRestClient("YOUR_API_KEY", Network.MAINNET) as tonapi:
    account = await tonapi.accounts.get_account(account_id="EQ...")

Retries

Rate-limited (429) and server-error responses are retried automatically with exponential backoff.
Status codesMax retriesBase delayMax delayBackoff
42950.3 s3 s2x
500, 502, 503, 50430.5 s5 s2x
Custom policy:
from pytonapi.types import RetryPolicy, RetryRule

tonapi = TonapiRestClient(
    "YOUR_API_KEY",
    Network.MAINNET,
    retry_policy=RetryPolicy(rules=(
        RetryRule(
            statuses=frozenset({429}),
            max_retries=10,
            base_delay=0.5,
            max_delay=10.0,
            backoff_factor=2.0,
        ),
    )),
)
FieldDefaultDescription
statusesRequiredHTTP status codes that trigger retry.
max_retries3Maximum retry attempts.
base_delay1 secondInitial delay.
max_delay30 secondsUpper bound after backoff.
backoff_factor2.0Multiplier per attempt.
The defaults above apply when creating a custom RetryRule. The SDK’s built-in policy uses tuned values shown in the table above.
Pass retry_policy=None to disable retries entirely.

Key Rotation

Pass a list of ApiKey objects to rotate between keys on rate-limit errors (429). Each key carries its own client-side rate limit.
from pytonapi.types import ApiKey

async with TonapiRestClient(
    api_key=[
        ApiKey("key-1", rps_limit=5),
        ApiKey("key-2", rps_limit=10),
    ],
    network=Network.MAINNET,
) as tonapi:
    ...
A single ApiKey also works — rate limit is taken from the object instead of the constructor rps_limit parameter:
async with TonapiRestClient(api_key=ApiKey("my-key", rps_limit=5)) as tonapi:
    ...
On 429, the SDK exhausts all retry attempts for the current key (with backoff), then rotates to the next key and retries with a fresh cycle. Each key uses its own RateLimiter.