{"record":{"id":"fe2a2d21b48da1f2","repo":"microsoft/autogen","slug":"please-set-openai-api-key-environment-variable","errorCode":null,"errorMessage":"Please set OPENAI_API_KEY environment variable.","messagePattern":"Please set OPENAI_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/README.md","lineNumber":27,"sourceCode":"You can install both new and old packages from the following feeds:\n\n[![dotnet-ci](https://github.com/microsoft/autogen/actions/workflows/dotnet-build.yml/badge.svg)](https://github.com/microsoft/autogen/actions/workflows/dotnet-build.yml)\n[![NuGet version](https://badge.fury.io/nu/AutoGen.Core.svg)](https://badge.fury.io/nu/AutoGen.Core)\n\n> [!NOTE]\n> Nightly build is available at:\n>\n> - [![Static Badge](https://img.shields.io/badge/azure_devops-grey?style=flat)](https://dev.azure.com/AGPublish/AGPublic/_artifacts/feed/AutoGen-Nightly) : <https://pkgs.dev.azure.com/AGPublish/AGPublic/_packaging/AutoGen-Nightly/nuget/v3/index.json>\n\nFirstly, following the [installation guide](./website/articles/Installation.md) to install AutoGen packages.\n\nThen you can start with the following code snippet to create a conversable agent and chat with it.\n\n```csharp\nusing AutoGen;\nusing AutoGen.OpenAI;\n\nvar openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\nvar gpt35Config = new OpenAIConfig(openAIKey, \"gpt-3.5-turbo\");\n\nvar assistantAgent = new AssistantAgent(\n    name: \"assistant\",\n    systemMessage: \"You are an assistant that help user to do some tasks.\",\n    llmConfig: new ConversableAgentConfig\n    {\n        Temperature = 0,\n        ConfigList = [gpt35Config],\n    })\n    .RegisterPrintMessage(); // register a hook to print message nicely to console\n\n// set human input mode to ALWAYS so that user always provide input\nvar userProxyAgent = new UserProxyAgent(\n    name: \"user\",\n    humanInputMode: HumanInputMode.ALWAYS)\n    .RegisterPrintMessage();\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/README.md#L9-L45","documentation":"Exception thrown in the dotnet README quickstart snippet when the OPENAI_API_KEY environment variable is null. It is a deliberate guard before constructing OpenAIConfig, so the sample fails fast instead of sending an invalid credential to OpenAI.","triggerScenarios":"Running the README example without setting OPENAI_API_KEY in the process environment (new shell, dotnet run without env, CI without secrets).","commonSituations":"Developer copies the snippet into a fresh console app and runs it before exporting the key; env var set in one shell but run in another (IDE/VS Code terminal doesn't inherit); key stored only in appsettings.json or user-secrets instead of the environment.","solutions":["Export the variable in the shell you run from: export OPENAI_API_KEY=sk-... (or setx / VS Code launchSettings.json environmentVariables on Windows).","Restart the IDE/terminal after setting it so the process picks up the new environment.","For .NET apps, alternatively load it from user-secrets: dotnet user-secrets set \"OPENAI_API_KEY\" \"sk-...\" and read configuration instead of the raw environment.","Verify with: echo $OPENAI_API_KEY (or echo %OPENAI_API_KEY% on cmd) before dotnet run."],"exampleFix":"// before\nvar openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\") ?? throw new Exception(\"Please set OPENAI_API_KEY environment variable.\");\n\n// after (fail fast with a clear message, validated early)\nvar openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n    ?? throw new InvalidOperationException(\n        \"OPENAI_API_KEY is not set. Run: export OPENAI_API_KEY=<your-key>\");","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")))\n{\n    Console.Error.WriteLine(\"Set OPENAI_API_KEY first: export OPENAI_API_KEY=sk-...\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var openAIKey = Environment.GetEnvironmentVariable(\"OPENAI_API_KEY\")\n        ?? throw new InvalidOperationException(\"Please set OPENAI_API_KEY environment variable.\");\n}\ncatch (InvalidOperationException e) when (e.Message.Contains(\"OPENAI_API_KEY\"))\n{\n    Console.Error.WriteLine(e.Message);\n    Environment.Exit(1);\n}","preventionTips":["Set required env vars in a startup script or launchSettings.json.","Prefer dotnet user-secrets over raw env vars for local development.","Fail fast at app start with a checklist of missing credentials."],"tags":["environment","api-key","dotnet","sample","openai"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}