{"record":{"id":"ae910a3091626483","repo":"microsoft/autogen","slug":"bing-api-key-environment-variable-is-not-set-ae910a","errorCode":null,"errorMessage":"BING_API_KEY environment variable is not set","messagePattern":"BING_API_KEY environment variable is not set","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs","lineNumber":16,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// Use_Bing_Search_With_Semantic_Kernel_Agent.cs\n\nusing AutoGen.Core;\nusing AutoGen.SemanticKernel.Extension;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Plugins.Web;\nusing Microsoft.SemanticKernel.Plugins.Web.Bing;\n\nnamespace AutoGen.SemanticKernel.Sample;\n\npublic class Use_Bing_Search_With_Semantic_Kernel_Agent\n{\n    public static async Task RunAsync()\n    {\n        var bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\") ?? throw new Exception(\"BING_API_KEY environment variable is not set\");\n        var bingSearch = new BingConnector(bingApiKey);\n        var webSearchPlugin = new WebSearchEnginePlugin(bingSearch);\n\n        var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n        var modelId = \"gpt-3.5-turbo\";\n        var kernelBuilder = Kernel.CreateBuilder()\n            .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);\n        kernelBuilder.Plugins.AddFromObject(webSearchPlugin);\n\n        var kernel = kernelBuilder.Build();\n\n        var skAgent = new SemanticKernelAgent(\n            kernel: kernel,\n            name: \"assistant\",\n            systemMessage: \"You are a helpful AI assistant\")\n            .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.\n            .RegisterPrintMessage(); // pretty print the message to the console\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs#L1-L34","documentation":"Thrown by the Bing-search Semantic Kernel sample when BING_API_KEY is not set. The sample constructs a BingConnector with the raw key to power a WebSearchEnginePlugin, and the guard fires before any web search call is attempted. Bing Web Search keys come from an Azure Bing Search resource, not from OpenAI.","triggerScenarios":"Running Use_Bing_Search_With_Semantic_Kernel_Agent.RunAsync() without BING_API_KEY in the environment; the 'new BingConnector(bingApiKey)' line executes first, so the guard precedes it.","commonSituations":"Developer set OPENAI_API_KEY but forgot the separate Bing key; Bing Search resource not created in Azure; key expired or copied from the wrong blade (endpoint vs keys); Bing Search APIs retired/deprecated so the key no longer exists.","solutions":["Create (or locate) an Azure 'Bing Search' resource and copy a key, then export BING_API_KEY=<key>.","If Bing Search is unavailable in your subscription, swap BingConnector for another ISearchEngineConnector the sample supports (e.g. GoogleConnector with GOOGLE_API_KEY / SEARCH_ENGINE_ID).","Verify with echo $BING_API_KEY before rerunning the sample.","For IDE runs, add BING_API_KEY to the sample project's launchSettings.json environmentVariables."],"exampleFix":"// before\nvar bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\") ?? throw new Exception(\"BING_API_KEY environment variable is not set\");\n\n// after (fail fast with a pointer to where the key comes from)\nvar bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\")\n    ?? throw new InvalidOperationException(\"BING_API_KEY is not set. Create an Azure Bing Search resource and export one of its keys.\");","handlingStrategy":"validation","validationCode":"var bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\");\nif (string.IsNullOrWhiteSpace(bingApiKey))\n{\n    Console.Error.WriteLine(\"BING_API_KEY missing. Create an Azure Bing Search resource and set it.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Document multi-key samples with a single 'required environment variables' table (this sample needs BING_API_KEY and OPENAI_API_KEY).","Add a preflight check that validates every required key before constructing any connector."],"tags":["bing","web-search","semantic-kernel","configuration","environment-variables","sample"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}