# Introduction

Read environment variables as typed values, and stop the app at startup when one is missing or malformed.

Every value in `process.env` is a string, so you've written lines like these before:

```ts
const port = Number(process.env.PORT ?? 3000);
const debug = process.env.DEBUG === 'true';
const cacheTtl = Number(process.env.CACHE_TTL ?? 60_000);
const databaseUrl = process.env.DATABASE_URL;
```

Each line breaks on a value you didn't expect:

- `port` is `NaN` for `PORT=80a`, and `0` for `PORT=` because `??` keeps an empty string.
- `debug` is `false` for `DEBUG=1`.
- `cacheTtl` is `NaN` for `CACHE_TTL=10m`.
- `databaseUrl` is a `string | undefined`, so you still check it and parse the URL yourself.

None of these stop the app. It starts with the bad value and fails later, somewhere far from the line that read it. Every file that reads one of these variables also carries its own copy of the line.

## Stop at startup on a bad value

[`getRequiredAll`](/docs/required-values) reads a group of variables and converts each one to the type you ask for. If any are missing or can't be converted, it throws one error that lists every bad key:

```ts title="config.ts"
export const config = Envapter.getRequiredAll({
    DATABASE_URL: Converters.Url,
    PORT: Converters.Port,
    CACHE_TTL: Converters.Time
});

config.CACHE_TTL;
```

```txt title="terminal"
$ PORT=80a CACHE_TTL=soon DATABASE_URL=postgres://db/app node config.ts
EnvaptError [210]: Validation failed for PORT, CACHE_TTL.
```

`PORT=80a` and `CACHE_TTL=soon` both show up in that one error, so you fix both before the next deploy. When the values are good, `config.PORT` is a `number`, `config.DATABASE_URL` is a `URL`, and `CACHE_TTL=10m` comes back as `600000` milliseconds. If your entry file imports `config` first, a bad deploy stops before the app serves anything.

## Fall back to a default

Some values have a safe default, like a debug flag or a timeout. [`Envapter.getAs`](/docs/envapter) takes the key, a [converter](/docs/converters), and the default:

```ts
const debug = Envapter.getAs('DEBUG', Converters.Boolean, false);
const timeout = Envapter.getAs('REQUEST_TIMEOUT', Converters.Time, '30s');
```

`Converters.Boolean` reads `1`, `yes`, `true`, and `on` as true, so `DEBUG=1` gives `true`. `timeout` is `30000` when `REQUEST_TIMEOUT` is unset. A fallback also removes `undefined` from the type, so `debug` is a `boolean`, while a read with no fallback, like `Envapter.getAs('DATABASE_URL', Converters.Url)`, is `URL | undefined` and you handle the missing case yourself.

The default also covers a value that can't convert, so `REQUEST_TIMEOUT=soon` gives `30000` with no error. Use `getRequiredAll` for anything where a typo should stop the app. [Fallbacks and missing values](/docs/envapter#fallbacks-and-missing-values) has the exact rules.

Reading through envapt is optional, and code that reads `process.env` directly keeps working. A script that reads one string can stay on `process.env`. envapt helps most once several files read config, since one call gives each read its type, its default, and its check.

## Functions or decorators

[`Envapter`](/docs/envapter) reads one value per call, as above. The [`@Envapt`](/docs/decorators) decorator reads a variable into a class field. A decorated field reads on first access and keeps that value until the cache is rebuilt, for example by a `useSource` call ([When values are read](/docs/sources#when-values-are-read)). A reader call converts again each time.

```ts
class Config {
    @Envapt('PORT', { converter: Converters.Port, fallback: 3000 })
    static accessor port: number;

    @Envapt('REDIS_URL', { converter: Converters.Url, fallback: new URL('redis://localhost:6379') })
    static accessor redisUrl: URL;
}
```

Use a decorator for settings that many files read, since the class declares each one once and `Config.port` works anywhere. Use a reader for a one-off read, or for a check that has to run at startup, since a decorated field reads only when something first accesses it. The cost is the `accessor` keyword on every field, which [Decorators](/docs/decorators) explains. Both forms share the same [converters](/docs/converters) and accept a [Standard Schema](/docs/standard-schema) validator such as zod, valibot, or arktype. A decorator takes the schema through its `schema` option.

## Where values come from

A source is the object envapt reads values from. On Node, Bun, and Deno the source is `process.env` plus your `.env` files. It binds when you import `envapt`, so the first read works with no setup. If other code reads `process.env` directly, import `envapt/config` to put your `.env` values there too ([.env files on Node](/docs/env-files#coming-from-dotenv)).

On Workers, in the browser, and on edge runtimes, envapt reads nothing until you bind a source. Your config arrives there as an object, such as the Workers `env` or `import.meta.env` in a bundler like Vite. Wrap that object in a [`PortableSource`](/docs/sources#the-sources-envapt-ships) and pass it to `Envapter.useSource` once, before the first read. If a read runs before that, it throws `NoSourceBound`. [Sources](/docs/sources) shows the full setup.

## Where to start

- [Node, Bun, and Deno](/docs/quick-start): Install envapt and read a typed value from your .env file.
- [Cloudflare Workers](/docs/workers): Bind the Worker env object once and read typed config.
- [Browser](/docs/browser): Read typed values from the config your bundler injects.
- [Edge runtimes](/docs/edge-runtimes): Bind the platform's config object on Vercel Edge, Fastly, and similar.
- [Upgrading from v8](/docs/migrations): What changed in v9 and how to upgrade.