upstash/context7 · warning

API key should start with

Error message

API key should start with '${API_KEY_PREFIX}'

What it means

The Context7 SDK client constructor warns when an API key credential does not start with the expected API_KEY_PREFIX. It is a non-fatal warning: the client is still constructed and the key is sent as-is, but authentication will likely fail if the key is malformed.

Solutions

  1. Copy the API key exactly as shown in the Context7 dashboard, including its prefix.
  2. Strip any manually added "Bearer " or surrounding whitespace before passing it.
  3. Confirm you are using an API key, not another credential type (e.g. OAuth token).
  4. If using a non-key credential intentionally, pass it via headers/config so it is not treated as an apiKey.

Example fix

// before
const client = new Context7({ apiKey: 'Bearer abc123' });
// after
const client = new Context7({ apiKey: process.env.CONTEXT7_API_KEY!.trim() }); // value starts with the expected prefix
Defensive patterns

Strategy: validation

Validate before calling

const key = process.env.CONTEXT7_API_KEY;
if (!key || !key.startsWith(API_KEY_PREFIX)) throw new Error('CONTEXT7_API_KEY must start with ' + API_KEY_PREFIX);

Type guard

const isValidApiKey = (v: string): v is `ctx7sk-${string}` => v.startsWith('ctx7sk-');

Prevention

When it happens

Trigger: Creating `new Context7({ apiKey })` where resolveCredential returns kind "apiKey" and the value does not start with API_KEY_PREFIX (e.g. passing a raw token, an OAuth secret, or a key with extra whitespace/characters).

Common situations: Copy-pasting the wrong credential type from the dashboard; including a "Bearer " prefix manually; trimming/pasting errors; confusion between API keys and other token formats.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16). Data as JSON: /api/errors/48e27f185c06f43f. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/src/client.ts:38

export type * from "@commands/types";
export type {
  AuthTokenProvider,
  CacheSetting,
  Context7Fetch,
  Context7ResponseMetadata,
  RateLimitMetadata,
  RetryConfig,
} from "@http";
export * from "@error";

export class Context7 {
  private readonly httpClient: HttpClient;

  constructor(config: Context7Config = {}) {
    const credential = resolveCredential(config);

    if (credential.kind === "apiKey" && !credential.value.startsWith(API_KEY_PREFIX)) {
      console.warn(`API key should start with '${API_KEY_PREFIX}'`);
    }

    this.httpClient = new HttpClient({
      baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
      headers: {
        ...withoutAuthorizationHeader(config.headers),
      },
      authToken: credential.value,
      retry: config.retry,
      cache: config.cache ?? "no-store",
      timeout: config.timeout,
      signal: config.signal,
      keepAlive: config.keepAlive,
      fetch: config.fetch,
      onResponse: config.onResponse,
    });
  }

View on GitHub (pinned to 4416fb855b)