AI Gmail Labeller
Imagine having an AI assistant that automatically organizes your Gmail inbox by intelligently labeling incoming emails. That’s exactly what we’ll build in this tutorial.
The complete app is available in our filter-gmails-example
repository. The app has a frontend built with Next.js along with Supabase for authentication.
In this guide, we’ll focus on the Composio + OpenAI Agents part of the app.
Prerequisites
- Clone the
filter-gmails-example
repository and set the project up locally. - Get a Composio API key. You can get one here.
Creating Triggers for users
Triggers are a way to listen for events from apps. They act as a notification system for your AI applications, enabling your agents to respond dynamically to external events occurring within your apps.
In some cases, triggers require certain configurations to set the correct events. You can inspect and add these properties while enabling the triggers.
Then the trigger can be created with the following code:
Listening for a trigger event
For local development, you can use ngrok to expose your local server to the internet.
Then you can set the ngrok URL in the trigger configuration.

This is the main webhook endpoint for processing Gmail new message events.
Composio sends a POST request to the webhook endpoint /composio/webhook
that contains the event data.
It is parsed and then the labeling method process_gmail_message
is called through a FastAPI Background task.
Processing the mail using OpenAI Agents SDK
The process_gmail_message
method is responsible for processing the email using the OpenAI Agents SDK.
We read the first 10k characters of the email body to pass it to the agent. This is done to avoid overwhelming or surpassing the token limits in case of really long emails.
user_filter
is the user’s custom prompt from the database. We pass it to the agent along with the email body.
You can view the agent trace in OpenAI’s Traces Dashboard.

Securing the webhooks!
It is important to validate the webhook signature to ensure the request is coming from Composio.
Generate and store the webhook signature in the environment variables.

The webhook signature is validated in the verify_webhook_signature
function.
In the /composio/webhook
endpoint, we verify the webhook signature and then parse the request body. If the signature is invalid, we return a 401 status code.
Inspiration
This example was inspired by an X/Twitter post by @FarzaTV.
We encourage you to check out the full app, fork it, build it, and make it your own! Reach out to sid@composio.dev for free credits ;)