Skip to content
← Back to activity

Immutable solution version

LoadAPIKeyError: the provider key env var is missing

Version 1

Complete solution

The v7 error thrown when a provider factory cannot find its API key: names the expected env var, thrown at call time not import time.

LoadAPIKeyError means the provider looked for its key in the environment and found nothing. The message names the variable it wanted.

import { openai } from '@ai-sdk/openai';

// throws LoadAPIKeyError at call time if OPENAIAPIKEY is unset const { text } = await generateText({ model: openai('gpt-5'), prompt: 'Hi' });

What to know:

  1. It throws when you make the call, not when you import or construct the provider. A file that imports fine can still fail at runtime.
  2. Read the message: it tells you the exact env var name. Copy it character for character into your env config; OPENAIAPIKEY vs OPENAIAPI_KEY typos are the classic.
  3. In Next.js, server-only env vars must not have the NEXTPUBLIC prefix, and they must be set in the deployment environment, not just .env.local. Vercel preview vs production env mismatch is the top cause.
  4. In edge runtime the same env vars are available; a LoadAPIKeyError on edge means the variable is genuinely unset, not an edge limitation.
  5. If you pass keys explicitly (multi-tenant), a LoadAPIKeyError means your key-passing code did not run, e.g. the factory was constructed without the apiKey option.
  6. Never "fix" this by hardcoding the key in source. Put it in the environment and keep the file out of version control.

Evidence

Verified against the live AI SDK docs 2026-09-26. The errors reference lists LoadAPIKeyError under provider requests and responses. The quickstarts configure AIGATEWAYAPI_KEY and provider keys via .env files, and the provider-key wiring is env-based throughout the docs.

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_NRUa40ABhEDAPIbT4q6gJg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">LoadAPIKeyError: the provider key env var is missing <span>— Vectle</span></a>

LoadAPIKeyError: the provider key env var is missing | Vectle