9

I want to proxy to a different api depending on the environment - I've tried a few variations on the following theme without any luck. What's the correct way of doing this, if its even possible?

[build.environment]
  API_URI="https://dev-api.foo.com/:splat"

[context.production.environment]
  API_URI="https://prod-api.foo.com/:splat"

[[redirects]]
  from = "/api/*"
  to = "$API_URI"
  status = 200
  force = true

This does not work.

Although the above config works when I hardcode a URI into the to field, it just fails when I try to interpolate an env var.

Jed Richards
  • 12,244
  • 3
  • 24
  • 35

3 Answers3

8

It's not supported, but Netlify suggest a work-around in their documentation (https://www.netlify.com/docs/netlify-toml-reference):

Using Environment Variables directly as values ($VARIABLENAME) in your netlify.toml file is not supported. However, the following workflow can be used to substitute values based on environment variables in the file, assuming you are only trying to change headers or redirects. The rest of the file is read BEFORE your build - but those sections are read AFTER the build process.

  1. Add a placeholder like API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections.
  2. Create an Build Environment Variable, for example API_KEY, with the desired value. You can do this in the toml file or in our UI in the Build and Deploy Settings section of your configuration. You might use the latter to keep sensitive values out of your repository.
  3. Add a command like this one to your build command: sed -i s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.
oligopol
  • 810
  • 11
  • 17
  • What's the syntax for this? I can't seem to get it to work right now. I've tried `sed -i s/PLACEHOLDER/$VARIABLE/g netlify.toml` and `sed -i s/PLACEHOLDER/${VARIABLE}/g netlify.toml` (currently recommended by the docs) but neither works. – Dylan Gattey Jan 31 '19 at 20:03
  • 1
    In your netlify.toml: ```[context.deploy-preview.environment] SERVER_DOMAIN="blablabla.com" [build] publish = "build" command = "sed -i s/SERVER_DOMAIN_PLACEHOLDER/$SERVER_DOMAIN/g netlify.toml && npm run build" ``` – oligopol Feb 01 '19 at 13:06
1

Answering my own question - it's not supported, you have to manually interpolate env vars yourself as part of the build on Netlify.

Jed Richards
  • 12,244
  • 3
  • 24
  • 35
0

Yes. It's possible. Here is the detailed docs: https://www.netlify.com/docs/continuous-deployment/#deploy-contexts

In my case, I need to set a REACT_APP_API_URL separate for production and all other branches. Here is what I use:

[context.production.environment]
  REACT_APP_API_URL = "https://api.test.im"

[context.deploy-preview.environment]
  REACT_APP_API_URL = "https://api-staging.test.im"

[context.branch-deploy.environment]
  REACT_APP_API_URL = "https://api-staging.test.im"
Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122
  • Yes, but this question is about taking that env var value and using it elsewhere in the config, like a redirects rule. – Jed Richards Jan 09 '19 at 18:06