{"record":{"id":"e031e066b910dc72","repo":"chocolatey/choco","slug":"an-api-key-was-not-found-for-0-you-must-eithe","errorCode":null,"errorMessage":"An API key was not found for '{0}'. You must either set an API key with the apikey command or specify one with --api-key.","messagePattern":"An API key was not found for '(.+?)'\\. You must either set an API key with the apikey command or specify one with --api-key\\.","errorType":"validation","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"src/chocolatey/infrastructure.app/commands/ChocolateyPushCommand.cs","lineNumber":92,"sourceCode":"                    throw new ApplicationException(\"The default push source configuration is not set. Either pass a source to push to, such as `--source=\\\"'{0}'\\\"`, or set the `defaultPushSource` configuration value, for example `choco config set --name=\\\"'defaultPushSource'\\\" --value=\\\"'{0}'\\\"`.\".FormatWith(ApplicationParameters.ChocolateyCommunityFeedPushSource));\r\n                }\r\n            }\r\n\r\n            IEnumerable<string> sources = configuration.Sources.Split(new[] { \";\", \",\" }, StringSplitOptions.RemoveEmptyEntries);\r\n\r\n            if (sources.Count() > 1)\r\n            {\r\n                throw new ApplicationException(\"Multiple sources are not supported by push command.\");\r\n            }\r\n\r\n            var remoteSource = new Uri(configuration.Sources);\r\n            if (string.IsNullOrWhiteSpace(configuration.PushCommand.Key) && !remoteSource.IsUnc && !remoteSource.IsFile)\r\n            {\r\n                // perform a lookup\r\n                configuration.PushCommand.Key = _configSettingsService.GetApiKey(configuration, null);\r\n                if (string.IsNullOrWhiteSpace(configuration.PushCommand.Key))\r\n                {\r\n                    throw new ApplicationException(\"An API key was not found for '{0}'. You must either set an API key with the apikey command or specify one with --api-key.\".FormatWith(configuration.Sources));\r\n                }\r\n            }\r\n\r\n            // security advisory\r\n            if (!configuration.Force || configuration.Sources.ToLowerSafe().Contains(\"chocolatey.org\"))\r\n            {\r\n                if (remoteSource.Scheme == \"http\" && remoteSource.Host != \"localhost\")\r\n                {\r\n                    var errorMessage =\r\n                        @\"WARNING! The specified source '{0}' is not secure.\r\n Sending apikey over insecure channels leaves your data susceptible to\r\n hackers. Please update your source to a more secure source and try again.\r\n\r\n Use --force if you understand the implications of this warning or are\r\n accessing an internal feed. If you are however doing this against an\r\n internet feed, then the choco gods think you are crazy. ;-)\r\n\r\nNOTE: For chocolatey.org, you must update the source to be secure.\".FormatWith(configuration.Sources);\r","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/chocolatey/infrastructure.app/commands/ChocolateyPushCommand.cs#L74-L110","documentation":"Thrown by ChocolateyPushCommand.Validate when pushing to a remote (non-UNC, non-file) source and no API key is available. The command tries to look up a stored key via _configSettingsService.GetApiKey for the given source; if that returns null/empty and --api-key was not passed, authentication is impossible and the push is rejected.","triggerScenarios":"Running 'choco push' against a remote HTTPS feed without --api-key and without having previously stored a key via 'choco apikey'. The condition is: PushCommand.Key is empty AND source is not UNC AND source is not a file path AND GetApiKey returns empty.","commonSituations":"First push to the community feed or an internal feed without prior 'choco apikey -k <key> --source <source>'. Also in CI/CD pipelines where the API key was expected to be pre-configured but was not, or the stored key is for a different source URL.","solutions":["Store the key first: 'choco apikey --key=\"<your-api-key>\" --source=\"https://push.chocolatey.org/\"', then run 'choco push'.","Or pass the key inline: 'choco push --api-key=\"<your-api-key>\" --source=\"https://push.chocolatey.org/\"'.","In CI/CD, set the API key as an environment variable or pipeline secret and pass it via --api-key.","Verify the stored key matches the exact source URL: 'choco apikey'."],"exampleFix":"// before\nchoco push --source=\"https://push.chocolatey.org/\"\n// after\nchoco apikey --key=\"abcd1234\" --source=\"https://push.chocolatey.org/\"\nchoco push --source=\"https://push.chocolatey.org/\"","handlingStrategy":"validation","validationCode":"// Ensure an API key is available before pushing\nif (string.IsNullOrWhiteSpace(apiKey) && string.IsNullOrWhiteSpace(GetStoredApiKey(source)))\n{\n    Console.Error.WriteLine($\"No API key for '{source}'. Set one with: choco apikey --key=<key> --source={source}\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    RunChoco($\"push --source={source}\");\n}\ncatch (ApplicationException ex) when (ex.Message.Contains(\"API key was not found\"))\n{\n    logger.Error(\"No API key configured. Run 'choco apikey' first.\");\n    // prompt user or fail the pipeline\n}","preventionTips":["Store the API key with 'choco apikey --key=<key> --source=<url>' before pushing.","In CI/CD, pass --api-key from a secret variable.","Verify stored keys with 'choco apikey' periodically."],"tags":["chocolatey","push-command","api-key","authentication"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}