vercel/ai · error · LoadAPIKeyError

KlingAI API key is missing. Pass it using the 'apiKey' param

Error message

KlingAI API key is missing. Pass it using the 'apiKey' parameter or the KLINGAI_API_KEY environment variable. Alternatively, pass the legacy 'accessKey' and 'secretKey' parameters or the KLINGAI_ACCESS_KEY and KLINGAI_SECRET_KEY environment variables.

What it means

resolveKlingAIAuthToken throws LoadAPIKeyError when no KlingAI credentials can be found: neither an apiKey parameter nor KLINGAI_API_KEY env var, nor the legacy accessKey/secretKey pair (params or KLINGAI_ACCESS_KEY/KLINGAI_SECRET_KEY env vars). The provider cannot authenticate without one of these.

Source

Thrown at packages/klingai/src/klingai-auth.ts:83

    settingValue: undefined,
    environmentVariableName: 'KLINGAI_API_KEY',
  });
  if (environmentApiKey) {
    return environmentApiKey;
  }

  const hasLegacyCredentials =
    loadTrimmedSetting({
      settingValue: accessKey,
      environmentVariableName: 'KLINGAI_ACCESS_KEY',
    }) != null ||
    loadTrimmedSetting({
      settingValue: secretKey,
      environmentVariableName: 'KLINGAI_SECRET_KEY',
    }) != null;

  if (!hasLegacyCredentials) {
    throw new LoadAPIKeyError({
      message:
        "KlingAI API key is missing. Pass it using the 'apiKey' parameter " +
        'or the KLINGAI_API_KEY environment variable. Alternatively, pass the ' +
        "legacy 'accessKey' and 'secretKey' parameters or the " +
        'KLINGAI_ACCESS_KEY and KLINGAI_SECRET_KEY environment variables.',
    });
  }

  return generateKlingAIAuthToken({ accessKey, secretKey });
}

/**
 * Generate a JWT authentication token for KlingAI API access from a legacy
 * access key / secret key pair.
 *
 * Uses HS256 (HMAC-SHA256) signing via the Web Crypto API — no external
 * dependencies required. Compatible with Node.js, Edge, and browser runtimes.
 *

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Set the KLINGAI_API_KEY environment variable (or pass apiKey: '...' to createKlingAI) with your KlingAI API key.
  2. Alternatively provide the legacy pair: both KLINGAI_ACCESS_KEY and KLINGAI_SECRET_KEY env vars, or accessKey/secretKey parameters.
  3. Verify the env vars are actually loaded in your runtime (dotenv/secret manager) and contain non-whitespace values.

Example fix

// before
const klingai = createKlingAI({}); // no credentials anywhere
// after
const klingai = createKlingAI({ apiKey: process.env.KLINGAI_API_KEY }); // or export KLINGAI_API_KEY=...
Defensive patterns

Strategy: validation

Validate before calling

const hasKlingAICredentials =
  !!process.env.KLINGAI_API_KEY ||
  (!!process.env.KLINGAI_ACCESS_KEY && !!process.env.KLINGAI_SECRET_KEY);
if (!hasKlingAICredentials) {
  throw new Error('Set KLINGAI_API_KEY (or KLINGAI_ACCESS_KEY + KLINGAI_SECRET_KEY) before using KlingAI.');
}

Try / catch

import { LoadAPIKeyError } from '@ai-sdk/provider';
try {
  await generateVideo({ model: klingai.video('kling-v2.6-t2v'), prompt });
} catch (e) {
  if (LoadAPIKeyError.isInstance(e)) {
    console.error('KlingAI credentials missing:', e.message);
  } else throw e;
}

Prevention

When it happens

Trigger: Creating or calling a KlingAI model (e.g. klingai.video(...)) in an environment where none of apiKey / KLINGAI_API_KEY / accessKey+secretKey / KLINGAI_ACCESS_KEY+KLINGAI_SECRET_KEY are set, or where the env vars hold only whitespace (they are trimmed and empty counts as missing).

Common situations: Deploying to CI/production where .env is not loaded; forgetting to export KLINGAI_API_KEY; setting only one of the legacy pair (both access key AND secret key are required); typos in env var names.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/efc0f75eed25c40d. Report an issue: GitHub.