Setting environment variables toml vs ui

Bit of a random question which I am trying it myself and will post an answer if I figure it out. Can you use “$URL/something” in both the UI and the toml file? Which one takes precedence, the UI or build.environment?

If so do you need the " " around it in the UI for the interpolation to replace $URL with the sites main url?

If some context helps I am trying to set two urls, which need to be different for production( production(

So i thought I could set the production one in the UI:
ACCEPT_URL “$URL/accept”
CANCEL_URL “$URL/cancel”

And the none production one in the TOML

Hi Duncan,

First off, I don’t recommend any pattern of trying to play precedence games between the UI and the toml file; the toml file is ideal to handle ALL variables because it has ability to set a default AND a per-branch or build type context:

# this is the default for all build types not defined specifically below
  ACCEPT_URL = "/accept”
  CANCEL_URL = "/cancel”

# for the beta branch only
  ACCEPT_URL = "/staging/accept"
  CANCEL_URL = "/staging/cancel”

Second, you wouldn’t be able to refer to one environment variable from another like that. The values need to be static, and need to be interpolated manually at build time, no matter where you define them. This article has some more details on the successful use of envvars at Netlify:

Specifically, you’ll have to BUILD a value AT BUILD TIME from $URL or $DEPLOY_PRIME_URL plus your path, and you can do contextually different build commands to handle that, something like this (untested but I think it will work)

# default
  command = `export FINAL_ACCEPT_URL=${URL}${ACCEPT_URL} && do-my-build`
  command = `export FINAL_ACCEPT_URL=${DEPLOY_PRIME_URL}${ACCEPT_URL} && do-my-build`

Let me know how it goes!