# @dzangolab/fastify-config

> Fastify config plugin

Latest version **0.87.0** (published 2025-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dzangolab/fastify-config
pnpm add @dzangolab/fastify-config
yarn add @dzangolab/fastify-config
bun add @dzangolab/fastify-config
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.87.0 |
| Published | 2025-07-21 |
| First published | 2022-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 55.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Maintainers | opichon, uddhab, rameshlohala, cpxpratik |

## Links

- npm: https://www.npmjs.com/package/@dzangolab/fastify-config
- Repository: https://github.com/dzangolab/fastify
- Homepage: https://github.com/dzangolab/fastify/tree/main/packages/config#readme
- Issues: https://github.com/dzangolab/fastify/issues
- npm.io page: https://npm.io/package/@dzangolab/fastify-config

## Recent versions

- 0.87.0 (latest) — 2025-07-21
- 0.3.3 (hotfix) — 2022-12-04
- 0.86.1 — 2025-07-16
- 0.86.0 — 2025-07-07
- 0.85.1 — 2025-07-04
- 0.85.0 — 2025-07-02
- 0.84.4 — 2025-06-20
- 0.84.3 — 2025-06-09
- 0.84.2 — 2025-05-22
- 0.84.1 — 2025-05-19
- 0.84.0 — 2025-05-14
- 0.83.0 — 2025-05-09
- 0.82.0 — 2025-05-08
- 0.81.0 — 2025-04-28
- 0.80.1 — 2025-04-23
- … 204 more at https://npm.io/package/@dzangolab/fastify-config/versions

## README

# @dzangolab/fastify-config

A [Fastify](https://github.com/fastify/fastify) plugin that defines an opinionated config for an API.

When registered on a Fastify instance, the plugin will:

* decorate the Fastify instance with the `config` object, available with the `config` attribute.
* decorate all requests with the `config` object, available with the `config` attribute; this can be used to construct a `buildContext` for mercurius resolvers, for example.
* decorate the Fastify instance with a `hostname` attribute.

## Installation

Install with npm:

```bash
npm install @dzangolab/fastify-config
```

Install with pnpm:

```bash
pnpm add --filter "@scope/project @dzangolab/fastify-config
```

## Usage

Somewhere in your code, create a `config.ts` file that looks like this:

```typescript
import { parse } from "@dzangolab/fastify-config";
import dotenv from "dotenv";

import type { ApiConfig } from "@dzangolab/fastify-config";

dotenv.config();

const config: ApiConfig = {
  appName: process.env.APP_NAME as string,
  appOrigin: (process.env.APP_ORIGIN as string).split(","),
  baseUrl: process.env.BASE_URL as string,
  env: parse(process.env.NODE_ENV, "development") as string,
  logger: {
    level: parse(process.env.LOG_LEVEL, "error") as string,
  },
  name: process.env.NAME as string,
  pagination: {
    default_limit: parse(process.env.PAGINATION_DEFAULT_LIMIT, 25) as number,
    max_limit: parse(process.env.PAGINATION_MAX_LIMIT, 50) as number,
  },
  port: parse(process.env.PORT, 20040) as number,
  protocol: parse(process.env.PROTOCOL, "http") as string,
  rest: {
    enabled: parse(process.env.REST_ENABLED, true) as boolean,
  },
  version: `${process.env.npm_package_version || process.env.API_VERSION}+${process.env.API_BUILD || "local"}` as string,
};

export default config;
```

Register the plugin with your Fastify instance:

```typescript
import configPlugin from "@dzangolab/fastify-config";
import Fastify from "fastify";

import config from "./config";

const start = async () => {
  // Create fastify instance
  const fastify = Fastify({
    logger: config.logger,
  });

  // Register fastify-config plugin
  await fastify.register(configPlugin, { config });

  await fastify.listen({
    port: config.port,
    host: "0.0.0.0",
  });
};

start();
```

---
_Source: https://npm.io/package/@dzangolab/fastify-config · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
