DEV Community

Cover image for Use, Access & Share Environment Variables on Netlify
Phil Hawksworth for Netlify

Posted on β€’ Originally published at netlify.com

3 2

Use, Access & Share Environment Variables on Netlify

Throughout December we'll be highlighting a different Netlify feature each day. It might just be the thing you need to unlock those creative juices, and dust off that domain you registered but never deployed! Keep an eye on the blog and on Twitter for each feature!

Netlify allows you to create and access build environment variables in a secure, and private location in your project's dashboard. Site settings > Build & deploy > Environment > Environment variables, to be exact, ooh or Team settings > Sites > Global site settings > Shared environment variables if you're the "sharing is caring" type.

Configuring environment variables in the Netlify admin app

These can also be set using the Netlify configuration file, netlify.toml. On top of that, you can even inject those environment variables into any shell command (such as the [build] command and ignore command). Here's more info on that and here's what that would look like.

# netlify.toml

[build]
  command = "cp ./ ${PROJECT_COPY_DIR} && npm run build"
  publish = "dist/my-project"
  environment = { 
    PROJECT_COPY_DIR = "/that/one/place",
    GO_IMPORT_PATH = "go/go/gadget.go",
    API_KEY = "abc1234teeheehee"
  }

[context.staging] # β€œstaging” is a branch name
  command = "npm run dev"
  base = "staging"
  environment = { SOME_KEY = "t0te5Important5tuff" }
Enter fullscreen mode Exit fullscreen mode

You can set different environment variables for different deploy contexts and access them through your serverless functions, snippet injection, app code (I tend to grab them via process.env.THIS_ENV_VAR_NAME in my code), and many other ways. If you have team members you are onboarding don't worry about giving them the project env vars insecurely over slack or through multiple steps of auth, they just live with the project.

You can learn a bunch more through our docs. There is all kinds of fun stuff there including a bunch of environment variables that tell you things like build metadata, git metdata, build hook meta data (& payload), deploy URLS and MORE metadata!

Hope this helps you keep things available to your nice devs and away from naughty hackers. Happy coding πŸ‘©πŸ»β€πŸ’»!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay