TypeScript API: Keys and environment
Loading API keys from a file or the environment. Generated from the declarations and doc comments of criogenai 0.1.7. See also: TypeScript API overview, the same module in Python.
import { parseEnv, LoadEnvOptions, loadEnv, envStatus, keyName } from "criogenai";Contents#
| Name | Kind | Summary |
|---|---|---|
parseEnv | function | Parse .env text: NAME=value lines, export NAME=value, # comments, and values in single or double quotes. |
LoadEnvOptions | interface | Options of loadEnv. |
loadEnv | function | Read a .env file into process.env and return the NAMES it set (sorted; never the values). |
envStatus | function | Which providers have a key set right now, by provider name. |
keyName | function | The environment variable a provider's key comes from ("openai" -> "OPENAI_API_KEY"). |
parseEnv#
function parseEnv(text: string): Record<string, string>Parse .env text: NAME=value lines, export NAME=value, # comments, and
values in single or double quotes. No variable expansion. A malformed line
throws an Error naming the line number (never its content: it may be a secret).
It does not touch process.env; loadEnv does.
Parameters
text(string): The content of a .env file.
Returns: The variables, by name.
Raises
Error: A line is notNAME=value, or a quoted value is not closed.
Example
import { parseEnv } from "criogenai";
parseEnv("# keys\nOPENAI_API_KEY=sk-test\nexport GEMINI_API_KEY='g-test'\n");
// { OPENAI_API_KEY: "sk-test", GEMINI_API_KEY: "g-test" }LoadEnvOptions#
interface LoadEnvOptions {
override?: boolean;
required?: boolean;
}Options of loadEnv.
Fields
override(boolean, optional): Replace variables that are already set (default: keep them).required(boolean, optional): Throw if the file is missing (default: a missing file is fine).
loadEnv#
function loadEnv(path?: string, options?: LoadEnvOptions): string[]Read a .env file into process.env and return the NAMES it set (sorted; never the values). Files over 1 MB are refused. Keep the file out of git.
Call it before you build a model: model objects and "provider:model" strings read their key from the environment when they are created. Node only (a browser has no environment variables).
Parameters
path(string, default".env"): The file to read, relative to the current directory.options(LoadEnvOptions, default{}):overridereplaces variables already set;requiredmakes a missing file an error.
Returns: The names of the variables it set, sorted.
Raises
Error: The file is over 1 MB, a line is malformed, or (withrequired) the file is missing.
Example
import { Agent, loadEnv } from "criogenai";
loadEnv(); // reads ./.env, e.g. OPENAI_API_KEY=...
const agent = new Agent({ model: "openai:gpt-5-mini" });envStatus#
function envStatus(): Record<string, boolean>Which providers have a key set right now, by provider name.
Names and yes/no only, never a key: { openai: true, gemini: false, ... }.
Local providers (ollama, lmstudio) need no key and are not listed.
keyName#
function keyName(provider: string): stringThe environment variable a provider's key comes from ("openai" -> "OPENAI_API_KEY").
Parameters
provider(string): A provider name, e.g. "openai" or "qwen" (local providers such as "ollama" need no key and are not known here).
Raises
Error: The provider is not one with a key.