Client setup
Construct and configure the synchronous Rank and asynchronous AsyncRank clients, including auth, timeouts, retries and custom headers.
Everything in the SDK starts from a client. There are two: rank.Rank for synchronous code and
rank.AsyncRank for asyncio. They expose the same resources and accept the same constructor
arguments — the async client simply returns awaitables.
Rank vs. AsyncRank
Pick the client that matches your runtime. Resource methods, parameters and return types are identical; only the call style differs.
import rank
client = rank.Rank(api_key="rk_live_xxxxxxxxxxxxxxxx")
pentests = client.pentests.list()
for p in pentests.items:
print(p.name, p.status) import asyncio
import rank
async def main():
client = rank.AsyncRank(api_key="rk_live_xxxxxxxxxxxxxxxx")
pentests = await client.pentests.list()
for p in pentests.items:
print(p.name, p.status)
await client.close()
asyncio.run(main()) Constructor options
Both clients take the same keyword-only arguments:
| Argument | Type | Default | Description |
|---|---|---|---|
api_key | str | RANK_API_KEY env var | API key (rk_...). Required — passed explicitly or via the environment. |
base_url | str | https://api.aleex-rank.ai | PHP REST backend. Falls back to RANK_BASE_URL. |
agent_base_url | str | https://aleex.aleex-rank.ai | Go agent backend for AI/streaming. Falls back to RANK_AGENT_BASE_URL. |
timeout | float | 60.0 | Per-request timeout in seconds. |
max_retries | int | 2 | Automatic retries on network errors and 5xx responses. |
custom_headers | dict[str, str] | None | Extra headers added to every request to both backends. |
import rank
client = rank.Rank(
api_key="rk_live_xxxxxxxxxxxxxxxx",
base_url="https://api.aleex-rank.ai",
agent_base_url="https://aleex.aleex-rank.ai",
timeout=60.0,
max_retries=2,
custom_headers={"X-Request-Source": "ci-pipeline"},
)
Authentication
Rank authenticates every request with an API key sent in the X-API-Key header. You can
provide the key in two ways:
import rank
# Explicit argument
client = rank.Rank(api_key="rk_live_xxxxxxxxxxxxxxxx")
# Or read RANK_API_KEY from the environment
# export RANK_API_KEY=rk_live_xxxxxxxxxxxxxxxx
client = rank.Rank() If no key is found — neither passed nor present in RANK_API_KEY — the constructor raises a
ValueError immediately, before any request is made. API keys are created from the Rank
dashboard; see Authentication for the full walkthrough.
Environment variables
| Variable | Purpose | Default |
|---|---|---|
RANK_API_KEY | API key used for both backends | — |
RANK_BASE_URL | Override the PHP REST backend | https://api.aleex-rank.ai |
RANK_AGENT_BASE_URL | Override the Go agent backend | https://aleex.aleex-rank.ai |
Explicit constructor arguments always take precedence over environment variables.
Timeouts and retries
timeout bounds each individual request, and max_retries controls how many times the client
retries transient failures (connection errors, timeouts and 5xx responses). Validation errors
and other 4xx responses are not retried — they are raised immediately as typed exceptions.
import rank
# A patient client for long pentest operations, with more aggressive retries
client = rank.Rank(timeout=120.0, max_retries=4)
Some long-running calls accept a per-call timeout that overrides the client default. For
example, client.pentests.process_vulnerabilities(...) defaults to a 600-second timeout because
the AI analyses every recorded response.
Custom headers
Use custom_headers to tag traffic from a specific environment or to satisfy a gateway in front
of your deployment. These headers are merged into every request to both the PHP and Go backends.
import rank
client = rank.Rank(
custom_headers={
"X-Request-Source": "ci-pipeline",
"X-Environment": "staging",
},
)
Closing clients
Each client holds pooled HTTP connections to both backends. Close them when you are done to release the sockets. The cleanest approach is the context-manager form, which closes automatically on exit.
import rank
# Automatic cleanup
with rank.Rank() as client:
client.pentests.list()
# Manual cleanup for a long-lived client
client = rank.Rank()
try:
client.pentests.list()
finally:
client.close() import rank
# Automatic cleanup
async with rank.AsyncRank() as client:
await client.pentests.list()
# Manual cleanup for a long-lived client
client = rank.AsyncRank()
try:
await client.pentests.list()
finally:
await client.close() With a client configured, continue to the Resources tour or jump straight to Streaming.