Custom Authentication Guide

Learn how to use various authentication methods to connect to your application.

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, you can specify your own client id and client secret while creating an integration.

1

Initiate the Connection

The first step is to initiate a connection request using your integration ID. This creates a connection request with the OAuth 2.0 credentials you configured.

1from composio_openai import App, ComposioToolSet, Action
2from uuid import uuid4
3import os
4
5toolset = ComposioToolSet()
6
7# Get the integration ID from environment variables
8integration_id = os.environ["GITHUB_INTEGRATION_ID"]
9
10# Initialize the connection request
11connection_request = toolset.initiate_connection(
12 integration_id=integration_id,
13 entity_id=str(uuid4()),
14 auth_scheme="OAUTH2"
15)
2

Display the Redirect URL and Wait for Activation

After initiating the connection, you’ll receive a redirect URL. This URL should be presented to the user, who needs to complete the OAuth flow by authorizing the application. Once authorized, you can wait for the connection to become active.

1# Display the redirect URL for the user to complete OAuth authentication
2print("Connect to GitHub: ", connection_request.redirectUrl)
3
4# Wait for the connection to become active (timeout after 10 minutes)
5connection = connection_request.wait_until_active(toolset.client, 10)
3

Checking the Connection Status

The status can be checked by:

1print(f"Connection Status: {connection.connectionStatus}")

Auth with API keys

Many applications have API key or token based authentication, in these cases, your users will need to provide the API key, token or other parameters to authenticate and use the service.

1

Retrieving Parameters Users Need to Provide

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

1from composio_openai import App, ComposioToolSet
2
3toolset = ComposioToolSet()
4
5shopify_params = toolset.get_expected_params_for_user(app=App.SHOPIFY, auth_scheme="API_KEY")
6print(shopify_params["expected_params"])
7
8# [ "api_key" , "shop"]
2

Initiating the Connection

Once the user has been prompted for the required parameters, you can initiate a connection with the connected_account_params parameter.

1shopify_connection = toolset.initiate_connection(
2 app=App.SHOPIFY,
3 auth_scheme="API_KEY",
4 connected_account_params={
5 "api_key": "secret_1234567890", # This is the api key the user will provide
6 "shop": "test-shop.myshopify.com", # This is the shop the user will provide
7 },
8)
3

Checking the Connection Status

In this case, there is no redirect URL, so the connection will be activated immediately.

The status can be checked by:

1print(f"Connection Status: {shopify_connection.connectionStatus}")

Auth with Bearer Token

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

1

Retrieving Parameters Users Need to Provide

Gmail also supports authentication via a Bearer token. If configured to use the Bearer token, the user will need to provide the token in the connectionParams parameter.

1from composio_openai import Action, App, ComposioToolSet
2
3toolset = ComposioToolSet()
4
5gmail_params = toolset.get_expected_params_for_user(
6 app=App.GMAIL, auth_scheme="BEARER_TOKEN"
7)
8print(gmail_params["expected_params"])
9# [ "token" ]
2

Initiating the Connection

Once the user has been prompted for the required parameters, you can initiate a connection with the connected_account_params parameter.

1gmail_connection = toolset.initiate_connection(
2 integration_id=integration.id,
3 entity_id=str(uuid4()),
4 auth_scheme="BEARER_TOKEN",
5 connected_account_params={"token": "secret_1234567890"}, # This is the token the user will provide
6)
3

Checking the Connection Status

The status can be checked by:

1print(f"Connection Status: {gmail_connection.connectionStatus}")