DEV Community

Alex Spinov
Alex Spinov

Posted on

Caddy Has a Free API — Heres How to Get Automatic HTTPS Without Configuration

Caddy is the only web server that automatically provisions and renews TLS certificates. Zero config HTTPS, HTTP/3, reverse proxy — all through a simple JSON API.

Why Caddy?

  • Automatic HTTPS: TLS certificates from Let's Encrypt, zero config
  • HTTP/3: Built-in QUIC support
  • JSON API: Configure everything at runtime
  • Reverse proxy: Load balancing, health checks
  • Single binary: No dependencies
  • Caddyfile: Human-readable config option

Install

# Debian/Ubuntu
sudo apt install -y caddy

# macOS
brew install caddy

# Docker
docker run -p 80:80 -p 443:443 caddy
Enter fullscreen mode Exit fullscreen mode

Caddyfile (Simple Config)

mysite.com {
  root * /var/www/html
  file_server
}

api.mysite.com {
  reverse_proxy localhost:3000
}
Enter fullscreen mode Exit fullscreen mode

That's it. Caddy automatically gets HTTPS certificates for both domains.

Admin API: Get Config

curl http://localhost:2019/config/
Enter fullscreen mode Exit fullscreen mode

Admin API: Set Config

curl -X POST http://localhost:2019/load \
  -H 'Content-Type: application/json' \
  -d '{
    "apps": {
      "http": {
        "servers": {
          "main": {
            "listen": [":443"],
            "routes": [{
              "match": [{"host": ["mysite.com"]}],
              "handle": [{
                "handler": "reverse_proxy",
                "upstreams": [{"dial": "localhost:3000"}]
              }]
            }]
          }
        }
      }
    }
  }'
Enter fullscreen mode Exit fullscreen mode

Admin API: Add Route at Runtime

curl -X POST http://localhost:2019/config/apps/http/servers/main/routes \
  -H 'Content-Type: application/json' \
  -d '{
    "match": [{"host": ["newsite.com"]}],
    "handle": [{
      "handler": "file_server",
      "root": "/var/www/newsite"
    }]
  }'
Enter fullscreen mode Exit fullscreen mode

Reverse Proxy with Load Balancing

api.mysite.com {
  reverse_proxy {
    to localhost:3001 localhost:3002 localhost:3003
    lb_policy round_robin
    health_uri /health
    health_interval 10s
  }
}
Enter fullscreen mode Exit fullscreen mode

PHP with Caddy

mysite.com {
  root * /var/www/html
  php_fastcgi unix//run/php/php-fpm.sock
  file_server
}
Enter fullscreen mode Exit fullscreen mode

SPA (React/Vue/Angular)

mysite.com {
  root * /var/www/app
  try_files {path} /index.html
  file_server
}
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case

A team replaced Nginx + Certbot + cron (150 lines of config + renewal scripts) with a 5-line Caddyfile. Certificate renewals never failed again — Caddy handles it automatically, including OCSP stapling.


Need to automate data collection? Check out my Apify actors for ready-made scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)