Nitro logoNitro

Runtime Config

View Source
Environment-aware configuration with runtime access.

Runtime config lets you define configuration values that can be overridden by environment variables at runtime.

├── .env
├── .gitignore
├── nitro.config.ts
├── package.json
├── README.md
├── server.ts
├── tsconfig.json
└── vite.config.ts

Define Config Schema

Declare your runtime config with default values in nitro.config.ts:

nitro.config.ts
import { defineConfig } from "nitro";

export default defineConfig({
  serverDir: "./",
  runtimeConfig: {
    apiKey: "",
  },
});

Access at Runtime

Use useRuntimeConfig to access configuration values in your handlers:

server.ts
import { defineHandler } from "nitro/h3";
import { useRuntimeConfig } from "nitro/runtime-config";

export default defineHandler((event) => {
  const runtimeConfig = useRuntimeConfig();
  return { runtimeConfig };
});

Environment Variables

Override config values via environment variables prefixed with NITRO_:

.env
# NEVER COMMIT SENSITIVE DATA. THIS IS ONLY FOR DEMO PURPOSES.
NITRO_API_KEY=secret-api-key

Learn More