{"record":{"id":"1a8d79330c727eb1","repo":"can1357/oh-my-pi","slug":"azure-openai-api-key-is-required-set-azure-openai","errorCode":null,"errorMessage":"Azure OpenAI API key is required. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument.","messagePattern":"Azure OpenAI API key is required\\. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument\\.","errorType":"exception","errorClass":"AIError.MissingApiKeyError","httpStatus":null,"severity":"error","filePath":"packages/ai/src/providers/azure-openai-responses.ts","lineNumber":345,"sourceCode":"\n/**\n * Replicates the `AzureOpenAI` SDK client's request shape for `/responses`:\n * a string api key becomes a single `api-key` header (azure.mjs `authHeaders`;\n * never `Authorization: Bearer`), `api-version` rides as a query parameter\n * (azure.mjs constructor `defaultQuery`), and `/responses` is not a\n * deployment-scoped path, so no `/deployments/{model}` URL rewriting applies.\n * Custom model/options headers may override the auth header, matching the SDK's\n * `buildHeaders` precedence.\n */\nfunction buildAzureResponsesRequest(\n\tmodel: Model<\"azure-openai-responses\">,\n\tapiKey: string,\n\toptions?: AzureOpenAIResponsesOptions,\n): { url: string; headers: Record<string, string>; baseUrl: string } {\n\tif (!apiKey) {\n\t\tconst envKey = $env.AZURE_OPENAI_API_KEY;\n\t\tif (!envKey) {\n\t\t\tthrow new AIError.MissingApiKeyError(\n\t\t\t\tundefined,\n\t\t\t\t\"Azure OpenAI API key is required. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument.\",\n\t\t\t);\n\t\t}\n\t\tapiKey = envKey;\n\t}\n\n\tconst headers: Record<string, string> = { \"api-key\": apiKey, ...(model.headers ?? {}) };\n\tif (options?.headers) {\n\t\tObject.assign(headers, options.headers);\n\t}\n\n\tconst { baseUrl, apiVersion } = resolveAzureConfig(model, options);\n\n\treturn {\n\t\turl: `${baseUrl}/responses?api-version=${encodeURIComponent(apiVersion)}`,\n\t\theaders,\n\t\tbaseUrl,","sourceCodeStart":327,"sourceCodeEnd":363,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/ai/src/providers/azure-openai-responses.ts#L327-L363","documentation":"buildAzureResponsesRequest requires an API key for Azure OpenAI authentication. When the apiKey argument is empty/undefined it falls back to the AZURE_OPENAI_API_KEY environment variable; if that is also missing it throws MissingApiKeyError. Azure OpenAI uses api-key header auth, so no request can be signed without it.","triggerScenarios":"Streaming from an azure-openai-responses model without passing options.apiKey and with AZURE_OPENAI_API_KEY unset in the environment; passing an empty-string apiKey which is falsy and still triggers the env fallback.","commonSituations":"New developer onboarding without the shared .env; CI secrets not injected; switching from OAuth/Entra ID setups where no static key exists; key defined under a different env name (e.g. AZURE_API_KEY).","solutions":["Set the AZURE_OPENAI_API_KEY environment variable to a valid key from your Azure OpenAI resource (Keys and Endpoint blade).","Pass the key explicitly: options.apiKey = \"<your-key>\" at the call site.","If you authenticate via Microsoft Entra ID instead of keys, use a provider path/token provider that supplies the credential rather than relying on the env var.","Check CI/CD secret configuration so AZURE_OPENAI_API_KEY is exported into the process environment."],"exampleFix":"// before\nawait streamAzureOpenAIResponses(model, context); // no key\n// after\nawait streamAzureOpenAIResponses(model, context, {\n  apiKey: process.env.AZURE_OPENAI_API_KEY,\n});","handlingStrategy":"validation","validationCode":"const apiKey = options?.apiKey ?? process.env.AZURE_OPENAI_API_KEY;\nif (!apiKey) throw new Error(\"AZURE_OPENAI_API_KEY must be set or an apiKey option provided before calling Azure OpenAI.\");","typeGuard":"null","tryCatchPattern":"try {\n  await streamAzureOpenAIResponses(model, ctx, options);\n} catch (err) {\n  if (err instanceof AIError.MissingApiKeyError) {\n    // prompt user / fail fast with credential setup instructions\n  } else throw err;\n}","preventionTips":["Validate AZURE_OPENAI_API_KEY presence during app bootstrap.","Never pass an empty-string key — it is falsy and still triggers the error.","Inject secrets via your deployment's secret manager and confirm they reach process.env."],"tags":["authentication","api-key","azure-openai","missing-env-var"],"backgroundTag":"missing-api-key","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}