Skip to content

How to use environment variables in Netlify functions

A short guide on how to use environment variables in Netlify functions

To use environment variables in your Netlify Functions, access the process.env variable:

process.env.YOUR_VARIABLE

You can use object destructuring at the beginning of your JS file, to make the code nicer:

const { YOUR_VARIABLE } = process.env;

so you can just use YOUR_VARIABLE in the rest of the program.

You set the variables through the Netlify administration interface (you can also add them in your repo, but I’d recommend using the Netlify UI so you have no secrets in your Git repository).

Note: this does not work on Netlify Edge Functions, just on Netlify “regular” Functions that run on AWS Lambda.

For Netlify Edge Functions, you need to use Deno.env.get(), like this:

Deno.env.get('YOUR_VARIABLE')

Example:

export default () => new Response(Deno.env.get('YOUR_VARIABLE'))

I wrote 21 books to help you become a better developer:

  • HTML Handbook
  • Next.js Pages Router Handbook
  • Alpine.js Handbook
  • HTMX Handbook
  • TypeScript Handbook
  • React Handbook
  • SQL Handbook
  • Git Cheat Sheet
  • Laravel Handbook
  • Express Handbook
  • Swift Handbook
  • Go Handbook
  • PHP Handbook
  • Python Handbook
  • Linux Commands Handbook
  • C Handbook
  • JavaScript Handbook
  • Svelte Handbook
  • CSS Handbook
  • Node.js Handbook
  • Vue Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list