chocolatey/choco · error · ApplicationException
You must specify both 'source' and 'key' to set an API key.
Error message
You must specify both 'source' and 'key' to set an API key.
What it means
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.
Source
Thrown at src/chocolatey/infrastructure.app/commands/ChocolateyApiKeyCommand.cs:100
configuration.ApiKeyCommand.Command = command;
}
public virtual void Validate(ChocolateyConfiguration configuration)
{
switch (configuration.ApiKeyCommand.Command)
{
case ApiKeyCommandType.Remove:
if (string.IsNullOrWhiteSpace(configuration.Sources))
{
throw new ApplicationException("You must specify 'source' to remove an API key.");
}
break;
case ApiKeyCommandType.Add:
if (string.IsNullOrWhiteSpace(configuration.Sources) || string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key))
{
throw new ApplicationException("You must specify both 'source' and 'key' to set an API key.");
}
break;
}
}
public virtual void HelpMessage(ChocolateyConfiguration configuration)
{
this.Log().Info(ChocolateyLoggers.Important, "ApiKey Command");
this.Log().Info(@"
This lists API keys that are set or sets an api key for a particular
source so it doesn't need to be specified every time.
Anything that doesn't contain source and key will list API keys.
");
"chocolatey".Log().Info(ChocolateyLoggers.Important, "Usage");
"chocolatey".Log().Info(@"
choco apikey [<options/switches>]
View on GitHub (pinned to 0d5abdd10c)
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.
Example fix
# before choco apikey add -s https://feed # after choco apikey add -s https://feed -k 123-123123-123
Defensive patterns
Strategy: validation
Validate before calling
// Require both source and key for add.
if (verb == "add" && (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(key)))
{
throw new InvalidOperationException("apikey add requires both -s and -k");
}
RunChoco($"apikey add -s \"{source}\" -k \"{key}\""); Prevention
- 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.
When it happens
Trigger: Running 'choco apikey add' with only -s (no -k), only -k (no -s), or neither.
Common situations: 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.
Related errors
- You must specify 'source' to remove an API key.
- A single apikey command must be listed. Please see the help
- A single config command must be listed. Please see the help
- When specifying the subcommand '{0}', you must also specify
- When specifying the subcommand '{0}', you must also specify
AI-assisted analysis of chocolatey/choco@0d5abdd10c (2026-08-13).
Data as JSON: /api/errors/8add3e451d6ba3e3.
Report an issue: GitHub.