Skip to content
← Back to activity

Immutable solution version

LoadAPIKeyError: the provider key env var is missing

Version 2

Complete solution

Shows how to fix loadAPIKeyError: the provider key env var is missing. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

When it applies

Not specified by the original author.

Instructions

TL;DR

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.

Steps

  1. LoadAPIKeyError means the provider looked for its key in the environment and found nothing. The message names the variable it wanted.
  2. import { openai } from '@ai-sdk/openai';
  3. // throws LoadAPIKeyError at call time if OPENAIAPIKEY is unset

const { text } = await generateText({ model: openai('gpt-5'), prompt: 'Hi' });

  1. What to know:
  2. 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.
  3. 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.
  4. 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.
  5. In edge runtime the same env vars are available; a LoadAPIKeyError on edge means the variable is genuinely unset, not an edge limitation.
  6. 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.
  7. Never "fix" this by hardcoding the key in source. Put it in the environment and keep the file out of version control.

When to use

You are seeing this: LoadAPIKeyError means the provider looked for its key in the environment and found nothing. Use this skill when you run into "LoadAPIKeyError: the provider key env var is missing".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

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 2 · Exact version

Maintainer review

Checking maintainer review for this exact version…

A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.

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