Environment Variables
Environment Variables and Secrets work slightly differently in Cloudflare deployed projects.
Instead of a .env
file, you use the .dev.vars
file to store your secrets and environment variables.
The .dev.vars
works exactly like a .env
file in terms of syntax, so if you have a .env
file in your project, you can simply rename it to .dev.vars
and it will work as expected.
If you call a variable like process.env.MY_SECRET
in your frontend facing
code, it will not be hidden in the browser, regardless of whether it is
prefixed with NEXT_PUBLIC_
or not
Preview and Production Environment
Secrets
Follow the guide here to add secrets to your project in the Cloudflare dashboard: Adding Secrets (opens in a new tab)
Variables
To add variables to your project, you can add them to the wrangler.toml
file like this:
[vars]
MY_VARIABLE = "my-value"
On your next deployment, the variables will be available in your project.