How am I supposed to set up env variables over API?


I am migrating API calls from old ENV using build_settings to new environment variables. I am using cURL calls to find a way to do it, but builds never pick up on it.

I want to set a variable MY_VAR and I use a call like so:

PATCH /api/v1/accounts/{account_id}/env/MY_VAR?site_id={site_id}
{ "value": "my_value", "context": "{see below}" }

I tried cycling through all four possible contexts:

  • "dev"
  • "branch-deploy"
  • "deploy-preview"
  • "production"

And I see see in browser admin panel that the value is correctly assigned. So the API works. However, in neither case do builds pick up on the value.

I tested it and the only way for me to set the value for REAL right now is to go to the admin panel in my browser and set a value for all contexts in there.

The API does not support setting a value for all contexts at once which means I need to do 4 calls for each env var in order to have it in my builds.

This looks like a bug to me, but Netlify has been breaking for me recently in every possible aspect, so this may as well be intended behaviour.

Or is there some kind of combination I could you that doesn’t need all four? I have just 2 vars, but that’s already 8 calls to API when it was just 1 before. :man_shrugging:

Best regards

Hi, @Goues. My recommendation on this comes from our support guide below about working with API:

The advice is summarized as:

  • make the change in our web UI and examine the network tab in devtools to see the exact format that is used there

For example, when I add an environment variable, this is the format of the JSON sent:

    "key": "EXAMPLE",
    "scopes": [
    "values": [
        "context": "production",
        "value": "this is production"
        "context": "deploy-preview",
        "value": "this is a deploy preview"
        "context": "branch-deploy",
        "value": "this is a branch deploy"
        "context": "dev",
        "value": "this is the CLI tool"

It can be set in a single API call using the format above. Please reply anytime if there are other questions.

Thank you, this works very well.