Basic FastAPI Server

Build a simple Gmail agent with Composio and FastAPI

This cookbook will guide you through building agents equipped with tools using Composio, OpenAI, and FastAPI.

Prerequisites

  • Python 3.10+
  • UV
  • Composio API key
  • OpenAI API key

Building an AI agent that can interact with gmail service

First, let’s start with building a simple AI agent with Composio tools that lets the agent interact with gmail.

1from openai import OpenAI
2from composio import Composio
3from composio_openai import OpenAIProvider
4import os
5
6def run_gmail_agent(
7 composio_client: Composio[OpenAIProvider],
8 openai_client: OpenAI,
9 user_id: str, # Composio uses the User ID to store and access user-level authentication tokens.
10 prompt: str,
11):
12 """
13 Run the Gmail agent using composio and openai clients.
14 """
15 # Step 1: Fetch the necessary Gmail tools list with Composio
16 tools = composio_client.tools.get(
17 user_id=user_id,
18 tools=[
19 "GMAIL_FETCH_EMAILS",
20 "GMAIL_SEND_EMAIL",
21 "GMAIL_CREATE_EMAIL_DRAFT"
22 ]
23 )
24
25 # Step 2: Use OpenAI to generate a response based on the prompt and available tools
26 response = openai_client.chat.completions.create(
27 model="gpt-4.1",
28 tools=tools,
29 messages=[{"role": "user", "content": prompt}],
30 )
31
32 # Step 3: Handle tool calls with Composio and return the result
33 result = composio_client.provider.handle_tool_calls(response=response, user_id=user_id)
34 return result

This example demonstrates a basic agent without state management or agentic loops, suitable for simple one-step tasks. For complex multi-step workflows requiring memory and iterative reasoning, see our cookbooks with frameworks like LangChain, CrewAI, or AutoGen.

To invoke this agent, authenticate your users with Composio’s managed authentication service.

Authenticating users

To authenticate your users with Composio you need an authentication config for the given app. In this case you need one for gmail.

To create an authentication config for gmail you need client_id and client_secret from your Google OAuth Console. Once you have the credentials, use the following piece of code to set up authentication for gmail.

1from composio import Composio
2from composio_openai import OpenAIProvider
3
4def create_auth_config(composio_client: Composio[OpenAIProvider]):
5 """
6 Create a auth config for the gmail toolkit.
7 """
8 client_id = os.getenv("GMAIL_CLIENT_ID")
9 client_secret = os.getenv("GMAIL_CLIENT_SECRET")
10 if not client_id or not client_secret:
11 raise ValueError("GMAIL_CLIENT_ID and GMAIL_CLIENT_SECRET must be set")
12
13 return composio_client.auth_configs.create(
14 toolkit="GMAIL",
15 options={
16 "name": "default_gmail_auth_config",
17 "type": "use_custom_auth",
18 "auth_scheme": "OAUTH2",
19 "credentials": {
20 "client_id": client_id,
21 "client_secret": client_secret,
22 },
23 },
24 )

This will create a Gmail authentication config to authenticate your app’s users. Ideally, create one authentication object per project, so check for an existing auth config before creating a new one.

1def fetch_auth_config(composio_client: Composio[OpenAIProvider]):
2 """
3 Fetch the auth config for a given user id.
4 """
5 auth_configs = composio_client.auth_configs.list()
6 for auth_config in auth_configs.items:
7 if auth_config.toolkit == "GMAIL":
8 return auth_config
9
10 return None

Composio platform provides composio managed authentication for some apps to fast-track your development, gmail being one of them. You can use these default auth configs for development, but for production, always use your own oauth app configuration.

Once you have authentication management in place, we can start with connecting your users to your gmail app. Let’s implement a function to connect users to your gmail app via composio.

1from fastapi import FastAPI
2
3# Function to initiate a connected account
4def create_connection(composio_client: Composio[OpenAIProvider], user_id: str):
5 """
6 Create a connection for a given user id and auth config id.
7 """
8 # Fetch or create the auth config for the gmail toolkit
9 auth_config = fetch_auth_config(composio_client=composio_client)
10 if not auth_config:
11 auth_config = create_auth_config(composio_client=composio_client)
12
13 # Create a connection for the user
14 return composio_client.connected_accounts.initiate(
15 user_id=user_id,
16 auth_config_id=auth_config.id,
17 )
18
19# Setup FastAPI
20app = FastAPI()
21
22# Connection initiation endpoint
23@app.post("/connection/create")
24def _create_connection(
25 request: CreateConnectionRequest,
26 composio_client: ComposioClient,
27) -> dict:
28 """
29 Create a connection for a given user id.
30 """
31 # For demonstration, using a default user_id. Replace with real user logic in production.
32 user_id = "default"
33
34 # Create a new connection for the user
35 connection_request = create_connection(composio_client=composio_client, user_id=user_id)
36 return {
37 "connection_id": connection_request.id,
38 "redirect_url": connection_request.redirect_url,
39 }

Now, you can make a request to this endpoint on your client app, and your user will get a URL which they can use to authenticate.

Set Up FastAPI service

We will use FastApi to build an HTTP service that authenticates your users and lets them interact with your agent. This guide will provide best practices for using composio client in production environments.

Setup dependencies

FastAPI allows dependency injection to simplify the usage of SDK clients that must be singletons. We recommend using composio SDK client as singleton.

1import os
2import typing_extensions as te
3
4from composio import Composio
5from composio_openai import OpenAIProvider
6
7from fastapi import Depends
8
9_composio_client: Composio[OpenAIProvider] | None = None
10
11def provide_composio_client():
12 """
13 Provide a Composio client.
14 """
15 global _composio_client
16 if _composio_client is None:
17 _composio_client = Composio(provider=OpenAIProvider())
18 return _composio_client
19
20
21ComposioClient = te.Annotated[Composio, Depends(provide_composio_client)]
22"""
23A Composio client dependency.
24"""

