Figuring out if netlify-function is in a main env or production deploy

What is the best way to figure out if a netlify function (written in js) is running in a deploy-preview or a production deploy?
I have hacked my way to use the context.clientContext.identity variable to get the baseURL of the current env. But this is definitely a big hack and might stop working any day i feel. How have others been taking care of this situation?

I think there should literally be a $CONTEXT variable that says something like “deploy-preview”, “branch-deploy”, or “production”. Can you see if you can see that from your function code?

It’d match what’s defined here, if I’m right:

unfortunately not. These are all the environment variables available to a function.

Jo @avk , none of those environment variables are available in your function, but we do inject your public URL in to the clientContext that’s passed in to the function. If you need to know the value of context from a function then you’ll have to interpolate that in during your build process.

Are there any docs on the best way to handle this? Without knowing where the function is running I’m not sure how we can have multiple environments.

@futuregerald how can i access those during the build process, is there a doc for doing that with netlify functions.
I currently am using javascript functions -


We don’t have a guide on how to inject variables into your functions, but you can do this with webpack if you are comfortable setting that up. Another option is to write a script that does this yourself, but there are libraries and plugins that can help with this already.

See also this thread for some tips about how to determine build context from within a Netlify function: Netlify Functions and Env Variables

1 Like