Skip to content

API key by host

  1. apiKey is set: send it, and nothing else.

  2. Else apiKeyEnv is set: read that env var (the variable’s name, never its value, goes in your code).

  3. Else read only the env var of the baseUrl host, over https:

    host env var
    api.openai.com OPENAI_API_KEY
    api.anthropic.com ANTHROPIC_API_KEY
    openrouter.ai (and subdomains) OPENROUTER_API_KEY
  4. Any other host, or plain http, without apiKey / apiKeyEnv: an error. There is no cross-provider fallback.

listModels follows the same rule.

// local server that ignores the key: any placeholder
const local = createClient({ baseUrl: "http://localhost:11434/v1", style: "openai", model: "llama3", apiKey: "unused" })
// proxy: name the env var to read
const proxy = createClient({ baseUrl: "https://llm.internal/v1", style: "openai", model: "m", apiKeyEnv: "INTERNAL_LLM_KEY" })

A hosted provider needs neither: baseUrl: "https://openrouter.ai/api/v1" with OPENROUTER_API_KEY in the environment works as before.

The full rule, with runnable examples, is on each port’s API page, e.g. JavaScript, Python, Go, Java, C#, Elixir, Clojure.