Check dependencies module for more details.

Invoke agent via FastAPI

When invoking an agent, make sure you validate the user_id.

1def check_connected_account_exists(
2 composio_client: Composio[OpenAIProvider],
3 user_id: str,
4):
5 """
6 Check if a connected account exists for a given user id.
7 """
8 # Fetch all connected accounts for the user
9 connected_accounts = composio_client.connected_accounts.list(
10 user_ids=[user_id],
11 toolkit_slugs=["GMAIL"],
12 )
13
14 # Check if there's an active connected account
15 for account in connected_accounts.items:
16 if account.status == "ACTIVE":
17 return True
18
19 # Ideally you should not have inactive accounts, but if you do, delete them.
20 print(f"[warning] inactive account {account.id} found for user id: {user_id}")
21 return False
22
23def validate_user_id(user_id: str, composio_client: ComposioClient):
24 """
25 Validate the user id, if no connected account is found, create a new connection.
26 """
27 if check_connected_account_exists(composio_client=composio_client, user_id=user_id):
28 return user_id
29
30 raise HTTPException(
31 status_code=404, detail={"error": "No connected account found for the user id"}
32 )
33
34# Endpoint: Run the Gmail agent for a given user id and prompt
35@app.post("/agent")
36def _run_gmail_agent(
37 request: RunGmailAgentRequest,
38 composio_client: ComposioClient,
39 openai_client: OpenAIClient, # OpenAI client will be injected as dependency
40) -> List[ToolExecutionResponse]:
41 """
42 Run the Gmail agent for a given user id and prompt.
43 """
44 # For demonstration, using a default user_id. Replace with real user logic in production.
45 user_id = "default"
46
47 # Validate the user id before proceeding
48 user_id = validate_user_id(user_id=user_id, composio_client=composio_client)
49
50 # Run the Gmail agent using Composio and OpenAI
51 result = run_gmail_agent(
52 composio_client=composio_client,
53 openai_client=openai_client,
54 user_id=user_id,
55 prompt=request.prompt,
56 )
57 return result

Check server module for service implementation

Putting everything together

So far, we have created an agent with ability to interact with gmail using the composio SDK, functions to manage connected accounts for users and a FastAPI service. Now let’s run the service.

Before proceeding, check the code for utility endpoints not discussed in the cookbook

  1. Clone the repository

    $git clone git@github.com:composiohq/composio-fastapi
    >cd composio-fastapi/
  2. Setup environment

    $cp .env.example .env

    Fill the api keys

    1COMPOSIO_API_KEY=
    2OPENAI_API_KEY=

    Create the virtual env

    $make env
    >source .venv/bin/activate
  3. Run the HTTP server

    $uvicorn simple_gmail_agent.server.api:create_app --factory

Testing the API with curl

Assuming the server is running locally on http://localhost:8000.

Check if a connection exists

$curl -X POST http://localhost:8000/connection/exists

Create a connection

Note: The body fields are required by the API schema, but are ignored internally in this example service.

$curl -X POST http://localhost:8000/connection/create \
> -H "Content-Type: application/json" \
> -d '{
> "user_id": "default",
> "auth_config_id": "AUTH_CONFIG_ID_FOR_GMAIL_FROM_THE_COMPOSIO_DASHBOARD"
> }'

Response includes connection_id and redirect_url. Complete the OAuth flow at the redirect_url.

Check connection status

Use the connection_id returned from the create step.

$curl -X POST http://localhost:8000/connection/status \
> -H "Content-Type: application/json" \
> -d '{
> "user_id": "default",
> "connection_id": "CONNECTION_ID_FROM_CREATE_RESPONSE"
> }'

Run the Gmail agent

Requires an active connected account for the default user.

$curl -X POST http://localhost:8000/agent \
> -H "Content-Type: application/json" \
> -d '{
> "user_id": "default",
> "prompt": "Summarize my latest unread emails from the last 24 hours."
> }'

Fetch emails (direct action)

$curl -X POST http://localhost:8000/actions/fetch_emails \
> -H "Content-Type: application/json" \
> -d '{
> "user_id": "default",
> "limit": 5
> }'

These examples are intended solely for testing purposes.

Using Composio for managed auth and tools

Composio reduces boilerplate for building AI agents that access and use various apps. In this cookbook, to build Gmail integration without Composio, you would have to write code to

  • manage Gmail OAuth app
  • manage user connections
  • tools for your agents to interact with Gmail

Using Composio simplifies all of the above to a few lines of code as shown in the cookbook.

Best practices

🎯 Effective Prompts:

  • Be specific: “Send email to john@company.com about tomorrow’s 2pm meeting” works better than “send email”
  • Include context: “Reply to Sarah’s email about the budget with our approval”
  • Use natural language: The agent understands conversational requests

🔒 User Management:

  • Use unique, consistent user_id values for each person
  • Each user maintains their own Gmail connection
  • User IDs can be email addresses, usernames, or any unique identifier

Troubleshooting

Connection Issues:

  • Ensure your .env file has valid COMPOSIO_API_KEY and OPENAI_API_KEY
  • Check if the user has completed Gmail authorization.
  • Verify the user_id matches exactly between requests

API Errors:

  • Check the server logs for detailed error messages
  • Ensure request payloads match the expected format
  • Visit /docs endpoint for API schema validation

Gmail API Limits:

  • Gmail has rate limits; the agent will handle these gracefully
  • For high-volume usage, consider implementing request queuing