Skip to main content

Official Python SDK for the Rerout branded-link API.

Project description

rerout

Official Python SDK for the Rerout API.

Branded link infrastructure on Cloudflare. Create short links, render QR codes, read analytics, and verify webhook signatures.

Install

pip install rerout
# or
uv add rerout
# or
poetry add rerout

Requires Python 3.10+.

Usage

from rerout import Rerout, CreateLinkInput

rerout = Rerout(api_key="rrk_...")

link = rerout.links.create(
    CreateLinkInput(
        target_url="https://example.com/q4-sale",
        domain_hostname="go.brand.com",
        code="q4",
    )
)
print(link.short_url)  # https://go.brand.com/q4

stats = rerout.project.stats(days=7)
print(f"Last 7 days: {stats.total_clicks} clicks, {stats.qr_scans} QR scans")

The client can be used as a context manager to ensure the underlying HTTP connection pool is closed:

with Rerout(api_key="rrk_...") as rerout:
    link = rerout.links.get("q4")

API

Construction

from rerout import Rerout

rerout = Rerout(
    api_key="rrk_...",                  # required
    base_url="https://api.rerout.co",   # optional, defaults shown
    timeout=30.0,                       # optional, seconds
    client=None,                        # optional, inject your own httpx.Client
)

Inject a custom httpx.Client for proxies, retries, or shared connection pools. The SDK does not close clients it didn't create.

Links

from rerout import CreateLinkInput, UpdateLinkInput

rerout.links.create(CreateLinkInput(target_url=..., domain_hostname=..., code=...))
rerout.links.list(cursor=None, limit=None)
rerout.links.get(code)
rerout.links.update(code, UpdateLinkInput(target_url=...))
rerout.links.delete(code)
rerout.links.stats(code, days=30)

Every Link carries a read-only tags tuple — each Tag has an id, name, and color. Tags are populated on get, list, and update responses (empty on create), and cannot be written through the SDK; the API ignores tag writes for API-key clients.

link = rerout.links.get("q4")
for tag in link.tags:
    print(tag.name, tag.color)

UpdateLinkInput uses an UNSET sentinel to distinguish "leave this field alone" from "clear this field on the server" — pass None to clear, or just don't pass the field at all:

from rerout import UpdateLinkInput

# Clear the expiry, leave everything else untouched
rerout.links.update("q4", UpdateLinkInput(expires_at=None))

# Disable the link without touching SEO
rerout.links.update("q4", UpdateLinkInput(is_active=False))

Calling update with an UpdateLinkInput where no field has been set raises ReroutError(code='bad_request') without hitting the API.

Project

rerout.project.stats(days=30)  # -> ProjectStats
rerout.project.me()            # -> ProjectInfo

QR codes

from rerout import QrOptions

# Pure URL builder — no network call
rerout.qr.url("q4", QrOptions(size=12, ecc="H", domain="go.brand.com"))

# Fetches the rendered SVG with the bearer token attached
svg = rerout.qr.svg("q4", QrOptions(refresh=True))

QR options:

Field Type Notes
size int Module size in px. 1–32. Server default 8.
margin int Quiet zone in modules. 0–16. Server default 4.
ecc 'L' | 'M' | 'Q' | 'H' Error correction level.
domain str Force the QR to encode a specific verified custom domain.
refresh str | bool Cache-bust on regenerate. True is sent as 1; strings pass through.

Webhook signature verification

from rerout import verify_rerout_signature

ok = verify_rerout_signature(
    raw_body=request.body.decode(),
    signature_header=request.headers["x-rerout-signature"],
    secret=settings.REROUT_WEBHOOK_SECRET,
)
if not ok:
    return Response(status=401)

Defaults to a 5-minute timestamp tolerance; pass tolerance_seconds=0 to disable that check.

Error handling

Every method raises ReroutError on failure:

from rerout import Rerout, ReroutError, CreateLinkInput

try:
    rerout.links.create(CreateLinkInput(target_url="http://insecure.example"))
except ReroutError as err:
    print(err.code)     # 'bad_target_url'
    print(err.status)   # 400
    print(err.message)  # 'target_url must use https.'
    if err.is_rate_limited:
        ...  # back off

Synthetic codes when the server didn't return a JSON body: network_error, timeout, unexpected_response, unauthorized, forbidden, not_found, rate_limited, server_error, client_error, missing_api_key, bad_request.

Local development

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest
mypy src
ruff check src tests
ruff format --check src tests

License

MIT — see LICENSE in the workspace root.

Repository

https://github.com/ModestNerds-Co/rerout-sdks

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rerout-0.2.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rerout-0.2.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file rerout-0.2.0.tar.gz.

File metadata

  • Download URL: rerout-0.2.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rerout-0.2.0.tar.gz
Algorithm Hash digest
SHA256 eb7118b941a4aa403b73cbe8a84f8f10808d8072f0f772fa007a3ad3b494a4f2
MD5 e24df6c2e6fede1d44c69637bb31042c
BLAKE2b-256 0189efa333119ac79740855ca2ddda6e9709d9ca2f654715b10bce0f3d68d116

See more details on using hashes here.

File details

Details for the file rerout-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rerout-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rerout-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea425c19207760c893f30c66d62d6c4ec8f2796704e57f2ec6f294123924f695
MD5 ae02d82b38e6cc0323eb63a07fb1d81c
BLAKE2b-256 6705f5e4804772a76c5cdd679d69a5ead5ab546be5569f23e239e0fb4cf50e80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page