Skip to content

Caddy admin API for runtime configuration

Driving Caddy via its admin API for dynamic configuration — endpoints, granular PATCH/PUT, security, and the operational pattern an MSP applies for orchestrator-driven deployments.

Caddy’s admin API is the runtime configuration surface — a localhost HTTP endpoint that lets you read, replace, and granularly mutate the running config. It is how containers, orchestrators, and service meshes drive Caddy without restarts. This article is the reference we hand to clients building automation around Caddy, and the security pattern we apply to keep the admin endpoint from becoming the attack surface.

How to verify

Confirm the admin API is listening and responds:

ss -ltnp | grep :2019
curl -s http://localhost:2019/config/ | jq . | head
curl -s http://localhost:2019/pki/ca/local | jq .
curl -s http://localhost:2019/reverse_proxy/upstreams | jq .
sudo journalctl -u caddy --since '1m ago' | grep -i admin

The ss should show Caddy listening on 127.0.0.1:2019. The /config/ endpoint returns the entire active JSON config — your source of truth for what is running.

What’s happening

Every Caddy process exposes an admin endpoint on 127.0.0.1:2019 by default. The endpoint is HTTP (not HTTPS — bind to localhost only), and it accepts standard REST verbs against paths that mirror the JSON config tree. The root path is /config/, and every JSON key becomes a path segment:

  • GET /config/ — return the whole config.
  • GET /config/apps/http/servers/srv0/listen — return one field.
  • POST /load — replace the whole config (body is the new JSON).
  • PUT /config/apps/http/servers/srv0/listen — replace one field.
  • PATCH /config/apps/http/servers/srv0/listen/0 — patch one element of an array.
  • DELETE /config/apps/http/servers/srv0/routes/2 — remove the third route.
  • POST /config/apps/http/servers/srv0/routes — append to an array.

This addressing scheme is what makes dynamic configuration practical. An orchestrator does not need to know the rest of the config to add a route — it knows the path apps/http/servers/srv0/routes and POSTs a single route object.

Beyond /config/, the API exposes several read-only diagnostic endpoints:

  • GET /pki/ca/local — the local PKI’s root cert (Caddy’s internal CA for issuing client certs).
  • GET /reverse_proxy/upstreams — current upstream health status per registered reverse proxy.
  • GET /id/<config-id> — fetch a config object by its @id tag (useful for cross-referencing).

The admin endpoint runs even with a Caddyfile-loaded config — the Caddyfile is just a different way to load the JSON, and the same JSON is what the API serves.

Reference

Boot-time vs runtime. A Caddyfile is parsed at boot and loaded as JSON. A POST /load replaces the JSON, including everything the Caddyfile contributed. If you mix Caddyfile-driven and API-driven, accept that any API call wins — the Caddyfile contents are lost on the next POST /load.

Granular updates. The PATCH/PUT/DELETE/POST scheme lets you change just one piece without re-sending the whole config. For an orchestrator adding a tenant route:

curl -X POST http://localhost:2019/config/apps/http/servers/srv0/routes/... \
  -H 'Content-Type: application/json' \
  -d '{
    "match": [{ "host": ["tenant42.example.com"] }],
    "handle": [{ "handler": "reverse_proxy", "upstreams": [{ "dial": "tenant42-backend:3000" }] }]
  }'

The routes/... path is the special “append to the end” indicator (a literal ... segment). You can also POST to routes/0 to insert at position 0, or PUT to routes/2 to replace.

Config IDs for cross-referencing. Adding @id: "tenant42" to a route object lets you fetch or modify it by ID:

curl http://localhost:2019/id/tenant42
curl -X DELETE http://localhost:2019/id/tenant42

This is how orchestrators avoid the brittleness of position-based addressing. Each tenant route gets a stable ID, mutations target the ID.

Configuration adapters. POST /load can accept JSON directly, or it can accept a Caddyfile or other format if you set the right Content-Type and adapter:

caddy adapt --config /etc/caddy/Caddyfile | \
  curl -X POST http://localhost:2019/load \
       -H 'Content-Type: application/json' \
       --data-binary @-

This is what caddy reload does internally — adapt then POST.

The procedure

  1. Confirm the admin endpoint is listening on localhost only. This is the default; if you have changed the config, verify:

    ss -ltnp | grep :2019
    # tcp LISTEN 0 4096 127.0.0.1:2019 — good
    # tcp LISTEN 0 4096 0.0.0.0:2019    — bad

    If it is bound to 0.0.0.0, anyone on the network can replace your Caddy config. Bind to localhost or to a private interface, not the public one.

  2. Add an @id to every object you might mutate. A tenant route, an upstream, a TLS config:

    {
      "@id": "tenant42",
      "match": [{ "host": ["tenant42.example.com"] }],
      "handle": [{ "handler": "reverse_proxy", "upstreams": [{ "dial": "tenant42-backend:3000" }] }]
    }

    Now your orchestrator can mutate by ID without parsing the full config.

  3. Write a thin wrapper for your orchestrator. Most orchestration tools do not speak Caddy admin natively; a small CLI or library makes it ergonomic:

    #!/usr/bin/env bash
    # caddy-tenant-add.sh — usage: caddy-tenant-add.sh tenant42 tenant42-backend:3000
    
    tenant="$1"
    upstream="$2"
    
    curl -fsS -X POST http://localhost:2019/config/apps/http/servers/srv0/routes/... \
      -H 'Content-Type: application/json' \
      -d "$(jq -n --arg t "$tenant" --arg u "$upstream" '
        { "@id": $t,
          "match": [{ "host": [ "\($t).example.com" ] }],
          "handle": [{ "handler": "reverse_proxy", "upstreams": [{ "dial": $u }] }]
        }')"
  4. Capture admin calls in audit logging. Caddy logs admin calls to its main log if you set the log level to INFO or below. For production, pipe these into your audit pipeline — a POST /load is a config change that should be reviewable in retrospect.

  5. Snapshot config before mutation. Critical change? Pull the current config first:

    curl -s http://localhost:2019/config/ > /tmp/caddy-config-pre-change.json
    # do the change
    diff <(jq -S . /tmp/caddy-config-pre-change.json) \
         <(curl -s http://localhost:2019/config/ | jq -S .)

    The diff is what you actually changed. Keep it in change tickets.

Operational notes

  • The admin endpoint speaks HTTP, not HTTPS. Binding it to a public interface exposes you to anyone who can route to that IP. Always bind localhost; reach the admin from outside via a tunnel or SSH-forwarded port.
  • A failed POST /load does not abort the running config — Caddy validates the new config before applying, and reverts on failure. Watch the response body for the validation error message.
  • Concurrent mutations are serialized internally. Two orchestrators racing to mutate the same path get sequential application — the later writer wins. For complex coordinated changes, hold a lock at the application layer.
  • The /load POST is atomic — the new config replaces the old in one swap. Per-field PATCH/PUT/DELETE are also atomic individually. Multi-field changes need either a full /load or an explicit transaction primitive (Caddy does not have one — use /load).
  • Restarting Caddy via systemctl restart caddy clears any state that was added only via the admin API and not persisted to disk. Persistence is your responsibility — write the desired state to a file and reload from it on startup, or use the caddy-api systemd unit which boots from /etc/caddy/caddy.json.

Stack Harbor wires the Caddy admin API into client orchestration platforms — tenant routes added on customer signup, upstream pool membership tracked against the service mesh, and every mutation captured in the audit pipeline. If you have a SaaS or multi-tenant setup where Caddy needs to be driven by code rather than hand-edited configs, this is the kind of work covered by our Managed Operations practice.