Custom Auth Parameters

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

In case 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:

1import { Composio } from "@composio/core";
2
3const composio = new Composio({
4 apiKey: process.env.COMPOSIO_API_KEY,
5});

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.

1const authModifier = (toolSlug: string, toolkitSlug: string, params: any) => {
2 // Add authentication parameters for specific toolkits
3 if (toolkitSlug === "NOTION") {
4 if (!params.customAuthParams) {
5 params.customAuthParams = {};
6 }
7
8 if (!params.customAuthParams.parameters) {
9 params.customAuthParams.parameters = [];
10 }
11
12 // Add an API key to the headers
13 params.customAuthParams.parameters.push({
14 in: "header",
15 name: "X-API-Key",
16 value: process.env.CUSTOM_API_KEY,
17 });
18 }
19 return params;
20};

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.

1const result = await composio.tools.execute(
2 "NOTION_GET_DATABASE_ITEMS",
3 {
4 userId: "sid",
5 arguments: {
6 database_id: "1234567890",
7 },
8 },
9 {
10 beforeExecute: authModifier,
11 }
12);
13
14console.log(JSON.stringify(result, null, 2));