Quickstart
Let's get you started with a few simple steps.
Link to section: installInstall
npm install arkenv arktypeLink to section: configure-your-projectConfigure your project
Enable strict mode for the best typesafety:
{
"compilerOptions": {
"strict": true
}
}ArkEnv is built on top of ArkType. Follow the ArkType setup instructions to configure your project.
Link to section: define-the-schemaDefine the schema
Add a schema to make your environment variables validated and typesafe:
import arkenv, { type } from 'arkenv';
export const env = arkenv({
// Built-in validators
DATABASE_HOST: "string.host",
DATABASE_PORT: "number.port",
// Boolean values (accepts "true"/"false" strings, converts to boolean)
DEBUG: "boolean",
// Custom string literals
NODE_ENV: "'development' | 'production' | 'test'",
// Optional variables with defaults
LOG_LEVEL: "'debug' | 'info' | 'warn' | 'error' = 'info'",
// Array defaults using type function
ALLOWED_ORIGINS: type("string[]").default(() => ["localhost"]),
FEATURE_FLAGS: type("string[]").default(() => []),
// Optional environment variable
"API_KEY?": 'string'
});Tip
Improve your DX with syntax highlighting in VS Code & Cursor or JetBrains IDEs.
Link to section: define-environment-variablesDefine environment variables
Create a .env file in your project root with some environment variables:
DATABASE_HOST=localhost
DATABASE_PORT=5432
DEBUG=true
NODE_ENV=development
API_KEY=your-secret-keyLink to section: use-in-your-codeUse in your code
Import and use your validated and typed environment variables. Here is an example for a database configuration file:
import { env } from './config/env';
// Hover to see your validated config
const dbConfig = {
host: env.DATABASE_HOST,
port: env.DATABASE_PORT,
};Now you might start to see the magic of ArkType and TypeScript working together to make your environment variables more reliable, more readable, and more typesafe.
Link to section: next-stepsNext steps
Integrate with VS Code & Cursor
Get syntax highlighting, ErrorLens, and more
Integrate with JetBrains IDEs
Get syntax highlighting
Examples
Dive into ArkEnv with our collection of examples
How to load environment variables
Learn environment variables management in different environments and deployment scenarios