How to beginner · 3 min read

How to authenticate Composio tools

Quick answer
Authenticate Composio tools by initializing ComposioToolSet with your Composio API key from os.environ. Use this toolset with an OpenAI client authenticated via your OpenAI API key to enable tool usage in chat completions.

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 API keys.

  • Install packages: openai, composio-core, and composio-openai.
  • Set environment variables OPENAI_API_KEY and COMPOSIO_API_KEY with your respective API keys.
bash
pip install openai>=1.0 composio-core composio-openai

# In your shell or environment setup
export OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
export COMPOSIO_API_KEY=os.environ["COMPOSIO_API_KEY"]
output
Collecting openai
Collecting composio-core
Collecting composio-openai
Successfully installed openai composio-core composio-openai-<version>

Step by step

Use the ComposioToolSet class to authenticate Composio tools with your Composio API key. Then create an OpenAI client with your OpenAI API key. Pass the tools to the tools parameter in chat.completions.create to enable tool usage.

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

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

# Get tools you want to use, e.g., GitHub star repository 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 in a chat completion request
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
result = toolset.handle_tool_calls(response)
print("Tool call result:", result)
output
Response: I have initiated the process to star the openai/openai-python repository.
Tool call result: {'success': True, 'message': 'Repository starred successfully.'}

Common variations

You can authenticate Composio tools asynchronously or use different OpenAI models by changing the model parameter. The ComposioToolSet supports multiple actions; specify them in the actions list.

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

async def main():
    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)
    result = toolset.handle_tool_calls(response)
    print("Async tool call result:", result)

asyncio.run(main())
output
Async response: I have started starring the openai/openai-python repository.
Async tool call result: {'success': True, 'message': 'Repository starred successfully.'}

Troubleshooting

  • If you get authentication errors, verify your COMPOSIO_API_KEY and OPENAI_API_KEY environment variables are set correctly.
  • If tools are not recognized, ensure you installed composio-core and composio-openai packages.
  • For unexpected API errors, check your network connectivity and API usage limits.

Key Takeaways

  • Always initialize ComposioToolSet with your Composio API key from environment variables.
  • Pass the tools from ComposioToolSet.get_tools() to the tools parameter in OpenAI.chat.completions.create.
  • Use toolset.handle_tool_calls() to process tool call responses after completion.
  • Set both OPENAI_API_KEY and COMPOSIO_API_KEY in your environment before running code.
  • For async usage, await the OpenAI client calls and run inside an async event loop.
Verified 2026-04 · gpt-4o-mini, gpt-4o
Verify ↗