ApiKeyIntegration is designed for services that authenticate using a simple API key. It handles the retrieval of this key, typically from a configured Store, and makes it available to the Application.
How ApiKeyIntegration Works
When an Application configured with an ApiKeyIntegration needs to make an authenticated request, the APIApplication’s _get_headers() method (or similar for GraphQLApplication) will call integration.get_credentials().
The ApiKeyIntegration then:
- Attempts to retrieve the API key from its associated
storeusing itsnameas the key. - If the key is found, it’s returned, typically in a dictionary like
{"api_key": "your_actual_api_key"}. - The
Applicationthen usually includes this as a Bearer token in theAuthorizationheader (e.g.,Authorization: Bearer your_actual_api_key). - If the key is not found in the store, a
NotAuthorizedErroris raised, which includes instructions (fromauthorize()) on how to set the API key.
Initializing ApiKeyIntegration
You initialize ApiKeyIntegration with a name and a Store instance:
API Key Name Sanitization
TheApiKeyIntegration uses a helper function sanitize_api_key_name when it’s initialized. This function ensures consistency in how API key names are formatted, typically by converting the provided name to uppercase and appending _API_KEY if it’s not already in that format.
For example:
"myservice"becomes"MYSERVICE_API_KEY""my_service_key"becomes"MY_SERVICE_KEY_API_KEY""ANOTHER_API_KEY"remains"ANOTHER_API_KEY"
Store. So, if you initialize with ApiKeyIntegration(name="tavily", store=EnvironmentStore()), it will look for an environment variable named TAVILY_API_KEY.
Configuration in local_config.json
When defining an ApiKeyIntegration within your server configuration file for a LocalServer, you’d structure it like this:
LocalServer loads this AppConfig, integration_from_config will be called, which in turn instantiates ApiKeyIntegration(name="TAVILY", store=EnvironmentStore()).
Setting Credentials
WhileApiKeyIntegration primarily retrieves credentials, if you need to set them programmatically (especially for MemoryStore or KeyringStore during setup), you can use the store’s set method:
ApiKeyIntegration.authorize() method provides a message guiding the user to set the API key in the store if it’s missing.