# Nexts api context environment variable

**URL:** <https://answers.netlify.com/t/nexts-api-context-environment-variable/43675>\
**Category:** Support\
**Tags:** environment-vars, netlify-newbie, nextjs\
**Created:** [September 5, 2021, 11:18pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675 "2021-09-05T23:18:40Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 5, 2021, 11:18pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/1 "2021-09-05T23:18:40Z")

</div>

site: [https://dev--keen-ritchie-2146a3.netlify.app](https://dev--keen-ritchie-2146a3.netlify.app)

I’m very new to netlify and nextjs for that matter. I’m managing to get everything working and in general not having in real issues with build/deploy. I am building some api’s using nextjs (v11).

I am trying to figure out how I can determine what context the api is running in (dev, branch-deploy, production). I am currently on the free tier. I see in the docs there is a CONTEXT environment variable. This looks to be exactly what I’d like to get to but I can’ t figure out how.

I’ve tried to use process.env.CONTEXT in my nextjs api endpoint. I’ve been using console.logs in various points to try and see what the value is at various points. It always seems to be undefined. What am I missing?

Thanks in advance,  
Bob

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 8:29am UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/2 "2021-09-06T08:29:04Z")

</div>

CONTEXT is a build-time variable, that is, it’s not available to functions. You might have to use this:

> **[netlify-plugin-inline-functions-env](https://www.npmjs.com/package/netlify-plugin-inline-functions-env)**
>
> Inline build time environment variables into function code. Latest version: 1.0.9, last published: 2 years ago. Start using netlify-plugin-inline-functions-env in your project by running \`npm i netlify-plugin-inline-functions-env\`. There are no other...

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 12:26pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/3 "2021-09-06T12:26:12Z")

</div>

Thank you for the response. More newbie issues trying to get plugin working…

I did not have a netlify.toml file in my project. I had configured everything in the web ui. I’ve added the following to the netlify.toml file at the root of my project.

```auto
[[plugins]]
package = "netlify-plugin-inline-functions-env"
  [plugins.inputs]
  include = ["CONTEXT"]

```

This is the only entry in the file.

When I build I am getting the following error:

```auto
7:23:35 AM: ────────────────────────────────────────────────────────────────
7:23:35 AM: Plugin "netlify-plugin-inline-functions-env" failed           
7:23:35 AM: ────────────────────────────────────────────────────────────────
7:23:35 AM: ​
7:23:35 AM: Error message
7:23:35 AM: Error: Failed to inline function files because netlify function folder was not configured or pointed to a wrong folder, please check your configuration

```

I have tried numerous things but I am not able to get the nextjs project to build. If I remove netlify.toml the project builds/runs with no errors.

Any help greatly appreciated.

Bob

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 1:51pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/4 "2021-09-06T13:51:56Z")

</div>

Do you have a functions directory setup in netlify.toml?

It should look like:

```auto
[functions]
  directory = "your functions folder"

[[plugins]]
  package = "netlify-plugin-inline-functions-env"
  [plugins.inputs]
    include = ["CONTEXT"]

```

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 3:00pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/5 "2021-09-06T15:00:43Z")

</div>

I’ve changed my netlify.toml to this with the same result. I removed the plugins.inputs to get things as simple as possible.

```auto
[functions]
  directory = "netlify/functions"

[[plugins]]
  package = "netlify-plugin-inline-functions-env"

```

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 3:08pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/6 "2021-09-06T15:08:33Z")

</div>

And the error is still the same?

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 3:36pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/7 "2021-09-06T15:36:29Z")

</div>

It appears to be the same.

```auto
10:57:13 AM: ────────────────────────────────────────────────────────────────
10:57:13 AM: Plugin "netlify-plugin-inline-functions-env" failed           
10:57:13 AM: ────────────────────────────────────────────────────────────────
10:57:13 AM: ​
10:57:13 AM: Error message
10:57:13 AM: Error: Failed to inline function files because netlify function folder was not configured or pointed to a wrong folder, please check your configuration

```

Bob

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 3:37pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/8 "2021-09-06T15:37:17Z")

</div>

Would it be possible for you to share the repo (or check if you’ve got the correct folder for functions)?

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 3:38pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/9 "2021-09-06T15:38:30Z")

</div>

I can do that. Is there a way to private message you the repo url?

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 3:41pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/10 "2021-09-06T15:41:46Z")

</div>

PMs should be enabled for your account.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 3:45pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/11 "2021-09-06T15:45:25Z")

</div>

So, if I’m seeing it correctly, your functions live inside `helpers/api` folder, correct?

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 3:49pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/12 "2021-09-06T15:49:21Z")

</div>

Well, those are functions that are used by other functions. The base functions are at /pages/api.

I was under the impression that the functions build are located in .netlify/functions.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 3:56pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/13 "2021-09-06T15:56:34Z")

</div>

`.netlify/functions` is the path for the deployed functions (or functions bundled using the CLI).

You would have your functions’ source code somewhere in the repo, correct?

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 4:02pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/14 "2021-09-06T16:02:31Z")

</div>

In nextjs the api functions I’m using start at pages/api. I added in a sub directory “v1” so I guess technically for now the api functions live at /pages/api/v1. When I look at the build logs it looks like those functions end up being built in .netlify/functions directory since I did not specify anything in the functions path.

The application is working as expected so I know that the functions are getting called. But the plugin seems to be confused by where the functions are. Should I set the functions to pages/api/v1 and try again?

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 6, 2021, 4:06pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/15 "2021-09-06T16:06:49Z")

</div>

Yes, if the files in that folder are being published to `/.netlify/functions/`, that’s your functions’ directory. You could add that to the plugin and it should work.

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 4:12pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/16 "2021-09-06T16:12:09Z")

</div>

With

```auto
[functions]
  directory = ".netlify/functions"

[[plugins]]
  package = "netlify-plugin-inline-functions-env"

```

I got same error

```auto
12:11:01 PM: ────────────────────────────────────────────────────────────────
12:11:01 PM: Plugin "netlify-plugin-inline-functions-env" failed           
12:11:01 PM: ────────────────────────────────────────────────────────────────
12:11:01 PM: ​
12:11:01 PM: Error message
12:11:01 PM: Error: Failed to inline function files because netlify function folder was not configured or pointed to a wrong folder, please check your configuration

```

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 6, 2021, 4:14pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/17 "2021-09-06T16:14:53Z")

</div>

I just tried this and got same error:

```auto
[functions]
  directory = "pages/api/v1"

[[plugins]]
  package = "netlify-plugin-inline-functions-env"

```

---

<div class="post-metadata">

**Author:** ![bhall2001](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bhall2001/32/29168_2.png) [@bhall2001](https://answers.netlify.com/u/bhall2001)\
**Post date:** [September 7, 2021, 11:07pm UTC](https://answers.netlify.com/t/nexts-api-context-environment-variable/43675/19 "2021-09-07T23:07:24Z")

</div>

I wanted to leave a post here with my resolution. First I want to thank @hrishikesh for helping me and for being instrumental in getting a working solution. Being a new customer (and on the free plan at the moment) , I was impressed by the level of support I received.

The plugin wasn’t going to be a good solution. What I ended up doing is the following.

_Before you do any of this make sure you understand what you are doing and for your own sake, don’t write a secret to your public nextjs directory. I take no responsibility if that happens._

1. create a file context.js at the root of the project with these contents:

```auto
const fs = require('fs');

fs.writeFileSync(
  'public/context.json',
  JSON.stringify({ context: process.env.CONTEXT })
);

```

This will write the CONTEXT environment variable to the public directory.

1. create the file public/context.json with the contents below. This is needed when you are running locally. Commit this file.

```auto
{ "context": "local" }

```

1. change your build command to `node context.js && npm run build`. This will execute the context.js file writing the value of the CONTEXT environment variable to public/context.json.

2. in your nextjs api server side code (in my case) you can access the value in context.json:

```auto
  const context = require('public/context.json');
  console.log('context :>> ', context);

```

Now nextjs api’s can distinguish between local, branch-deploy and production. AWESOME!

Cheers!  
Bob  
Manchester, NH US
