2021-11-10 12:40:02 +00:00
# Runtime Config
2022-04-12 10:04:15 +00:00
Nuxt provides a runtime config API to expose config within your application and server routes with the ability to update them at runtime using environment variables.
2021-11-10 12:40:02 +00:00
## Exposing runtime config
2022-04-12 10:04:15 +00:00
To expose config and environment variables to the rest of your app, you will need to define runtime configuration in your `nuxt.config` file, using the [`runtimeConfig` option ](/guide/directory-structure/nuxt.config#runtimeconfig ).
2021-11-10 12:40:02 +00:00
**Example:**
```ts [nuxt.config.ts]
export default defineNuxtConfig({
2022-04-12 08:12:33 +00:00
runtimeConfig: {
2022-04-12 10:04:15 +00:00
// The private keys which are only available within server-side
apiSecret: '123',
// Keys within public, will be also exposed to the client-side
2022-04-12 08:12:33 +00:00
public: {
2022-04-12 10:04:15 +00:00
apiBase: '/api'
2022-04-12 08:12:33 +00:00
}
2022-04-12 10:04:15 +00:00
}
2021-11-10 12:40:02 +00:00
})
```
2022-04-12 10:04:15 +00:00
When adding `apiBase` to the `runtimeConfig.public` , Nuxt adds it to each page payload. We can universally access `apiBase` in both server and browser.
```js
const runtimeConfig = useRuntimeConfig()
console.log(runtimeConfig.apiSecret)
console.log(runtimeConfig.public.apiBase)
```
2021-11-10 12:40:02 +00:00
### Environment Variables
2021-11-21 12:31:44 +00:00
The most common way to provide configuration is by using [Environment Variables ](https://medium.com/chingu/an-introduction-to-environment-variables-and-how-to-use-them-f602f66d15fa ).
2022-04-29 18:09:12 +00:00
::alert{type=info}
2021-11-10 12:40:02 +00:00
Nuxt CLI has built-in [dotenv ](https://github.com/motdotla/dotenv ) support.
2021-11-21 12:31:44 +00:00
In addition to any process environment variables, if you have a `.env` file in your project root directory, it will be automatically loaded into `process.env` and accessible within your `nuxt.config` file and modules.
2021-11-10 12:40:02 +00:00
2022-04-29 18:09:12 +00:00
However, **after your project is built** , you are responsible for setting environment variables when you run the server - your `.env` file will not be read at this point.
::
2022-04-20 14:24:59 +00:00
Runtime config values are automatically replaced by matching environment variables at runtime. For this to work, you _must_ have a fallback value (which can just be an empty string) defined in your `nuxt.config` .
2022-04-12 10:04:15 +00:00
2021-11-10 12:40:02 +00:00
**Example:**
```sh [.env]
2022-04-12 10:04:15 +00:00
NUXT_API_SECRET=api_secret_token
2022-04-25 09:29:26 +00:00
NUXT_PUBLIC_API_BASE=https://nuxtjs.org
2021-11-10 12:40:02 +00:00
```
```ts [nuxt.config.ts]
export default defineNuxtConfig({
2022-04-12 08:12:33 +00:00
runtimeConfig: {
2022-07-13 17:03:02 +00:00
apiSecret: '', // can be overridden by NUXT_API_SECRET environment variable
2022-04-12 08:12:33 +00:00
public: {
2022-07-13 17:03:02 +00:00
apiBase: '', // can be overridden by NUXT_PUBLIC_API_BASE environment variable
2022-04-12 08:12:33 +00:00
}
2021-11-10 12:40:02 +00:00
},
})
```
## Accessing runtime config
### Vue app
Within the Vue part of your Nuxt app, you will need to call `useRuntimeConfig()` to access the runtime config.
2022-04-12 10:04:15 +00:00
**Note:** Behavior is different between the client-side and server-side:
2021-11-10 12:40:02 +00:00
2022-04-12 10:04:15 +00:00
- On the client-side, only keys in `public` are available, and the object is both writable and reactive.
The entire runtime config is available on the server-side, but it is read-only to avoid context sharing.
2021-11-10 12:40:02 +00:00
```vue
< template >
< div >
2022-04-12 10:04:15 +00:00
< div > Check developer console!< / div >
2021-11-10 12:40:02 +00:00
< / div >
< / template >
< script setup >
const config = useRuntimeConfig()
2022-04-12 10:04:15 +00:00
console.log('Runtime config:', config)
if (process.server) {
console.log('API secret:', config.apiSecret)
}
2021-11-10 12:40:02 +00:00
< / script >
```
2022-04-12 10:04:15 +00:00
**🛑 Security note:** Be careful not to expose runtime config keys to the client-side by either rendering them or passing them to `useState` .
2021-11-10 12:40:02 +00:00
2021-11-15 13:13:00 +00:00
::alert{icon=👉}
2021-11-21 12:31:44 +00:00
**`useRuntimeConfig` only works during `setup` or `Lifecycle Hooks` **.
2021-11-15 13:13:00 +00:00
::
2022-01-25 12:04:22 +00:00
### Plugins
2022-04-12 08:12:33 +00:00
If you want to use the runtime config within any (custom) plugin, you can use `useRuntimeConfig()` inside of your `defineNuxtPlugin` function.
2022-01-25 12:04:22 +00:00
2022-04-12 10:04:15 +00:00
For Example:
2022-01-25 12:19:40 +00:00
2022-01-25 12:04:22 +00:00
```ts
export default defineNuxtPlugin((nuxtApp) => {
2022-04-07 21:16:42 +00:00
const config = useRuntimeConfig()
2022-04-12 10:04:15 +00:00
console.log('API base URL:', config.public.apiBase)
2022-01-25 12:04:22 +00:00
});
```
2022-04-12 10:04:15 +00:00
### Server Routes
2021-11-10 12:40:02 +00:00
2022-04-12 10:04:15 +00:00
You can access runtime config within the server routes as well using `useRuntimeConfig` .
2021-11-10 12:40:02 +00:00
```ts
export default async () => {
const result = await $fetch('https://my.api.com/test', {
headers: {
2022-04-12 10:04:15 +00:00
Authorization: `Bearer ${useRuntimeConfig().apiSecret}`
2021-11-10 12:40:02 +00:00
}
})
return result
}
```
2022-04-12 10:04:15 +00:00
### Manually Typing Runtime Config
2022-07-01 10:01:14 +00:00
Nuxt tries to automatically generate a typescript interface from provided runtime config using [unjs/untyped ](https://github.com/unjs/untyped ).
2021-11-10 12:40:02 +00:00
2022-04-12 10:04:15 +00:00
It is also possible to type your runtime config manually:
2021-11-10 12:40:02 +00:00
```ts [index.d.ts]
2021-11-23 15:43:24 +00:00
declare module '@nuxt/schema' {
2022-04-12 10:04:15 +00:00
interface RuntimeConfig {
apiSecret: string
2022-04-12 08:12:33 +00:00
public: {
2022-04-12 10:04:15 +00:00
apiBase: string
2022-04-12 08:12:33 +00:00
}
2021-11-10 12:40:02 +00:00
}
}
// It is always important to ensure you import/export something when augmenting a type
export {}
```