How to add multiple environment variables to Vercel

How to add multiple environment variables to Vercel

Bulk import environment variables to a Vercel environment with a single CLI command.

Ryan Blunden's photo
Ryan Blunden
·Mar 24, 2022·

2 min read

Vercel's dashboard or CLI doesn't yet provide a solution for bulk uploading or adding multiple environment variables.

But with the bash shell, jq, and Vercel CLI, we can make it happen!

It bulk inserts environment variables for a single environment (e.g. Development) by using environment variables in JSON format to produce a list of vercel env add commands to execute as a script using source and bash process substitution.

If you haven’t already, install the Vercel CLI:

npm i -g vercel

Then link the application codebase on your local machine with your Vercel application by changing into the root directory of your application, then running:

vercel link

Next, create the environment variables in JSON for a single environment (e.g.Development):

// vercel-dev-env-vars.json
{
  "API_SECRET": "d3f5c0a1-2aaf-4e01-b94c-a9a8071896a6",
  "AUTH_TOKEN": "eeb40e34-a9e5-4781-88ef-3a9a63fc3f27"
}

In your terminal, define which environment you'll be importing to:

VERCEL_ENV="development"

Then run:

source <(jq -rj '. | to_entries[] | "echo -n \"\(.value)\" | vercel env add \(.key) $VERCEL_ENV;\n"' vercel-dev-env-vars.json)

You'll see the sequential output from the Vercel CLI as each variable is added successfully.

Be sure to delete the JSON file environment variables file to ensure no secrets such as API keys or auth tokens are at risk from attackers by living unencrypted on your machine.

Then simply repeat for other environments!

Shameless plug

If you're reading this post, you clearly care about automation when it comes to managing application secrets and configuration.

As a Developer Advocate from Doppler, I'm a bit biased, but that's where the Doppler CLI and Doppler Vercel Integration can help with secret automation features such as a Git style activity log with instant rollback support, automatic production redeploys when secrets change, and more.

youtube.com/watch?v=z3S40wpnFGI