Use process.env in netlify.toml


I have different sites for different environments.
Staging, UAT and Production.

I’m using netlify.toml for [[redirects]] so it can redirect to the API.

  from = "/v1/*"
  to = "${REACT_APP_API_ENDPOINT}/v1/:splat"
  force = true # COMMENT: ensure that we always redirect
  headers = { X-From = "Netlify" }
  status = 200

The thing is that I cannot have a versioned netlify.toml for every branch because I will have conflicts.

It’s specified in the documentation that env variables don’t work inside netlify.toml [[redirects]].
We can specify contexts and use them inside netlify.toml, but it can only be done with using Netlify CLI (with --context staging, etc).
We have automatic deploys. Inside the Netlify UI there is no section to set contexts based on branches or sites.

I don’t want to use Netlify CLI locally, I want to use the process.env or contexts from Netlify UI based on branches or sites to change the variables dynamically inside netlify.toml.

Is this possible?

Hi @lightningassist , to my knowledge it is not possible to read environment variables at runtime in your netlify.toml file.
If you want to read environment variables in your application during runtime, the best alternative will be to use Netlify functions to run the redirects.

Or depending on your setup, using an .env file is also an option.
But make sure not to commit that file for security purposes!