Basic Hono Server
With Composio’s managed authentication and tool calling, it’s easy to build
AI agents that interact with the real world while reducing boilerplate for
setup and authentication management. This cookbook will guide you through
building and serving agents using Composio
, OpenAI
, and Hono.js
.
Prerequisites
- Node.js 18.x or higher
- npm or yarn package manager
- Composio API key
- OpenAI API key
- Basic knowledge of OAuth
- Understanding of building HTTP services (preferably using Hono.js)
Building an AI agent that can interact with gmail
service
First, let’s start with building a simple AI agent embedded with tools from
Composio that lets the agent interact with the gmail
service.
This is a simple agent without state management and agentic loop implementation, so the agent can’t perform complicated tasks. If you want to understand how composio can be used with agentic loops, check other cookbooks with more agentic frameworks.
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
.
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.
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.
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 Hono service
We will use Hono.js
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
Hono allows dependency injection patterns to simplify the usage of SDK clients that must be singletons. We recommend using composio SDK client as singleton.
Check config/composio.ts module for more details.
Invoke agent via Hono
When invoking an agent, make sure you validate the user_id
.
Check src/api.ts 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 Hono service. Now let’s run the service.
Before proceeding, check the code for utility endpoints not discussed in the cookbook
-
Clone the repository
-
Setup environment
Fill the api keys
Install dependencies
-
Run the HTTP server
Testing the API with curl
Assuming the server is running locally on http://localhost:8000
.
Check if a connection exists
Create a connection
Note: The body fields are required by the API schema, but are ignored internally in this example service.
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.
Run the Gmail agent
Requires an active connected account for the default
user.
Fetch emails (direct action)
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 validCOMPOSIO_API_KEY
andOPENAI_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