{"record":{"id":"30603e5fd44ff5a5","repo":"upstash/context7","slug":"api-key-is-required-pass-it-in-the-config-or-set","errorCode":null,"errorMessage":"API key is required. Pass it in the config or set CONTEXT7_API_KEY environment variable.","messagePattern":"API key is required\\. Pass it in the config or set CONTEXT7_API_KEY environment variable\\.","errorType":"validation","errorClass":"Context7Error","httpStatus":null,"severity":"error","filePath":"packages/sdk/src/client.ts","lineNumber":31,"sourceCode":"const API_KEY_PREFIX = \"ctx7sk\";\n\nexport type * from \"@commands/types\";\nexport type {\n  CacheSetting,\n  Context7Fetch,\n  Context7ResponseMetadata,\n  RateLimitMetadata,\n  RetryConfig,\n} from \"@http\";\nexport * from \"@error\";\n\nexport class Context7 {\n  private readonly httpClient: HttpClient;\n\n  constructor(config: Context7Config = {}) {\n    const apiKey = config.apiKey || getEnvironmentApiKey();\n\n    if (!apiKey) {\n      throw new Context7Error(\n        \"API key is required. Pass it in the config or set CONTEXT7_API_KEY environment variable.\"\n      );\n    }\n\n    if (!apiKey.startsWith(API_KEY_PREFIX)) {\n      console.warn(`API key should start with '${API_KEY_PREFIX}'`);\n    }\n\n    this.httpClient = new HttpClient({\n      baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,\n      headers: {\n        ...withoutAuthorizationHeader(config.headers),\n        Authorization: `Bearer ${apiKey}`,\n      },\n      retry: config.retry,\n      cache: config.cache ?? \"no-store\",\n      timeout: config.timeout,","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/upstash/context7/blob/80e681a507c5287bc12e483367c40754e29461b9/packages/sdk/src/client.ts#L13-L49","documentation":"The `Context7` SDK constructor resolves the API key as `config.apiKey || process.env.CONTEXT7_API_KEY` and throws `Context7Error` when both are falsy. A key that does not start with `ctx7sk` only triggers a console warning, not this throw — only a fully missing key does.","triggerScenarios":"`new Context7()` or `new Context7({})` with CONTEXT7_API_KEY unset; passing `apiKey: \"\"`; the env var being set in a different shell/PM2/CI context than the process; dotenv loading after construction.","commonSituations":"Missing `.env` entry or `.env` not loaded before import side effects; CI secret not exposed to the job; deploying with a different user that lacks the env var; key stored under a differently-named variable.","solutions":["Pass the key explicitly: `new Context7({ apiKey: 'ctx7sk_...' })`","Or `export CONTEXT7_API_KEY=ctx7sk_...` in the environment that runs the process","If using dotenv, ensure `import 'dotenv/config'` runs before constructing the client","Verify with `process.env.CONTEXT7_API_KEY` logging right before construction"],"exampleFix":"// before\nimport 'dotenv/config';\nconst client = new Context7(); // throws if .env lacks the key\n\n// after\nimport 'dotenv/config';\nconst client = new Context7({ apiKey: process.env.CONTEXT7_API_KEY! });","handlingStrategy":"validation","validationCode":"const apiKey = process.env.CONTEXT7_API_KEY;\nif (!apiKey) {\n  throw new Error('CONTEXT7_API_KEY is not set — add it to .env or the process environment');\n}\nconst client = new Context7({ apiKey });","typeGuard":"function hasApiKey(cfg: { apiKey?: string } | undefined): cfg is { apiKey: string } {\n  return Boolean(cfg?.apiKey) || Boolean(process.env.CONTEXT7_API_KEY);\n}","tryCatchPattern":null,"preventionTips":["Load dotenv (`import 'dotenv/config'`) before constructing the client","Expose CONTEXT7_API_KEY as a CI/CD secret on every job that uses the SDK","Construct the client once in a factory that validates config at startup, not deep in request paths"],"tags":["sdk","authentication","api-key","environment","typescript"],"backgroundTag":"missing-env-var","analyzedSha":"80e681a507c5287bc12e483367c40754e29461b9","analyzedAt":"2026-09-08T05:18:41.043Z","contentChangedAt":"2026-09-08T05:18:41.043Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}