{"record":{"id":"7b020532a4f15aaa","repo":"microsoft/autogen","slug":"bing-api-key-environment-variable-is-not-set","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.Basic.Sample/Example11_Sequential_GroupChat_Example.cs","lineNumber":26,"sourceCode":"using AutoGen.SemanticKernel;\nusing AutoGen.SemanticKernel.Extension;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Plugins.Web;\nusing Microsoft.SemanticKernel.Plugins.Web.Bing;\n#endregion using_statement\n\nnamespace AutoGen.Basic.Sample;\n\npublic partial class Sequential_GroupChat_Example\n{\n    public static async Task<IAgent> CreateBingSearchAgentAsync()\n    {\n        #region CreateBingSearchAgent\n        var config = LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo();\n        var apiKey = config.ApiKey;\n        var kernelBuilder = Kernel.CreateBuilder()\n            .AddAzureOpenAIChatCompletion(config.DeploymentName, config.Endpoint, apiKey);\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        kernelBuilder.Plugins.AddFromObject(webSearchPlugin);\n\n        var kernel = kernelBuilder.Build();\n        var kernelAgent = new SemanticKernelAgent(\n            kernel: kernel,\n            name: \"bing-search\",\n            systemMessage: \"\"\"\n            You search results from Bing and return it as-is.\n            You put the original search result between ```bing and ```\n\n            e.g.\n            ```bing\n            xxx\n            ```\n            \"\"\")\n            .RegisterMessageConnector()","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/Example11_Sequential_GroupChat_Example.cs#L8-L44","documentation":"Guard in the Bing search agent factory of the sequential group-chat sample: after reading the Azure OpenAI config, it fetches BING_API_KEY and throws if it is absent, because the WebSearchEnginePlugin's BingConnector requires a Bing Search (Azure Cognitive Services / Bing Search v7) key for every query. Note the Azure OpenAI variables were already validated at this point — only the Bing key is missing.","triggerScenarios":"Calling Sequential_GroupChat_Example.CreateBingSearchAgentAsync() when BING_API_KEY is unset, while (typically) AZURE_OPENAI_API_KEY/ENDPOINT/DEPLOY_NAME are already set because LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo() ran first without throwing.","commonSituations":"Developers who configured OpenAI/Azure OpenAI keys but skipped provisioning a Bing Search resource on Azure; the Bing Search API being retired/renamed so a new key was never created; CI secrets file listing only the OpenAI variables.","solutions":["Provision a Bing Search / Azure Cognitive Services Bing Search v7 resource and set BING_API_KEY in the environment before running the sample.","If you cannot get a Bing key, substitute another WebSearchEngineConnector supported by the Kernel (e.g. GoogleConnector) and set the corresponding environment variable instead.","Verify the key works with a direct BingConnector call before wiring it into the group chat.","Keep AZURE_OPENAI_* variables set as well — this sample needs both Azure OpenAI and Bing credentials."],"exampleFix":"// before\nvar bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\") ?? throw new Exception(\"BING_API_KEY environment variable is not set\");\n\n// after\nvar bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\")\n    ?? throw new InvalidOperationException(\"BING_API_KEY is not set. Create a Bing Search v7 resource and export its key, or swap in another WebSearchEngineConnector.\");","handlingStrategy":"validation","validationCode":"var bingApiKey = Environment.GetEnvironmentVariable(\"BING_API_KEY\");\nif (string.IsNullOrWhiteSpace(bingApiKey))\n{\n    Console.Error.WriteLine(\"BING_API_KEY is required for the Bing search agent. Provision a Bing Search v7 key or use another connector.\");\n    return null;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var bingAgent = await CreateBingSearchAgentAsync();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"BING_API_KEY\"))\n{\n    Console.Error.WriteLine($\"Missing Bing credential: {ex.Message}\");\n}","preventionTips":["Inventory every external service a sample uses (here Azure OpenAI AND Bing) and set all keys before running.","Prefer a WebSearchEngineConnector you already have credentials for and adjust the variable name accordingly.","Smoke-test third-party keys (Bing, search APIs) with a direct call before embedding them in multi-agent flows."],"tags":["configuration","environment-variables","bing","web-search","azure","dotnet","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}