How to Add ENV vars using the CLI

Hey there. I’ve been clicking through the GUI to build a number of sites recently and decided to try out the CLI to make the deployment process more efficient.

I need to set 2 or 3 vars per site to get my app to work. Is there a way to set ENV vars per site using the CLI? Or is that part of the API still private and only accessible through the GUI?

Thanks

hi there,

you can definitely used file based configuration to set up environment vars without going through the UI:

but i don’t think we have an API endpoint for you to be able to do that programmatically per se. :thinking: At least, i don’t see anything in here:

So it would still have to be a bit manual, but not as clicky as going through the UI for each one. Does that help?

Hey @perry I’m familiar with the netlify.toml and file-based config options, but that doesn’t really fit the bill here. I’m deploying many sites using the same codebase, and simply modifying a few sensitive ENV vars per site. So they shouldn’t be checked into my rep, like in a config file.

Ideally I could run something like netlify sites:create --with-ci VUE_APP_VAR_1=blah VUE_APP_KEY=app12398unalskjh. Or even just do it in a few steps after retrieving the new site ID.

Does that distinction make sense?

I just looked at the Open API docs you sent over and there seems to be something in the build config section, but its use is a little murky. Anyone on your side know how I can use it?

Your best advice on doing anything our UI can do via API will be in this article, @Resto:

If you watch what our UI does when you save a variable, you can see a call like this:

HTTP PUT https://api.netlify.com/api/v1/site/YOUR_SITE_ID_HERE

with a value like this:

{"build_settings":
  {"env":
    {"EXISTING_VAR":"existing_val","NEW_VAR":"new_val"}
  }
}

Make sure you send ALL existing vars, but also, try it in the UI first to see the shape of the command so you make sure you aren’t missing anything - perhaps my test site didn’t have some other configuration that yours did and you’d need to send that along too :slight_smile:

Super helpful, @fool thank you. The link to that Support Guide article is great. I’m wondering is there a way to make that part of Netlify’s docs more obvious? I’d consider myself somewhat of a “power” user who’s been using Netlify for at least 2+ years and had no idea it even existed until last week. I’m excited to dig in but wish I knew about it earlier! Thank you.

That’s a great suggestion! I’ll bring it up with our tech writers during our weekly pairing. I think it is already linked from the “main” API article here: https://docs.netlify.com/api/get-started/#make-a-request - so if you have any more specific suggestions about how to surface it, they would be welcome!

Hey @fool I just spent a few minutes thinking about how to surface the API docs. You’re totally right that it’s clearly listed on the Netlify Docs sidebar. I think the main reason I never investigated it was because there was no “entry point” for me. I think if you sprinkled examples around the Docs that pointed to different API endpoints that would be really really valuable.

For example, I noticed you have a short mention of the API endpoints here: https://docs.netlify.com/site-deploys/create-deploys/#api-endpoints but if you designed it more like a callout box with a specific color or icon that would be helpful. And then use that same design pattern around the docs like in the build configuration section and other places as well. Just to give some examples to devs of how they can automate or leverage the API in specific ways instead of just having it sit on the sidebar on the docs.

Hope that makes sense. I’d love to be a part of this discussion moving forward so keep me in the loop!

1 Like

That is DELIGHTFULLY, DELICIOUSLY AWESOME feedback. We’ll discuss with the docs team on friday and if they have any followup comments they’ll probably hit you up in this thread!

Thanks again!

1 Like

Agreed! That’s an awesome approach. Maybe the docs could just have parallel tracks for how to do any given action - how to do it in the UI and how to do it via API. :nerd_face: can’t wait to see where this goes!


Jon

2 Likes

Following up here as we’ve released a built in command to manage environment variables:

1 Like

Hey @erez is this tool for shared ENV vars only? I don’t see a way to target a specific site in the docs.

Hi @Resto, this is a command to manage environment variables for the currently linked site (run netlify link in a repo to link it).

You can use NETLIFY_SITE_ID env variable for a non linked site, and passing a site id similar to https://cli.netlify.com/commands/deploy does makes sense, but not supported as the moment.

You could however open a feature request for it in the CLI repo.

1 Like