{"record":{"id":"8add3e451d6ba3e3","repo":"chocolatey/choco","slug":"you-must-specify-both-source-and-key-to-set-an","errorCode":null,"errorMessage":"You must specify both 'source' and 'key' to set an API key.","messagePattern":"You must specify both 'source' and 'key' to set an API key\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"src/chocolatey/infrastructure.app/commands/ChocolateyApiKeyCommand.cs","lineNumber":100,"sourceCode":"\r\n            configuration.ApiKeyCommand.Command = command;\r\n        }\r\n\r\n        public virtual void Validate(ChocolateyConfiguration configuration)\r\n        {\r\n            switch (configuration.ApiKeyCommand.Command)\r\n            {\r\n                case ApiKeyCommandType.Remove:\r\n                    if (string.IsNullOrWhiteSpace(configuration.Sources))\r\n                    {\r\n                        throw new ApplicationException(\"You must specify 'source' to remove an API key.\");\r\n                    }\r\n\r\n                    break;\r\n                case ApiKeyCommandType.Add:\r\n                    if (string.IsNullOrWhiteSpace(configuration.Sources) || string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key))\r\n                    {\r\n                        throw new ApplicationException(\"You must specify both 'source' and 'key' to set an API key.\");\r\n                    }\r\n\r\n                    break;\r\n            }\r\n        }\r\n\r\n        public virtual void HelpMessage(ChocolateyConfiguration configuration)\r\n        {\r\n            this.Log().Info(ChocolateyLoggers.Important, \"ApiKey Command\");\r\n            this.Log().Info(@\"\r\nThis lists API keys that are set or sets an api key for a particular\r\n source so it doesn't need to be specified every time.\r\n\r\nAnything that doesn't contain source and key will list API keys.\r\n\");\r\n            \"chocolatey\".Log().Info(ChocolateyLoggers.Important, \"Usage\");\r\n            \"chocolatey\".Log().Info(@\"\r\n    choco apikey [<options/switches>]\r","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/chocolatey/infrastructure.app/commands/ChocolateyApiKeyCommand.cs#L82-L118","documentation":"Validate() requires BOTH a source and a key when adding/setting an API key. With ApiKeyCommand.Add, if either configuration.Sources or configuration.ApiKeyCommand.Key is blank, it throws. The command must know which source to bind the key to and what the key value is.","triggerScenarios":"Running 'choco apikey add' with only -s (no -k), only -k (no -s), or neither.","commonSituations":"Typing the add verb then forgetting one of the two required flags; passing the key positionally instead of via -k; trailing whitespace making a value effectively empty.","solutions":["Provide both flags: 'choco apikey add -s <source> -k <key>'.","For user:password sources, pass 'user:password' as the -k value.","Double-check that neither value is empty or only whitespace."],"exampleFix":"# before\nchoco apikey add -s https://feed\n\n# after\nchoco apikey add -s https://feed -k 123-123123-123","handlingStrategy":"validation","validationCode":"// Require both source and key for add.\nif (verb == \"add\" && (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(key)))\n{\n    throw new InvalidOperationException(\"apikey add requires both -s and -k\");\n}\nRunChoco($\"apikey add -s \\\"{source}\\\" -k \\\"{key}\\\"\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate both -s and -k are non-empty in CI before calling choco.","For user:password keys, confirm the format matches your source's expectations."],"tags":["apikey","add","source","key","validation"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}