Custom Auth Parameters

Guide to injecting custom credentials in headers or parameters for a toolkit

In cases where Composio is not being used for managing the auth but only for the tools, it is possible to use the beforeExecute hook to inject custom auth headers or parameters for a toolkit.

Setup and Initialization

First, initialize the Composio SDK with your API key:

1from composio import Composio
2
3composio = Composio()

Creating the Auth Modifier Function

Define a function that modifies authentication parameters for specific toolkits. This function checks the toolkit name and adds custom authentication headers when needed.

1from composio import before_execute
2from composio.types import ToolExecuteParams
3
4
5@before_execute(toolkits=["NOTION"])
6def add_custom_auth(
7 tool: str,
8 toolkit: str,
9 params: ToolExecuteParams,
10) -> ToolExecuteParams:
11 if params["custom_auth_params"] is None:
12 params["custom_auth_params"] = {"parameters": []}
13
14 params["custom_auth_params"]["parameters"].append(
15 {
16 "name": "x-api-key",
17 "value": os.getenv("NOTION_API_KEY"),
18 "in": "header",
19 }
20 )
21 return params

Executing Tools with Custom Auth

Execute the tool using the custom authentication modifier. The beforeExecute hook allows you to modify parameters before the tool runs.

Following is an example of how to execute a tool with a custom authentication modifier for Completion Providers.

For Agentic Providers, read about Before Execution Modifiers.

1result = composio.tools.execute(
2 slug="NOTION_GET_DATABASE_ITEMS",
3 user_id="default",
4 arguments={},
5 modifiers=[
6 add_custom_auth,
7 ],
8)
9print(result)