Skip to content

CIRCE / DOCUMENTATION

Environment

Validated environment variables and the credentials contract.

Documentation menu
On this page

defineConfig(schema) validates process.env at startup. If anything is invalid, Circe lists every problem and exits with code 1.

Credentials contract

The validated result must contain token and clientId, and may contain guildId. The CLI uses them to log in and register commands. Environment variables are usually SCREAMING_CASE, so map them:

// src/config.ts
import { defineConfig } from "@sxnnyside/circe";
import { z } from "zod";

export default defineConfig(
  z
    .object({
      DISCORD_TOKEN: z.string().min(1),
      DISCORD_CLIENT_ID: z.string().min(1),
      DISCORD_GUILD_ID: z.string().optional(),
      DATABASE_URL: z.string().url().optional(),
    })
    .transform((env) => ({
      token: env.DISCORD_TOKEN,
      clientId: env.DISCORD_CLIENT_ID,
      guildId: env.DISCORD_GUILD_ID,
      databaseUrl: env.DATABASE_URL,
    })),
);

Without the mapping, token is undefined and login fails with An invalid token was provided. circe init 1.2.0 templates use the raw names; add the transform. Since 1.2.1 the templates map them, and the CLI falls back to DISCORD_TOKEN, DISCORD_CLIENT_ID and DISCORD_GUILD_ID.

guildId

Set: commands register for that server and appear instantly. Unset: they register globally, slower to propagate. Use it in development, unset it in production.

Schema libraries

Function Accepts
defineConfig A Zod schema.
defineEnvConfig Any schema with safeParse: Valibot, ArkType, Standard Schema v1.

Both return an object with .load(source?).

.env files

The CLI loads .env from the project folder. Variables already in the process environment win.