Trouble accessing environment variables in Netlify Function (vanilla JS app)

Hello -

I’m trying to access an environment variable (ie. API_KEY) from a Function, with the goal of fetching data from an API and returning the data to my JS front end. I’ve set the environment variable in the Netlify UI and the logs tell me it is being injected. The function seems to be deployed and running, however when I try to access my environment variable with the line:

const API_KEY = process.env.API_KEY

I get the error:

ReferenceError: Can't find variable: process

It seems like I should have access to process from my Function, but clearly I’m missing a step. I also feel like I’m getting mixed messages from the docs, with some articles saying use process.env to access environment variables, but other sources (including Ask Netlify) saying to use Netlify.env.get(), but with little information about how to include/access the Netlify object.

I’m searched the forum for a solution but most of the info I’ve found is around accessing environment variables from the client side or at build time. To be clear, I’m trying to access the environment variable from my Netlify Function, but the function doesn’t seem to have access to process for some reason.

The site is: https://ptfc-db.netlify.app
Repo: GitHub - madamo/timbers-dashboard: A data viz of Portland Timbers stats

Thanks!

“articles” are not docs, the docs are here: Functions API reference | Netlify Docs which mention that Netlify is a global.

You can still choose to use process.env like:

import { env } from 'node:process'

but I don’t see a good reason to do that when Netlify.env exists.

Thanks, hrishikesh. Not sure if it was a case of overthinking or under thinking, but the issue was that I was including the function in my front end code rather than fetching my function’s endpoint. Once I made that fix, the function worked as expected.