How to beginner · 3 min read

Composio user authentication flow

Quick answer
To authenticate a user with Composio, initialize the ComposioToolSet with your API key, then retrieve tools for your user session. Use the tools parameter in your OpenAI client chat calls to enable authenticated tool access. This flow securely links your user identity to Composio tools for AI interactions.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • Composio API key
  • pip install openai>=1.0 composio-core composio-openai

Setup

Install the required packages and set environment variables for your OPENAI_API_KEY and COMPOSIO_API_KEY. The composio-core and composio-openai packages provide the SDK and toolset integration.

bash
pip install openai composio-core composio-openai
output
Collecting openai
Collecting composio-core
Collecting composio-openai
Successfully installed openai-1.x composio-core-1.x composio-openai-1.x

Step by step

This example shows how to authenticate a user by initializing the ComposioToolSet with your API key, fetching tools, and passing them to the OpenAI client chat call to enable authenticated tool usage.

python
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action

# Initialize Composio toolset with your Composio API key
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])

# Get tools for the user session (e.g., GitHub star action)
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])

# Initialize OpenAI client with your OpenAI API key
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Use the tools parameter to authenticate and enable tool calls
response = client.chat.completions.create(
    model="gpt-4o-mini",
    tools=tools,
    messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
)

print("Response:", response.choices[0].message.content)

# Handle tool calls if any
if response.choices[0].finish_reason == "tool_calls":
    tool_call = response.choices[0].message.tool_calls[0]
    print("Tool call function:", tool_call.function.name)
    print("Arguments:", tool_call.function.arguments)

# Process toolset responses if needed
result = toolset.handle_tool_calls(response)
print("Toolset handled result:", result)
output
Response: Thanks for your request! I've starred the openai/openai-python repository for you.
Tool call function: github_star_repository
Arguments: {"repository": "openai/openai-python"}
Toolset handled result: {'status': 'success', 'details': 'Repository starred'}

Common variations

You can use different Action enums to get other tool capabilities. For async usage, wrap calls with asyncio. You can also use different OpenAI models like gpt-4o or gpt-4o-mini depending on your needs.

python
import asyncio
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action

async def async_auth_flow():
    toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
    tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

    response = await client.chat.completions.create(
        model="gpt-4o",
        tools=tools,
        messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
    )

    print("Async response:", response.choices[0].message.content)

asyncio.run(async_auth_flow())
output
Async response: I've successfully starred the openai/openai-python repository for you.

Troubleshooting

  • If you see authentication errors, verify your COMPOSIO_API_KEY and OPENAI_API_KEY environment variables are set correctly.
  • If tools are not recognized, ensure you pass the tools parameter in the chat completion call.
  • For unexpected failures, check network connectivity and API quota limits.

Key Takeaways

  • Initialize ComposioToolSet with your Composio API key to authenticate users.
  • Pass the retrieved tools to the OpenAI client chat call to enable tool usage.
  • Handle tool_calls in the response to process user-invoked actions securely.
Verified 2026-04 · gpt-4o-mini, gpt-4o
Verify ↗