> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nanoforge.eu/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment

> Read and validate environment variables in your game

Environment variables configure a game without changing its code: a server address, a port, a game mode.

## Set them

Put them in a `.env` file at the root of the project. Their names start with `NANOFORGE_`, and the prefix is removed before the game sees them: `NANOFORGE_GAME_MODE` becomes `GAME_MODE`. In the `.env` file, `NANOFORGE_CLIENT_X` is meant for the client and `NANOFORGE_SERVER_X` for the server; both become `X`. See [Environment variables](https://docs.nanoforge.eu/docs/cli/configuration/environment-variables.md) in the CLI docs.

Never put a secret in a variable the client receives: the client runs in the player's browser. Put server-only values in the `.env` file with the `NANOFORGE_SERVER_` prefix, and never export secrets in the shell or container environment that runs `nf start` or `nf dev`.

## Read them

The run options carry the variables as raw strings: `options.env` in `main`, and `ctx.env` in a library's `__init`. To check them and convert them to typed values, describe them with a class and pass it to `registerEnv` from `@nanoforge-dev/env`:

```ts theme={null}
declare module "@nanoforge-dev/common" {
  interface Context {
    rules: { mode: "coop" | "versus"; debug: boolean };
  }
}

class GameEnv {
  @Expose()
  @Default("coop")
  @IsIn(["coop", "versus"])
  GAME_MODE!: "coop" | "versus";

  @Expose()
  @TransformToBoolean()
  @IsBoolean()
  @Default(false)
  DEBUG!: boolean;
}

export class RulesLibrary extends Library {
  readonly key = defineLibraryKey("rules");

  private _env?: GameEnv;

  public override async __init(ctx: InitContext): Promise<void> {
    this._env = await registerEnv(GameEnv, ctx.env);
  }

  public override expose() {
    if (!this._env) this.throwNotInitializedError();
    return { mode: this._env.GAME_MODE, debug: this._env.DEBUG };
  }
}
```

* The `declare module` block types `ctx.rules`: see [Writing your own library](https://docs.nanoforge.eu/docs/engine/getting-started/concepts.md#writing-your-own-library).
* Mark every variable with `@Expose()`: the others are dropped.
* `@Default(value)` gives the value of a missing variable. `@TransformToBoolean()` turns `"true"` and `"false"` into booleans. `@IsIpOrURL()` checks an address.
* The package re-exports the [class-validator](https://github.com/typestack/class-validator) decorators (`@IsIn`, `@IsPort`, `@IsString`, `@IsOptional`...) and `Transform` from class-transformer.
* `registerEnv` throws `NfEnvValidationException` with every failed check, so a bad value stops the game at start-up instead of later.

The network library reads its own variables this way: see [Network](https://docs.nanoforge.eu/docs/engine/network/index.md#environment-variables).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.