Caddy Environment with cloudflare issue

1. The problem I’m having:

I am trying to set up an environment variable. I have got it all set up and have my code to what I believe is correct. I’ve triple checked about a thousand times but I continue to get my variable being invalid. I am using caddy with added cloudflare plugin. When I paste the key directly in to the caddy file is the only time it works, otherwise no luck.

2. Error messages and/or full log output:

root@Caddy:/# caddy reload --config /etc/caddy/Caddyfile
2025/07/17 18:01:42.023 INFO    using config from file  {"file": "/etc/caddy/Caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': unknown subdirective: acme_dns, at /etc/caddy/Caddyfile:13
root@Caddy:/# nano /etc/caddy/Caddyfile                 
root@Caddy:/# caddy reload --config /etc/caddy/Caddyfile
2025/07/17 18:02:36.619 INFO    using config from file  {"file": "/etc/caddy/Caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': unknown subdirective: acme_dns, at /etc/caddy/Caddyfile:13
root@Caddy:/# nano /etc/caddy/Caddyfile
root@Caddy:/# caddy reload --config /etc/caddy/Caddyfile
2025/07/17 18:03:23.766 INFO    using config from file  {"file": "/etc/caddy/Caddyfile"}
2025/07/17 18:03:23.767 INFO    adapted config to JSON  {"adapter": "caddyfile"}
Error: sending configuration to instance: caddy responded with error: HTTP 400: **{"error":"loading config: loading new config: loading tls app module: provision tls: provisioning automation policy 1: loading TLS automation management module: position 0: loading module 'acme': provision tls.issuance.acme: loading DNS provider module: loading module 'cloudflare': provision dns.providers.cloudflare: API token '' appears invalid; ensure it's correctly entered and not wrapped in braces nor quotes"}**

3. Caddy version:

v2.10.0 h1:fonubSaQKF1YANl8TXqGcn4IbIRUDdfAkpcsfI/vX5U=

4. How I installed and ran Caddy:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf ‘https://dl.cloudsmith.io/public/caddy/stable/gpg.key’ | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf ‘https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt’ | sudo tee /etc/apt/sources.list.d/caddy-stable.list
chmod o+r /usr/share/keyrings/caddy-stable-archive-keyring.gpg
chmod o+r /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo caddy add-package github.com/caddy-dns/cloudflare

a. System environment:

/etc/systemd/system/caddy.service.d/override.conf
[Service]
Environment=“CF_API_TOKEN=************”

b. Command:

I have ran the export command too with no luck but the command where i'm getting the error is caddy reload --config /etc/caddy/Caddyfile

c. Service/unit/compose file:

[Service]
Environment="CF_API_TOKEN=************"

d. My complete Caddy config:

mydomainishere.com {

tls {

dns cloudflare env.CF_API_TOKEN

resolvers 1.1.1.1

}

reverse_proxy ***.***.*.**

}

5. Links to relevant resources:

NA, I have followed documentation many times, just cannot get it to run.

You’re missing the braces { }. Also, there are 2 ways of using environment variables in config. See here:

1 Like

I swear i’ve tried that and it never worked. I finally got it, thanks

1 Like