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

  1. Provide both flags: 'choco apikey add -s <source> -k <key>'.
  2. For user:password sources, pass 'user:password' as the -k value.
  3. 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

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


AI-assisted analysis of chocolatey/choco@0d5abdd10c (2026-08-13). Data as JSON: /api/errors/8add3e451d6ba3e3. Report an issue: GitHub.