Custom Auth Parameters

Inject custom credentials in headers or parameters

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:

from composio import Composio

composio = Composio()
import { Composio } from "@composio/core";

const composio = new Composio({
  apiKey: process.env.COMPOSIO_API_KEY,
});

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.

This is a Before Execute Modifier!

Before Execute Modifiers are a way to modify the parameters of a tool before it is executed. In this case, they are useful for adding custom authentication headers or parameters to a tool.

from composio import before_execute
from composio.types import ToolExecuteParams


@before_execute(toolkits=["NOTION"])
def add_custom_auth(
    tool: str,
    toolkit: str,
    params: ToolExecuteParams,
) -> ToolExecuteParams:
    if params["custom_auth_params"] is None:
        params["custom_auth_params"] = {"parameters": []}

    params["custom_auth_params"]["parameters"].append(
        {
            "name": "x-api-key",
            "value": os.getenv("NOTION_API_KEY"),
            "in": "header",
        }
    )
    return params
const authModifier = (toolSlug: string, toolkitSlug: string, params: any) => {
  // Add authentication parameters for specific toolkits
  if (toolkitSlug === "NOTION") {
    if (!params.customAuthParams) {
      params.customAuthParams = {};
    }

    if (!params.customAuthParams.parameters) {
      params.customAuthParams.parameters = [];
    }

    // Add an API key to the headers
    params.customAuthParams.parameters.push({
      in: "header",
      name: "X-API-Key",
      value: process.env.CUSTOM_API_KEY,
    });
  }
  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 Modifying tool inputs.

result = composio.tools.execute(
    slug="NOTION_GET_DATABASE_ITEMS",
    user_id="default",
    arguments={},
    modifiers=[
        add_custom_auth,
    ],
)
print(result)
const result = await composio.tools.execute(
  "NOTION_GET_DATABASE_ITEMS",
  {
    userId: "sid",
    arguments: {
      database_id: "1234567890",
    },
  },
  {
    beforeExecute: authModifier,
  }
);

console.log(JSON.stringify(result, null, 2));