Shared API Keys

Red has a central API key storage utilising the core bots config. This allows cog creators to add a single location to store API keys for their cogs which may be shared between other cogs.

There needs to be some consistency between cog creators when using shared API keys between cogs. To help make this easier service should be all lowercase and the key names should match the naming convention of the API being accessed.


Twitch has a client ID and client secret so a user should be asked to input

[p]set api twitch client_id,1234ksdjf client_secret,1234aldlfkd

and when accessed in the code it should be done by


Each service has its own dict of key, value pairs for each required key type. If there’s only one key required then a name for the key is still required for storing and accessing.


[p]set api youtube api_key,1234ksdjf

and when accessed in the code it should be done by


Basic Usage

class MyCog:
    async def youtube(self, ctx, user: str):
        youtube_keys = await"youtube")
        if youtube_keys.get("api_key") is None:
            return await ctx.send("The YouTube API key has not been set.")
        # Use the API key to access content as you normally would

Event Reference

on_red_api_tokens_update(service_name, api_tokens)

Dispatched when service’s api keys are updated.

  • service_name (str) – Name of the service.
  • api_tokens (Mapping[str, str]) – New Mapping of token names to tokens. This contains api tokens that weren’t changed too.

Additional References


Gets the shared API tokens for a service

Parameters:service_name (str) – The service to get tokens for.
Returns:A Mapping of token names to tokens. This mapping exists because some services have multiple tokens.
Return type:Dict[str, str]
Red.set_shared_api_tokens(service_name, **tokens)

Sets shared API tokens for a service

In most cases, this should not be used. Users should instead be using the set api command

This will not clear existing values not specified.

  • service_name (str) – The service to set tokens for
  • **tokens – token_name -> token


Setting the api_key for youtube from a value in a variable my_key

>>> await"youtube", api_key=my_key)
Red.remove_shared_api_tokens(service_name, *token_names)

Removes shared API tokens

  • service_name (str) – The service to remove tokens for
  • *token_names (str) – The name of each token to be removed


Removing the api_key for youtube

>>> await"youtube", "api_key")