2022-03-30 17:32:30 +00:00
# Runtime Config
If you wish to reference environment variables within your Nuxt 3 app, you will need to use runtime config.
2023-07-07 16:24:09 +00:00
When referencing these variables within your components, you will have to use the [`useRuntimeConfig` ](/docs/api/composables/use-runtime-config ) composable in your setup method (or Nuxt plugin).
2022-10-06 09:15:30 +00:00
2023-07-07 16:24:09 +00:00
In the `server/` portion of your app, you can use [`useRuntimeConfig` ](/docs/api/composables/use-runtime-config ) without any import.
2022-03-30 17:32:30 +00:00
2022-11-16 10:04:28 +00:00
[Read more about runtime config ](/docs/guide/going-further/runtime-config ).
2022-03-30 17:32:30 +00:00
## Migration
2022-04-27 09:05:08 +00:00
1. Add any environment variables that you use in your app to the `runtimeConfig` property of the `nuxt.config` file.
2023-07-07 16:24:09 +00:00
1. Migrate `process.env` to [`useRuntimeConfig` ](/docs/api/composables/use-runtime-config ) throughout the Vue part of your app.
2022-03-30 17:32:30 +00:00
## Example
::code-group
```ts [nuxt.config.ts]
export default defineNuxtConfig({
2022-04-12 08:12:33 +00:00
runtimeConfig: {
2022-06-22 18:07:23 +00:00
// Private config that is only available on the server
apiSecret: '123',
// Config within public will be also exposed to the client
2022-04-12 08:12:33 +00:00
public: {
2022-06-22 18:07:23 +00:00
apiBase: '/api'
2022-04-12 08:12:33 +00:00
}
2022-03-30 17:32:30 +00:00
},
})
```
```vue [pages/index.vue]
< script setup >
2022-06-22 18:07:23 +00:00
const config = useRuntimeConfig()
2022-09-14 17:26:43 +00:00
2022-06-22 18:07:23 +00:00
// instead of process.env you will now access config.public.apiBase
console.log(config.public.apiBase)
2022-03-30 17:32:30 +00:00
< / script >
```
```ts [server/api/hello.ts]
2022-06-22 18:07:23 +00:00
const config = useRuntimeConfig()
2022-04-07 13:47:15 +00:00
2022-03-30 17:32:30 +00:00
export default (req, res) => {
2022-06-22 18:07:23 +00:00
// In server, you can now access config.apiSecret, in addition to config.public
console.log(config.apiSecret)
console.log(config.public.apiBase)
2022-03-30 17:32:30 +00:00
}
```
2022-06-22 18:07:23 +00:00
```ini [.env]
# Runtime config values are automatically replaced by matching environment variables at runtime
NUXT_API_SECRET=api_secret_token
NUXT_PUBLIC_API_BASE=https://nuxtjs.org
```
2022-03-30 17:32:30 +00:00
::