Navigated to /docs/environment-variables.

Environment variables

Configuring your application depending on the runtime environment

When building an application with Nokkio, you may need to configure it to behave differently in development and production. For example, if you use a third-party service, the URL you use to connect to that service might differ between environments. Nokkio supports environment variables for this purpose.

Caution
Do not use environment variables to store sensitive values like API keys. Use Nokkio's secrets feature instead.

Setting environment variables

All Nokkio applications are created with a nokkio.env.json file where you can specify your environment variables for development and production. For example:

nokkio.env.json
{
  "development": {
    "FOO": "BAR"
  },
  "production": {
    "FOO": "BAZ"
  }
}

Using environment variables

Once you have environment variables defined in nokkio.env.json, they are accessible via the NOKKIO_ENV global.

pages/index.js
export default function IndexPage() {
  return <div>The value is {NOKKIO_ENV.FOO}</div>;
}

In the above example, NOKKIO_ENV.FOO will return BAR in development and BAZ in your production build.

Using environment variables in server code

The same NOKKIO_ENV global is available in your server code, so your environment variables can be used in endpoints, gates, model events, and schedules:

server/endpoints/hello-word.js
export default function () {
  return new Response(`The value is ${NOKKIO_ENV.FOO}`);
}