Quickstart
Let's get you started with a few simple steps
#Install
npm install arkenv arktype
#Configure 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.
#Define the schema
Add a schema to make your environment variables validated and typesafe:
import arkenv from 'arkenv';
export const env = arkenv({
// Built-in validators
DATABASE_HOST: "string.host",
DATABASE_PORT: "number.port",
// Custom string literals
NODE_ENV: "'development' | 'production' | 'test'",
// Optional variables with defaults
LOG_LEVEL: "'debug' | 'info' | 'warn' | 'error' = 'info'",
// Optional environment variable
"API_KEY?": 'string'
});
Tip
VS Code Users: Want syntax highlighting for your ArkType schemas? Check out our VS Code Integration guide to learn about the ArkType extension and improve your DX!
#Define environment variables
Create a .env
file in your project root with some environment variables:
DATABASE_HOST=localhost
DATABASE_PORT=5432
NODE_ENV=development
API_KEY=your-secret-key
#Use in your code
Import and use your validated and typed environment variables. For example, you might have a file like database.ts
:
import { env } from './config/env';
// TypeScript knows the ✨exact✨ types!
const dbConfig = {
host: env.DATABASE_HOST, // string (valid host)
port: env.DATABASE_PORT, // number (valid port)
};
#Next steps
VS Code Integration
Get syntax highlighting and enhanced developer experience with the ArkType VS Code extension
Type Function
Learn how to separate schema definition from validation using ArkEnv's type function
Loading environment variables
Learn how to manage environment variables across different environments and deployment scenarios
Examples
See examples of ArkEnv in action