Skip to main content

traefik

Async Python client for the Traefik API.

Read-only: version, the dashboard overview, entrypoints, HTTP routers and services, and — optionally — the handful of useful numbers from the Prometheus metrics endpoint.

Written for HA-Traefik, but it has no Home Assistant dependency and works anywhere.

Install

pip install traefik

Use

import asyncio

from traefik import TraefikClient


async def main() -> None:
    async with TraefikClient("http://192.0.2.10:8080") as client:
        info = await client.get_version()
        print(info.version, info.codename)

        overview = await client.get_overview()
        print(overview.http_routers.total, "routers")
        print(overview.http_routers.errors, "with errors")

        for router in await client.list_routers():
            print(router.name, router.rule, router.status)


asyncio.run(main())

Authentication

Traefik's API has no authentication of its own. It is normally protected by an ipAllowList or a basicAuth middleware. For the second case, pass credentials:

TraefikClient("https://traefik.example.com", username="admin", password="…")

For the first, the host running this code has to be inside the allowed range — there is nothing the client can do about it. Either way a refusal raises TraefikAuthenticationError.

Metrics

Traefik's Prometheus exporter usually lives on its own entrypoint, on a different port from the API, so it is configured separately and is off unless you ask for it:

client = TraefikClient(
    "http://192.0.2.10:8080",
    metrics_url="http://192.0.2.10:8082",
)
metrics = await client.get_metrics()

print(metrics.open_connections)
print(metrics.last_reload)
for cert in metrics.certificates:
    print(cert.common_name, cert.days_remaining, "days left")

Only four metric families are read — open connections, config reloads, last successful reload, and per-certificate expiry. The rest of Traefik's output is per-route latency histograms and is skipped.

A note on serverStatus

Service.all_servers_up returns None, not True, when Traefik is not probing a service's backends. Traefik only marks a server down when the configuration gives that service a loadBalancer.healthCheck; without one it reports every server as up forever, including servers that are switched off.

Note that a populated serverStatus is not evidence of probing — Traefik fills it in either way. Service.has_health_check reads the service's loadBalancer.healthCheck instead, which is the only honest signal.

Errors

All of them derive from TraefikError:

Error Meaning
TraefikConnectionError Could not reach the instance
TraefikAuthenticationError 401/403 — allowlist or credentials
TraefikNotFoundError The endpoint does not exist here
TraefikResponseError The answer was not usable

License

MIT

Release files for traefik 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for traefik 1.1.0
File Size Uploaded
traefik-1.1.0.tar.gz 15.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for traefik 1.1.0
File Interpreter ABI Platform
traefik-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.6 kB

Release files / traefik-1.1.0.tar.gz

Download URL traefik-1.1.0.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2af5dd77d651e5bb4e39293cb580cba045bd07569f308b46fa6d82293d8552f3
BLAKE2b-256 checksum
How to use checksums
492356f5d0360413d65d1ab769ada199703f69af021cc8e1037488c5f7b91759
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / traefik-1.1.0-py3-none-any.whl

Download URL traefik-1.1.0-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b190d72277bf847cdcedde0db45f67e2e025c52db8cc4d555a9517aced2ceb1
BLAKE2b-256 checksum
How to use checksums
ba72fda817ff200b0e045b194ef0710dcdd756870d1687a47607ca09e3328f18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page