Auth Concepts

Understand the core concepts behind Composio Auth

Composio Auth simplifies integrating user-authorized actions into your app. It securely manages OAuth flows, handles token storage, and ensures actions are executed with the correct user’s credentials.

Composio Auth relies on three core concepts. Integration, Connection, Entities.

Composio Auth Concepts showing the relationship between Integration, Connection, and Entity

Getting Started

Let’s use Composio Auth to use the X (formerly Twitter) API to read a tweet.

1

Import libraries

First, initialize and import the Composio and OpenAI SDKs.

1from composio_openai import ComposioToolSet, App, Action
2
3toolset = ComposioToolSet()
2

Create an entity

Create or get an existing entity for a user.

1username = input("Enter your X username: ").strip()
2entity = toolset.get_entity(username)
3

Initiate connection to the X API

Here, we’ll initiate a connection to the X API. This will redirect you to the X OAuth login page where you can login and grant permissions.

1conn_req = entity.initiate_connection(app_name=App.TWITTER)
2print(f"Navigate to the following URL to connect your X account: {conn_req.redirectUrl}")
4

Wait for connection to be active

Here, we wait for the connection process to finalize and become active.

1connection = conn_req.wait_until_active(toolset.client, timeout=20)
2print(f"Connection created: {connection.id}")
5

Read the tweet

Here, we’ll read the tweet from the X API. We specify the connection ID to use the correct user’s credentials.

1post_res = toolset.execute_action(
2 Action.TWITTER_POST_LOOKUP_BY_POST_ID,
3 params={"id": "1886192184808149383"},
4 connected_account_id=connection.id,
5)
6print(post_res["data"]["text"])
1from composio_openai import Action, App, ComposioToolSet
2from openai import OpenAI
3
4client = OpenAI()
5toolset = ComposioToolSet()
6
7username = input("Enter your X username: ").strip()
8entity = toolset.get_entity(username)
9
10conn_req = entity.initiate_connection(app_name=App.TWITTER)
11print(
12 f"Navigate to the following URL to connect your X account: {conn_req.redirectUrl}"
13)
14
15connection = conn_req.wait_until_active(toolset.client, timeout=20)
16print(f"Connection created: {connection.id}")
17
18post_res = toolset.execute_action(
19 Action.TWITTER_POST_LOOKUP_BY_POST_ID,
20 params={"id": "1886192184808149383"},
21 connected_account_id=connection.id,
22)
23
24print(post_res["data"]["text"])