500 Internal Server Error on API endpoints setEnvVarValue (PATCH) & updateEnvVar (PUT)

Hi,

I’m currently dealing with multiple issues when I’m trying to create/update environment variables through the Netlify API. I’ve tried to call multiple endpoints to achieve what I want to achieve, but I keep getting 500 responses on different endpoints.

These are the issues that I’ve encountered so far:

createEnvVars

The site_id property seems to be ignored, environment variables are always created on team level, and thats not what I want in my specific case.

So I went to use another/different endpoint:

setEnvVarValue

According to the API docs I can set a context here. This works for a custom context (“dev” “branch-deploy” “deploy-preview” “production” “branch”), but as soon as I send context=“all”, I will get a 500 response, according to the docs this should be possible.

What I don’t really understand is why this endpoint (as the only upsert endpoint) doesn’t allow me to set is_secret & scopes. This was a reason for me to also call the updateEnvVar endpoint.

updateEnvVar

This endpoint doesn’t seem to work at all. I keep getting 500 responses without any explanation what went wrong. I did make sure my environment variable existed on site level, because this endpoint can only update existing variables.

Seems related to issue: 500 Internal Server Error when setting Env Vars via API

Another (unrelated) thing that I’ve found: The OpenAPI specifications that I’ve downloaded from https://open-api.netlify.com/ (and the docs itself) are not fully up-to-date with the current API version, It’s missing quite some properties on the repoInfo object for example (base, untrusted_flow, base_rel_dir, package_path, repo_type, repo_owner_type, skip_automatic_builds, configuration_file_path)

I hope someone can help me out.
Thanks!

1 Like

Do you think you can share your request/response data by redacting the personal tokens?

Here is one of the requests that fails: (will post the other issues mentioned later)

setEnvVarValue

{account_id} is my accountId on my side
{access_token} is my accessToken on my side
{site_id} is my siteId on my side

PATCH: https://api.netlify.com/api/v1/accounts/{account_id}/env/TEST_ENV_VAR

Headers:
Accept: application/json
Authorization: Bearer {access_token}

Body:
{“key”:“TEST_ENV_VAR”,“account_id”:“{account_id}”,“site_id”:“{site_id}”,“value”:“devtest”,“context”:“all”}

Response:
{“code”:500,“message”:“Internal server error”}

I’m getting this 500 error as soon as i start sending context=all (as already mentioned above)
Request id: 578f897c-0e75-4b91-a000-dbc6710caf03

Nevermind about this part:
updateEnvVar

This endpoint doesn’t seem to work at all. I keep getting 500 responses without any explanation what went wrong. I did make sure my environment variable existed on site level, because this endpoint can only update existing variables.

I just found out that the library that I’m using (openapi-typescript-fetch) has a bug which malforms the request body, and thats the reason why this endpoint is always returning 500 errors in my case.

I would still appreciate some support on my previous comment, this one is different because I can also reproduce/trigger that issue in Postman.

Could you take a look at 500 Internal Server Error on API endpoints setEnvVarValue (PATCH) & updateEnvVar (PUT) - #3 by bas-schuitema please?

This body is incorrect.

It should be:

{
  "key": "TEST_ENV_VAR",
  "is_secret": false,
  "scopes": [
    "builds",
    "functions",
    "runtime",
    "post_processing"
  ],
  "values": [
    {
      "context": "all",
      "value": "value"
    }
  ]
}

The site ID needs to be in query params, not in the JSON.

The question I had left was about this endpoint:
https://open-api.netlify.com/#tag/environmentVariables/operation/setEnvVarValue

Site_id shouldn’t be in the JSON indeed, the library I’m using is doing that. But even if its not there, this endpoint is always failing whenever I send context=all.

{account_id} is my accountId on my side
{access_token} is my accessToken on my side
{site_id} is my siteId on my side

PATCH: https://api.netlify.com/api/v1/accounts/{account_id}/env/TEST_ENV_VAR?site_id={site_id}

Headers:
Accept: application/json
Authorization: Bearer {access_token}

Body:

{"value":"devtest","context":"all"}

Response:

{“code”:500,“message”:“Internal server error”}