Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

basic webserver example

Rahul Tarak4b5211872824
diff --git a/fern/pages/examples/src/sample-script/sample-script.mdx b/fern/pages/examples/src/sample-script/sample-script.mdx
index 047d591e6..5966aebb6 100644
--- a/fern/pages/examples/src/sample-script/sample-script.mdx
+++ b/fern/pages/examples/src/sample-script/sample-script.mdx
@@ -9,6 +9,8 @@ hide-nav-links: true
This tutorial will show you the basics of how to use the Composio SDK to call tools.
+SAMPLE
+
```bash
pip install composio
```
@@ -18,9 +20,8 @@ Here's a complete Python example that demonstrates the Composio SDK:
<ExampleCode
src="fern/pages/examples/src/sample-script/sample-script.py"
startLine={1}
- endLine={13}
title="Setting up Composio and OpenAI"
- highlightStart={5}
- highlightEnd={12}
+ highlightStart={17}
+ highlightEnd={19}
/>
diff --git a/fern/pages/examples/src/sample-webserver/index.ts b/fern/pages/examples/src/sample-webserver/index.ts
new file mode 100644
index 000000000..cdd0fe427
--- /dev/null
+++ b/fern/pages/examples/src/sample-webserver/index.ts
@@ -0,0 +1,61 @@
+import { Hono } from 'hono';
+import { Composio } from '@composio/core';
+import OpenAI from 'openai';
+
+const app = new Hono();
+
+// reads COMPOSIO_API_KEY from the environment variables
+// uses OpenAI by default
+const composio = new Composio();
+
+const openai = new OpenAI();
+
+// this should be made once and stored in a database
+// const authConfigId = (await composio.authConfigs.create('GMAIL')).id;
+const authConfigId = 'ac_DUMMY';
+
+app.post('/create-connection', async c => {
+ const { userId } = await c.req.json();
+
+ const connection = await composio.connectedAccounts.initiate(userId, authConfigId);
+
+ return c.json({
+ id: connection.id,
+ // for the user to authorize the connection
+ redirectUrl: connection.redirectUrl,
+ });
+});
+
+app.post('/send-email', async c => {
+ const { userId, feedback, overwrite_email } = await c.req.json();
+
+ const tools = await composio.tools.get(userId, {
+ toolkits: ['GMAIL'],
+ });
+
+ const completion = await openai.chat.completions.create({
+ model: 'gpt-4o',
+ messages: [
+ {
+ role: 'system',
+ content:
+ 'Send the composio team feedback about their product at [email protected], parse the user feedback and send an email from their account, if they provide an email address, use that, otherwise sent it to [email protected]',
+ },
+ {
+ role: 'user',
+ content: `
+ User feedback: ${feedback}
+ Overwrite email: ${overwrite_email}
+ `,
+ },
+ ],
+ tools,
+ tool_choice: 'auto',
+ });
+
+ const result = await composio.provider.handleToolCalls(userId, completion);
+
+ return c.json({ completion, result });
+});
+
+export default app;
diff --git a/fern/pages/examples/src/sample-webserver/sample-webserver.mdx b/fern/pages/examples/src/sample-webserver/sample-webserver.mdx
index e69de29bb..ce7df1fcf 100644
--- a/fern/pages/examples/src/sample-webserver/sample-webserver.mdx
+++ b/fern/pages/examples/src/sample-webserver/sample-webserver.mdx
@@ -0,0 +1,126 @@
+---
+title: A basic web server
+image: "" # image for socials
+description: "A simple example of a web server that uses the Composio SDK to call tools."
+keywords: "web server, composio, sdk, tool calling"
+subtitle: ""
+hide-nav-links: true
+---
+
+This tutorial shows you a minimal example of a web server that can use composio
+
+
+### Install dependencies
+
+using `bun` for this example, it is super convenient for rapid prototyping.
+
+```bash
+bun install @composio/core@next openai hono
+```
+
+Please set the following environment variables:
+
+```bash
+# .env
+COMPOSIO_API_KEY=your_api_key
+OPENAI_API_KEY=your_api_key
+```
+
+The full source code is available [here](https://github.com/ComposioHQ/composio/blob/next/fern/pages/examples/src/sample-webserver/index.ts).
+
+In this tutorial, we will create an auth config, initiate a connection, and send an email.
+
+### Setup
+
+Basic scaffolding for the server.
+
+<ExampleCode
+ src="fern/pages/examples/src/sample-webserver/index.ts"
+ startLine={1}
+ endLine={10}
+ title="Setup"
+ highlightStart={9}
+ highlightEnd={9}
+/>
+
+
+### Auth Configs
+
+These are configurations for tools that control how they are authorized, the scopes and permissions they have. You can learn more about them [here](/authenticating-tools).
+
+For this example, we will create a basic GMAIL auth config.
+
+You are recommend to store these config ids, so that you can reuse them later.
+
+<ExampleCode
+ src="fern/pages/examples/src/sample-webserver/index.ts"
+ startLine={12}
+ endLine={16}
+ title="Auth Configs"
+/>
+
+
+### Initiating a connection
+
+Connections are your users authorizing your tools.
+
+Here we start the connection process, once your user uses the `redirectUrl` from the response,
+they will be redirected to the auth flow.
+Once they have authorized the connection, you can use the `id` to get and execute the tools as the user.
+
+<ExampleCode
+ src="fern/pages/examples/src/sample-webserver/index.ts"
+ startLine={16}
+ endLine={27}
+ title="Initiating a connection"
+ highlightStart={19}
+ highlightEnd={19}
+/>
+
+You can test your server by running `bun run index.ts` and then running the following command:
+
+```bash
+curl -X POST http://localhost:3000/create-connection \
+-H "Content-Type: application/json" \
+-d '{"userId": "123"}'
+```
+
+### Sending an email
+
+Now let's send that email.
+
+<ExampleCode
+ src="fern/pages/examples/src/sample-webserver/index.ts"
+ startLine={29}
+ endLine={59}
+ title="Sending an email"
+ highlightStart={56}
+ highlightEnd={56}
+/>
+
+Using the `id` from the connection, we can now send an email.
+
+```bash
+curl -X POST http://localhost:3000/send-email \
+-H "Content-Type: application/json" \
+-d '{"userId": "123", "feedback": "These docs are great!"}'
+```
+
+### What just happened?
+
+Well first - we created an instance of our `gmail` tool with our default auth config, then our user authorized that tool with their account and some permissions.
+
+Then we used that `gmail` tool by giving it to OpenAI's `gpt-4o`. The model composed the email and sent it correctly formatted to the composio team.
+
+### What's next?
+
+This is a fairly basic example, but you can do a lot more with our sdk.
+
+Recommended further reading:
+
+- [Authenticating tools](/authenticating-tools) - understand how to control tool authorization
+- [Executing Tools](/executing-tools) - understand how tool execution works
+- [Providers](/providers) - want to use something other than OpenAI? we probably support it
+- [Tools](/tools) - understand what tools we have
+
+