{"record":{"id":"aff0ab7f10a6bdb3","repo":"upstash/context7","slug":"authentication-is-required-pass-apikey-or-authtoken-or-set","errorCode":null,"errorMessage":"Authentication is required. Pass apiKey or authToken, or set CONTEXT7_API_KEY.","messagePattern":"Authentication is required\\. Pass apiKey or authToken, or set CONTEXT7_API_KEY\\.","errorType":"exception","errorClass":"Context7Error","httpStatus":null,"severity":"error","filePath":"packages/sdk/src/client.ts","lineNumber":151,"sourceCode":"   */\n  async getContext(\n    query: string,\n    libraryId: string,\n    options?: GetContextOptions\n  ): Promise<Documentation[] | string> {\n    const command = new GetContextCommand(query, libraryId, options);\n    return command.exec(this.httpClient);\n  }\n}\n\nfunction resolveCredential(config: Context7Config): Credential {\n  if (config.apiKey) return { kind: \"apiKey\", value: config.apiKey };\n  if (config.authToken) return { kind: \"authToken\", value: config.authToken };\n\n  const environmentApiKey = getEnvironmentApiKey();\n  if (environmentApiKey) return { kind: \"apiKey\", value: environmentApiKey };\n\n  throw new Context7Error(\n    \"Authentication is required. Pass apiKey or authToken, or set CONTEXT7_API_KEY.\"\n  );\n}\n\nfunction getEnvironmentApiKey(): string | undefined {\n  return typeof process === \"undefined\" ? undefined : process.env?.CONTEXT7_API_KEY;\n}\n\nfunction withoutAuthorizationHeader(headers?: Record<string, string>): Record<string, string> {\n  return Object.fromEntries(\n    Object.entries(headers ?? {}).filter(([name]) => name.toLowerCase() !== \"authorization\")\n  );\n}\n","sourceCodeStart":133,"sourceCodeEnd":165,"githubUrl":"https://github.com/upstash/context7/blob/4416fb855b8f752be735e34f943b5d0762701aad/packages/sdk/src/client.ts#L133-L165","documentation":"resolveCredential builds the client credential by checking config.apiKey, then config.authToken, then the CONTEXT7_API_KEY environment variable. If none is present it throws Context7Error because every API request requires authentication. The error is deliberate and immediate so misconfigured clients fail before making network calls.","triggerScenarios":"Constructing the Context7 client with no apiKey and no authToken in the config object while CONTEXT7_API_KEY is unset (or process is unavailable, e.g. in some edge/non-Node runtimes).","commonSituations":"Forgetting to pass credentials after migrating from an anonymous usage pattern; .env file not loaded (dotenv not initialized); CI/CD missing the CONTEXT7_API_KEY secret; typo like CONTEXT_API_KEY; browser bundle where process.env is undefined.","solutions":["Pass apiKey in the client config: new Context7Client({ apiKey: 'ctx7sk_...' })","Pass an authToken if you authenticate via a bearer token instead","Set the CONTEXT7_API_KEY environment variable and ensure your .env is actually loaded (import 'dotenv/config')","Verify the variable name is exactly CONTEXT7_API_KEY and that the secret is present in your deployment environment"],"exampleFix":"// before\nconst client = new Context7Client({});\n// after\nconst client = new Context7Client({ apiKey: process.env.CONTEXT7_API_KEY });","handlingStrategy":"validation","validationCode":"const apiKey = config.apiKey ?? process.env.CONTEXT7_API_KEY;\nif (!apiKey && !config.authToken) throw new Error('CONTEXT7: no credential configured');","typeGuard":"function hasCredential(c: { apiKey?: string; authToken?: string }): boolean {\n  return Boolean(c.apiKey || c.authToken || (typeof process !== 'undefined' && process.env?.CONTEXT7_API_KEY));\n}","tryCatchPattern":"let client;\ntry {\n  client = new Context7Client(config);\n} catch (e) {\n  if (e instanceof Context7Error && e.message.startsWith('Authentication is required')) {\n    console.error('Set CONTEXT7_API_KEY or pass apiKey/authToken');\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Load dotenv (or your runtime's env mechanism) before constructing the client","Keep credential names consistent: CONTEXT7_API_KEY exactly, in every environment","Add a startup check that fails fast with a clear message when credentials are absent","Never rely on ambient credentials in CI; explicitly inject the secret"],"tags":["authentication","config","api-key","client"],"backgroundTag":"authentication-required","analyzedSha":"4416fb855b8f752be735e34f943b5d0762701aad","analyzedAt":"2026-09-16T20:28:07.148Z","contentChangedAt":"2026-09-16T20:28:07.148Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}