Telaio
API Reference

Config API Reference

Complete reference for loadConfig(), defineConfig(), InferConfig, and all configuration environment variables.

Config API Reference

loadConfig()

function loadConfig<const TModules extends ConfigModules, TExtend extends z.ZodType>(
  options?: LoadConfigOptions<TModules, TExtend>
): InferConfig<TModules, TExtend>

Synchronously loads, validates, and returns the config. Reads environment variables from the process environment and, by default, from a .env file via dotenv. Returns a plain record typed as InferConfig<TModules, TExtend>.

import { loadConfig } from 'telaio';

const config = loadConfig({
  modules: { server: true, database: true },
});

For tests, bypass .env loading entirely with skipEnvLoad and supply values inline:

const config = loadConfig({
  source: {
    DATABASE_URL: 'postgresql://localhost/test',
    NODE_ENV: 'test',
  },
  skipEnvLoad: true,
});

loadConfigAsync()

async function loadConfigAsync<const TModules extends ConfigModules, TExtend extends z.ZodType>(
  options?: LoadConfigOptions<TModules, TExtend>
): Promise<InferConfig<TModules, TExtend>>

Async variant of loadConfig(). Use when you need async .env resolution or are in an async context. API is identical to loadConfig().


defineConfig()

function defineConfig<const TModules extends ConfigModules, TExtend extends z.ZodType>(
  options: DefineConfigOptions<TModules, TExtend>
): DefineConfigResult<TModules, TExtend>

Defines a config factory for use in config.ts files. Returns a callable that accepts an optional source override, enabling the same config definition to be reused in both production (reads env) and tests (inline source).

// src/config.ts
import { defineConfig } from 'telaio';
import { z } from 'zod';

export default defineConfig({
  modules: { server: true, database: true, cache: true },
  extend: z.object({
    STRIPE_SECRET_KEY: z.string(),
  }),
});
// In app entry point:
import config from './config.js';
const loaded = config();

// In tests:
const loaded = config({ source: { DATABASE_URL: '...', STRIPE_SECRET_KEY: 'sk_test_...' }, skipEnvLoad: true });

InferConfig<TModules, TExtend>

type InferConfig<TModules, TExtend extends z.ZodType> =
  CoreConfig &
  (TModules['server'] extends true ? ServerConfig : unknown) &
  (TModules['database'] extends true ? DatabaseConfig : unknown) &
  (TModules['cache'] extends true ? CacheConfig : unknown) &
  (TModules['queue'] extends true ? QueueConfig : unknown) &
  (TModules['s3'] extends true ? S3Config : unknown) &
  (TModules['email'] extends true ? EmailConfig : unknown) &
  z.infer<TExtend>

Conditional utility type that assembles the config type from enabled modules. Each module flag adds the corresponding typed fields to the intersection.

InferConfig<Modules, never> collapses to never in Zod v4. Never use this generic directly with never as TExtend. Use loadConfig() with defaults or pass z.object({}) as the extend type.


ConfigModules

interface ConfigModules {
  server?: boolean;
  database?: boolean;
  cache?: boolean;
  queue?: boolean;
  s3?: boolean;
  email?: boolean;
}

Flags that opt into additional environment variable groups. Each flag set to true adds the corresponding config schema fields to the returned type and validation.


LoadConfigOptions

interface LoadConfigOptions<TModules, TExtend extends z.ZodType> {
  modules?: TModules;
  extend?: TExtend;
  skipEnvLoad?: boolean;
  source?: Record<string, unknown>;
}
OptionTypeDefaultDescription
modulesConfigModules{}Which module schemas to include
extendz.ZodTypez.object({})Additional Zod schema to merge into validation
skipEnvLoadbooleanfalseSkip .env file loading (for tests)
sourceRecord<string, unknown>process.envOverride environment variable source entirely

DefineConfigOptions

interface DefineConfigOptions<TModules, TExtend extends z.ZodType> {
  modules?: TModules;
  extend?: TExtend;
}

Same as LoadConfigOptions minus skipEnvLoad and source, which are supplied at call time.


Config schema types

CoreConfig

Always present. No module flag required.

VariableTypeDefaultDescription
APP_NAMEstring'App'Application name
NODE_ENV'development' | 'production' | 'test''development'Runtime environment
BASE_DIRstringprocess.cwd()Base filesystem directory

ServerConfig

Enabled with modules: { server: true }.

VariableTypeDefaultDescription
API_URLstring (URL)'http://localhost:4001'Public API base URL
API_LISTEN_PORTnumber4001Port to listen on
API_LISTEN_ADDRESSstring'0.0.0.0'Host address to bind
CORS_ORIGINSstring[][]Allowed CORS origins (comma-separated in env)
WHITELIST_PROXIESstring[][]Trusted proxy IPs (comma-separated in env)
ENABLE_API_DOCSbooleanfalseWhether to mount the Scalar API reference UI

DatabaseConfig

Enabled with modules: { database: true }.

VariableTypeDefaultDescription
DATABASE_URLstring (URL)'postgresql://localhost/app'Postgres connection URI
DATABASE_SSLboolean | undefinedundefinedForce SSL. Auto-detected for RDS hostnames.

CacheConfig

Enabled with modules: { cache: true }.

VariableTypeDefaultDescription
REDIS_ENABLEDbooleantrueWhether to connect to Redis
REDIS_URLstring | undefinedundefinedRedis connection URI

QueueConfig

Enabled with modules: { queue: true }.

VariableTypeDefaultDescription
QUEUE_SCHEMAstring'pgboss'Postgres schema for pg-boss tables

S3Config

Enabled with modules: { s3: true }.

VariableTypeDefaultDescription
AWS_S3_REGIONstring'us-east-1'AWS region for S3
AWS_S3_BUCKET_NAMEstring'assets'Default S3 bucket name
AWS_S3_ENDPOINTstring | undefinedundefinedCustom S3 endpoint (for LocalStack, MinIO, etc.)
AWS_S3_ACCESS_KEY_IDstring | undefinedundefinedAWS access key ID
AWS_S3_SECRET_ACCESS_KEYstring | undefinedundefinedAWS secret access key

EmailConfig

Enabled with modules: { email: true }.

VariableTypeDefaultDescription
AWS_SES_REGIONstring'us-east-1'AWS region for SES
EMAIL_FROMstring'noreply@example.com'Default sender address

Helper schemas

envBoolean

const envBoolean: z.ZodEffects<z.ZodBoolean>

A Zod preprocessor that coerces string environment variable values to boolean. The strings 'false', '0', and '' (empty string) become false. Any other truthy string becomes true.

Use this in extend schemas when you need boolean flags from environment variables:

import { envBoolean } from 'telaio/config';
import { z } from 'zod';

const config = loadConfig({
  extend: z.object({
    FEATURE_FLAG: envBoolean.default(false),
  }),
});

csvString

const csvString: z.ZodEffects<z.ZodArray<z.ZodString>>

A Zod preprocessor that splits a comma-separated string into a string[]. Used internally for CORS_ORIGINS and WHITELIST_PROXIES. Handy for any env var that should accept a list:

import { csvString } from 'telaio/config';
import { z } from 'zod';

const config = loadConfig({
  extend: z.object({
    ALLOWED_IPS: csvString.default([]),
  }),
});
// ALLOWED_IPS="1.2.3.4,5.6.7.8" => ['1.2.3.4', '5.6.7.8']

On this page