personal-lifecycle-tracker / .agents / skills / neon / references / parse-env.md
parse-env.md
Raw

Type-safe env vars with parseEnv

@neon/env's parseEnv takes your neon.ts config object and returns a parsed, typed env object, validated against the services you declared. The shape of env follows your config, and missing variables are flagged with clear errors.

npm i @neon/env
import { parseEnv } from "@neon/env";
import config from "./neon";

const env = parseEnv(config);

console.log(env.postgres.databaseUrl);
console.log(env.auth.baseUrl);

By default parseEnv requires every variable your config implies. When one of your apps only uses a subset, for example when you need to read DATABASE_URL but never the unpooled URL, pass an array of env-var keys to require and validate only those. The keys are typesafe: autocomplete only offers variables your config enables, and the returned shape is narrowed to exactly what you selected (so unselected variables are neither enforced nor present).

import { parseEnv } from "@neon/env";
import config from "./neon";

// Only DATABASE_URL is required and returned; DATABASE_URL_UNPOOLED is not enforced.
const { postgres } = parseEnv(config, ["DATABASE_URL"]);
console.log(postgres.databaseUrl);

// Selecting across services — only these keys are validated.
const env = parseEnv(config, ["DATABASE_URL", "NEON_AUTH_BASE_URL"]);
console.log(env.postgres.databaseUrl, env.auth.baseUrl);