chocolatey/choco · error · ApplicationException

You must specify 'source' to remove an API key.

Error message

You must specify 'source' to remove an API key.

What it means

Validate() requires a source when removing an API key, because removal targets a specific source. With ApiKeyCommand.Remove and a blank configuration.Sources it throws, telling the user to specify 'source'.

Source

Thrown at src/chocolatey/infrastructure.app/commands/ChocolateyApiKeyCommand.cs:93

                }
                else if (!string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key))
                {
                    this.Log().Warn("API key provided. Setting command to add.");
                    command = ApiKeyCommandType.Add;
                }
            }

            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

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Supply the source: 'choco apikey remove -s https://feed'.
  2. Confirm the exact source string with 'choco apikey list' first so the name matches the stored entry.
  3. Quote URLs that contain special characters.

Example fix

# before
choco apikey remove

# after
choco apikey remove -s https://push.chocolatey.org/
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a source is present before issuing a remove.
if (verb == "remove" && string.IsNullOrWhiteSpace(source))
{
    throw new InvalidOperationException("apikey remove requires -s <source>");
}
RunChoco($"apikey remove -s \"{source}\"");

Prevention

When it happens

Trigger: Running 'choco apikey remove' (or 'setapikey remove') without -s/--source, so configuration.Sources is null/whitespace.

Common situations: Forgetting the -s flag on removal; assuming remove works on all keys at once; copy-paste from an add example that dropped the source.

Related errors


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