Runtime Config

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.

Exposing runtime config

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.

Example:

nuxt.config.ts

  1. export default defineNuxtConfig({
  2. runtimeConfig: {
  3. // The private keys which are only available within server-side
  4. apiSecret: '123',
  5. // Keys within public, will be also exposed to the client-side
  6. public: {
  7. apiBase: '/api'
  8. }
  9. }
  10. })

When adding apiBase to the runtimeConfig.public, Nuxt adds it to each page payload. We can universally access apiBase in both server and browser.

  1. const runtimeConfig = useRuntimeConfig()
  2. console.log(runtimeConfig.apiSecret)
  3. console.log(runtimeConfig.public.apiBase)

Environment Variables

The most common way to provide configuration is by using Environment Variables. Nuxt CLI has built-in dotenv support.

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.

Runtime config values are automatically replaced by matching environment variables at runtime.

Example:

.env

  1. NUXT_API_SECRET=api_secret_token
  2. NUXT_PUBLIC_BASE_URL=https://nuxtjs.org

nuxt.config.ts

  1. export default defineNuxtConfig({
  2. runtimeConfig: {
  3. apiSecret: '',
  4. public: {
  5. apiBase: '', // Or a default value
  6. }
  7. },
  8. })

Accessing runtime config

Vue app

Within the Vue part of your Nuxt app, you will need to call useRuntimeConfig() to access the runtime config.

Note: Behavior is different between the client-side and server-side:

  • 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.

  1. <template>
  2. <div>
  3. <div>Check developer console!</div>
  4. </div>
  5. </template>
  6. <script setup>
  7. const config = useRuntimeConfig()
  8. console.log('Runtime config:', config)
  9. if (process.server) {
  10. console.log('API secret:', config.apiSecret)
  11. }
  12. </script>

🛑 Security note: Be careful not to expose runtime config keys to the client-side by either rendering them or passing them to useState.

👉

useRuntimeConfig only works during setup or Lifecycle Hooks.

Plugins

If you want to use the runtime config within any (custom) plugin, you can use useRuntimeConfig() inside of your defineNuxtPlugin function.

For Example:

  1. export default defineNuxtPlugin((nuxtApp) => {
  2. const config = useRuntimeConfig()
  3. console.log('API base URL:', config.public.apiBase)
  4. });

Server Routes

You can access runtime config within the server routes as well using useRuntimeConfig.

  1. export default async () => {
  2. const result = await $fetch('https://my.api.com/test', {
  3. headers: {
  4. Authorization: `Bearer ${useRuntimeConfig().apiSecret}`
  5. }
  6. })
  7. return result
  8. }

Manually Typing Runtime Config

Nuxt tries to automatically generate a typescript interface from provided runtime config using unjs/untyped

It is also possible to type your runtime config manually:

index.d.ts

  1. declare module '@nuxt/schema' {
  2. interface RuntimeConfig {
  3. apiSecret: string
  4. public: {
  5. apiBase: string
  6. }
  7. }
  8. }
  9. // It is always important to ensure you import/export something when augmenting a type
  10. export {}