Sample Script

This tutorial will show you the basics of how to use the Composio SDK to call tools.

SAMPLE

$pip install composio

Here’s a complete Python example that demonstrates the Composio SDK:

Setting up Composio and OpenAI
1from composio_core import Composio
2from openai import OpenAI
3
4# Initialize OpenAI client
5openai = OpenAI(
6 api_key="your-openai-api-key"
7)
8
9# Initialize Composio client
10composio = Composio(
11 api_key="your-composio-api-key"
12)
13
14# Set up user identifier
15userId = "your@email.com"
16
17# Authorize and get tools for Gmail
18connection, tools = composio.toolkits.authorize(userId, "gmail")
19print(f"🔗 Visit the URL to authorize:\n👉 {connection.redirect_url}")
20
21# Wait for the user to complete authorization
22connection.wait_for_connection()
23
24# Create a chat completion with tool calling
25completion = openai.chat.completions.create(
26 model="gpt-4o",
27 messages=[
28 {
29 "role": "system",
30 "content": "You are a helpful assistant that can send emails.",
31 },
32 {
33 "role": "user",
34 "content": "send an email to sid@composio.dev saying 'hi from the composio quickstart'",
35 # we'll ship you free merch if you do ;)
36 },
37 ],
38 tools=tools,
39 tool_choice="auto",
40)
41
42# Handle the tool call and execute
43result = composio.provider.handle_tool_call(userId, completion)
44print("✅ Email sent successfully!")
45print(result)