Determining execution scope at runtime

Is there a recommended method to determine code execution scope at runtime?

I wish to pull an API authentication token from process.env in Build scope but wish to pull that same token via API call in Function scope.

Thank you,

Sorry, could you please rephrase your question? I tried reading it multiple times, but I can’t seem to understand it. What I understood was you wish to access an environment variable during build time and in functions, but I think there’s more to this question.

Hello @hrishikesh ,

Yes, let me attempt to make the intent more clear.

Starting with the description of environment variable scopes here: Environment variables overview | Netlify Docs

We are attempting to make sure that when we are executing code, either during a build or during a Netlify function call, that we are able to get the latest oAuth token necessary to make a particular API call.

In the build scope we can get that value from the environment variables as configured.

In the function scope we need to call the Netlify API to get the latest oAuth token from the environment variable.

My question is, how do we determine the execution scope we are in, if there is a recommended way to do that?

Thank you,
Hardy Johnson

Hi, @hardy. Is the issue here that you are running identical code both places? If so, one way to differentiate is by checking for an environment variable of NETLIFY=true. That is always set automatically in the build system but never for the function environment.

Thank you @luke I believe that is what I am looking for.