Skip to main content

Traefik API

Updated Feb 05, 2023 ·

Overview

The Traefik API gives you access to Traefik’s configuration and status. It is enabled by default in the static Traefik configuration but is often underused.

  • Can be accessed through a URL like traefik.localhost/api
  • Provides data about routers, services, middlewares, and more
  • Full API paths are needed to get proper results (e.g., /api/http/routers)
  • Lets you monitor and manage Traefik programmatically.

Security Considerations

The Traefik API exposes critical configuration details, so it should be used carefully.

  • Best used only in development or test environments
  • If enabled in production, always secure with authorization middleware
  • Restrict access by IP to trusted networks only

If the Traefik API is enabled in production, make sure you it secure with authorization middleware.

Traefik API Endpoints

The Traefik API lets you view and manage your Traefik setup through various endpoints. These endpoints provide information about routers, services, middlewares, and more.

EndpointDescription
/api/http/routersList all HTTP routers
/api/http/servicesShow all HTTP services
/api/http/middlewaresDisplay middlewares
/api/tcp/routersList TCP routers
/api/tcp/servicesShow TCP services
/api/udp/routersList UDP routers
/api/udp/servicesShow UDP services

You can access these endpoints via HTTP requests. For example, to get all HTTP routers:

curl http://traefik.localhost/api/http/routers

This will return a JSON response with details about your HTTP routers.

Lab: Viewing APIs

Github repo: joseeden/labs-traefik

This lab shows how to view Traefik API information using its HTTP endpoints.

To do the lab, clone the project repository from GitHub.

git clone https://github.com/joseeden/labs-traefik.git
cd labs-traefik/07-operations/01-traefik-api

Project structure:

07-operations
└── 01-traefik-api
├── docker-compose.yml
└── traefik.yml

Deploy the stack:

docker stack deploy -c docker-compose.yml traefik

To list routers:

curl -s http://localhost:8080/api/http/routers | jq 

Expected output:

[
{
"entryPoints": [
"traefik"
],
"service": "api@internal",
"rule": "PathPrefix(`/api`)",
"priority": 2147483646,
"status": "enabled",
"using": [
"traefik"
],
"name": "api@internal",
"provider": "internal"
},
{
"entryPoints": [
"web"
],
"service": "catapp",
"rule": "Host(`catapp.localhost`)",
"status": "enabled",
"using": [
"web"
],
"name": "catapp@docker",
"provider": "docker"
},
{
"entryPoints": [
"traefik"
],
"middlewares": [
"dashboard_redirect@internal",
"dashboard_stripprefix@internal"
],
"service": "dashboard@internal",
"rule": "PathPrefix(`/`)",
"priority": 2147483645,
"status": "enabled",
"using": [
"traefik"
],
"name": "dashboard@internal",
"provider": "internal"
}
]

To list services:

curl -s http://localhost:8080/api/http/services | jq 

Output:

[
{
"status": "enabled",
"usedBy": [
"api@internal"
],
"name": "api@internal",
"provider": "internal"
},
{
"loadBalancer": {
"servers": [
{
"url": "http://10.0.1.3:5000"
}
],
"passHostHeader": true
},
"status": "enabled",
"usedBy": [
"catapp@docker"
],
"serverStatus": {
"http://10.0.1.3:5000": "UP"
},
"name": "catapp@docker",
"provider": "docker",
"type": "loadbalancer"
},
{
"status": "enabled",
"usedBy": [
"dashboard@internal"
],
"name": "dashboard@internal",
"provider": "internal"
},
{
"status": "enabled",
"name": "noop@internal",
"provider": "internal"
}
]