How to use Connections?

Learn how to use connections

Get all connected accounts for an entity

1from composio import ComposioToolSet, App
2toolset = ComposioToolSet()
3
4# Filter based on entity id
5entity = toolset.get_entity(id="Jessica")
6try:
7 # Lists all connections of the entity
8 connected_accounts = entity.get_connections()
9 print(connected_accounts)
10except NoItemsFound as e:
11 print("No connected account found")

Check if an entity has a connected account for a particular app

1from composio import ComposioToolSet, App
2from composio.client.exceptions import NoItemsFound
3
4toolset = ComposioToolSet()
5
6# Filter based on entity id
7entity = toolset.get_entity(id="Jessica")
8
9try:
10 # Filters based on app name
11 connection_details = entity.get_connection(app=App.GITHUB)
12 # Filters based on connected account id for an entity
13 connection_details = entity.get_connection(connected_account_id="<connected_account_id>")
14 print(connection_details)
15except NoItemsFound as e:
16 print("No connected account found")

Fetch the Connection Parameters

You can use connection parameters locally to build custom actions. The following parameters are typically available:

  • status: Current state of the connection (initiated, active, or failed)
  • refresh_token: Token used to obtain new access tokens
  • expires_in: Token expiration time in seconds
  • token_type: Type of token (usually “Bearer”)
  • base_url: Base URL for API requests
  • Additional parameters specific to the connected service
1connected_account = toolset.get_connected_account(connection_request.connectedAccountId)
2
3# Get the parameters for your local usage
4print(toolset.get_auth_params(connection_id=connected_account.id))
5
6#print(connected_account.connectionParams) # use this for raw/advanced cases

Example of how connection params would look like

1{
2 "base_url": "", // This is the base URL for the API Ex. https://api.linear.app
3 "params": [{ // This is the list of all the params
4 "name": "x-api-key",
5 "in": "header", // `in` value could be of type `header`, `query`
6 "value": "<api-key>"
7 }],
8 "body": {}
9}

You can fetch connection details after user is redirected back to your app (If redirect was needed) and connectionStatus & connectedAccountId will be available in the query params.