Custom Auth

Execute tools with custom auth

Composio supports multiple authentication schemes, and a connection can be created with any of these.

Authentication MethodDescription
OAuth 2.0Requires an authorization URL, token URL, and scopes for user authentication.
OAuth 1.0An older version of OAuth, requiring a request token and access token for user authentication.
API KeyUses a static API key, optionally with an API secret, included in request headers or query parameters.
Basic AuthenticationRequires a username and password for authentication, typically sent in an Authorization header.
Bearer TokenUses a token in the Authorization header, often retrieved from OAuth 2.0 or another identity provider.
Basic with JWTA hybrid approach that combines basic authentication (username/password) with JWT-based token authentication.
No AuthenticationSome APIs do not require authentication, allowing open access to endpoints.

Auth with OAuth 2.0

To add custom OAuth 2.0 credentials, the auth_config parameter needs to be specified with the OAuth client id and client secret while creating an integration. Integration object stores the credentials.

A connection request is initiated using the specified integration ID which uses the specified auth OAuth credentials.

Once the connection is established, an action can be executed using the connection object.

1from composio_openai import App, ComposioToolSet, Action
2from uuid import uuid4
3
4toolset = ComposioToolSet()
5
6integration = toolset.create_integration(
7 app=App.GITHUB,
8 auth_mode="OAUTH2",
9 auth_config={
10 "client_id": "12345678",
11 "client_secret": "12345678"
12 },
13)
14
15connection_request = toolset.initiate_connection(
16 integration_id=integration.id,
17 entity_id=str(uuid4()),
18 auth_scheme="OAUTH2"
19)
20
21print("Connect to GitHub: ", connection_request.redirectUrl)
22
23connection = connection_request.wait_until_active(toolset.client, 10)
24
25tool_res = toolset.execute_action(
26 action=Action.GITHUB_GET_A_PULL_REQUEST,
27 params={
28 "pull_number": 1,
29 "owner": "composiohq",
30 "repo": "agi"
31 },
32 connected_account_id=connection.id
33)

How to retrieve auth configuration

1from composio_openai import App, ComposioToolSet
2
3toolset = ComposioToolSet()
4
5auth_scheme = toolset.get_auth_scheme_for_app(App.GMAIL)
6
7print(auth_scheme.fields[0].name)
8print(auth_scheme.fields[1].name)

Auth with Bearer Token

Many applications that have OAuth 2.0, also support the user providing their own Bearer token. For these, the auth_config parameter is not required. However, the user is expected to provide the token in the connectionParams parameter.

1from composio_openai import Action, App, ComposioToolSet
2from uuid import uuid4
3
4toolset = ComposioToolSet()
5
6integration = toolset.create_integration(app=App.GMAIL, auth_mode="BEARER_TOKEN")
7
8gmail_params = toolset.get_expected_params_for_user(
9 app=App.GMAIL, auth_scheme="BEARER_TOKEN"
10)
11param_key = gmail_params["expected_params"][0].name
12param_value = input(f"Enter the Gmail {param_key}: ")
13
14gmail_connection = toolset.initiate_connection(
15 integration_id=integration.id,
16 entity_id=str(uuid4()),
17 auth_scheme="BEARER_TOKEN",
18 connected_account_params={param_key: param_value}, # This is the token the user will provide
19)
20
21print(f"Connection Status: {gmail_connection.connectionStatus}")
22
23toolset.execute_action(
24 action=Action.GMAIL_FETCH_EMAILS,
25 params={},
26 connected_account_id=gmail_connection.connectedAccountId,
27)

Auth with API Key

Similar to Bearer Token Authentication, the auth_config parameter is not required. In some token based authentication, the user is expected to provide more information than just the token.

In this case, Shopify requires the user to provide the api_key and shop parameters.

1from composio_openai import App, ComposioToolSet, Action
2
3toolset = ComposioToolSet()
4
5integration = toolset.create_integration(
6 app=App.SHOPIFY, auth_mode="API_KEY", use_composio_oauth_app=False
7)
8
9shopify_params = toolset.get_expected_params_for_user(
10 app=App.SHOPIFY, auth_scheme="API_KEY"
11)
12
13params = {}
14for param in shopify_params["expected_params"]:
15 param_key = param.name
16 param_value = input(f"Enter the Shopify {param.name}: ")
17 params[param_key] = param_value
18
19
20shopify_connection = toolset.initiate_connection(
21 app=App.SHOPIFY,
22 auth_scheme="API_KEY",
23 connected_account_params=params,
24)
25
26print(f"Connection Status: {shopify_connection.connectionStatus}")
27
28toolset.execute_action(
29 action=Action.SHOPIFY_GET_ALL_CUSTOMERS,
30 params={},
31 connected_account_id=shopify_connection.connectedAccountId,
32)