{"record":{"id":"efc0f75eed25c40d","repo":"vercel/ai","slug":"klingai-api-key-is-missing-pass-it-using-the-api","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"LoadAPIKeyError","httpStatus":null,"severity":"error","filePath":"packages/klingai/src/klingai-auth.ts","lineNumber":83,"sourceCode":"    settingValue: undefined,\n    environmentVariableName: 'KLINGAI_API_KEY',\n  });\n  if (environmentApiKey) {\n    return environmentApiKey;\n  }\n\n  const hasLegacyCredentials =\n    loadTrimmedSetting({\n      settingValue: accessKey,\n      environmentVariableName: 'KLINGAI_ACCESS_KEY',\n    }) != null ||\n    loadTrimmedSetting({\n      settingValue: secretKey,\n      environmentVariableName: 'KLINGAI_SECRET_KEY',\n    }) != null;\n\n  if (!hasLegacyCredentials) {\n    throw new LoadAPIKeyError({\n      message:\n        \"KlingAI API key is missing. Pass it using the 'apiKey' parameter \" +\n        'or the KLINGAI_API_KEY environment variable. Alternatively, pass the ' +\n        \"legacy 'accessKey' and 'secretKey' parameters or the \" +\n        'KLINGAI_ACCESS_KEY and KLINGAI_SECRET_KEY environment variables.',\n    });\n  }\n\n  return generateKlingAIAuthToken({ accessKey, secretKey });\n}\n\n/**\n * Generate a JWT authentication token for KlingAI API access from a legacy\n * access key / secret key pair.\n *\n * Uses HS256 (HMAC-SHA256) signing via the Web Crypto API — no external\n * dependencies required. Compatible with Node.js, Edge, and browser runtimes.\n *","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/klingai/src/klingai-auth.ts#L65-L101","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Set the KLINGAI_API_KEY environment variable (or pass apiKey: '...' to createKlingAI) with your KlingAI API key.","Alternatively provide the legacy pair: both KLINGAI_ACCESS_KEY and KLINGAI_SECRET_KEY env vars, or accessKey/secretKey parameters.","Verify the env vars are actually loaded in your runtime (dotenv/secret manager) and contain non-whitespace values."],"exampleFix":"// before\nconst klingai = createKlingAI({}); // no credentials anywhere\n// after\nconst klingai = createKlingAI({ apiKey: process.env.KLINGAI_API_KEY }); // or export KLINGAI_API_KEY=...","handlingStrategy":"validation","validationCode":"const hasKlingAICredentials =\n  !!process.env.KLINGAI_API_KEY ||\n  (!!process.env.KLINGAI_ACCESS_KEY && !!process.env.KLINGAI_SECRET_KEY);\nif (!hasKlingAICredentials) {\n  throw new Error('Set KLINGAI_API_KEY (or KLINGAI_ACCESS_KEY + KLINGAI_SECRET_KEY) before using KlingAI.');\n}","typeGuard":null,"tryCatchPattern":"import { LoadAPIKeyError } from '@ai-sdk/provider';\ntry {\n  await generateVideo({ model: klingai.video('kling-v2.6-t2v'), prompt });\n} catch (e) {\n  if (LoadAPIKeyError.isInstance(e)) {\n    console.error('KlingAI credentials missing:', e.message);\n  } else throw e;\n}","preventionTips":["Set KLINGAI_API_KEY in every environment (dev, CI, prod) — remember .env files are not auto-loaded in production.","If using legacy auth, provide BOTH accessKey and secretKey; one alone is insufficient.","Fail fast at app startup with a credential presence check rather than at first model call."],"tags":["missing-api-key","authentication","env-var"],"backgroundTag":"missing-api-key","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}