Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 23, 01:45 UTC
basic webserver example
Rahul Tarak committed
Jun 9, 2025 at 19:13 UTC
3bc975ecaaab736d9d712d3a56b9c712608700b0
3 files changed
+191
-3
fern/pages/examples/src/sample-script/sample-script.mdx
+4
-3
@@ -9,6 +9,8 @@ hide-nav-links: true
9
10
This tutorial will show you the basics of how to use the Composio SDK to call tools.
11
12
+SAMPLE
13
+
14
```bash
15
pip install composio
16
```
@@ -18,9 +20,8 @@ Here's a complete Python example that demonstrates the Composio SDK:
20
<ExampleCode
21
src="fern/pages/examples/src/sample-script/sample-script.py"
22
startLine={1}
21
- endLine={13}
23
title="Setting up Composio and OpenAI"
23
- highlightStart={5}
24
- highlightEnd={12}
24
+ highlightStart={17}
25
+ highlightEnd={19}
26
/>
27
fern/pages/examples/src/sample-webserver/index.ts
new
+61
@@ -0,0 +1,61 @@
1
+import { Hono } from 'hono';
2
+import { Composio } from '@composio/core';
3
+import OpenAI from 'openai';
4
+
5
+const app = new Hono();
6
+
7
+// reads COMPOSIO_API_KEY from the environment variables
8
+// uses OpenAI by default
9
+const composio = new Composio();
10
+
11
+const openai = new OpenAI();
12
+
13
+// this should be made once and stored in a database
14
+// const authConfigId = (await composio.authConfigs.create('GMAIL')).id;
15
+const authConfigId = 'ac_DUMMY';
16
+
17
+app.post('/create-connection', async c => {
18
+ const { userId } = await c.req.json();
19
+
20
+ const connection = await composio.connectedAccounts.initiate(userId, authConfigId);
21
+
22
+ return c.json({
23
+ id: connection.id,
24
+ // for the user to authorize the connection
25
+ redirectUrl: connection.redirectUrl,
26
+ });
27
+});
28
+
29
+app.post('/send-email', async c => {
30
+ const { userId, feedback, overwrite_email } = await c.req.json();
31
+
32
+ const tools = await composio.tools.get(userId, {
33
+ toolkits: ['GMAIL'],
34
+ });
35
+
36
+ const completion = await openai.chat.completions.create({
37
+ model: 'gpt-4o',
38
+ messages: [
39
+ {
40
+ role: 'system',
41
+ content:
42
+ 'Send the composio team feedback about their product at feedback@composio.dev, parse the user feedback and send an email from their account, if they provide an email address, use that, otherwise sent it to feedback@composio.dev',
43
+ },
44
+ {
45
+ role: 'user',
46
+ content: `
47
+ User feedback: ${feedback}
48
+ Overwrite email: ${overwrite_email}
49
+ `,
50
+ },
51
+ ],
52
+ tools,
53
+ tool_choice: 'auto',
54
+ });
55
+
56
+ const result = await composio.provider.handleToolCalls(userId, completion);
57
+
58
+ return c.json({ completion, result });
59
+});
60
+
61
+export default app;
fern/pages/examples/src/sample-webserver/sample-webserver.mdx
+126
@@ -0,0 +1,126 @@
1
+---
2
+title: A basic web server
3
+image: "" # image for socials
4
+description: "A simple example of a web server that uses the Composio SDK to call tools."
5
+keywords: "web server, composio, sdk, tool calling"
6
+subtitle: ""
7
+hide-nav-links: true
8
+---
9
+
10
+This tutorial shows you a minimal example of a web server that can use composio
11
+
12
+
13
+### Install dependencies
14
+
15
+using `bun` for this example, it is super convenient for rapid prototyping.
16
+
17
+```bash
18
+bun install @composio/core@next openai hono
19
+```
20
+
21
+Please set the following environment variables:
22
+
23
+```bash
24
+# .env
25
+COMPOSIO_API_KEY=your_api_key
26
+OPENAI_API_KEY=your_api_key
27
+```
28
+
29
+The full source code is available [here](https://github.com/ComposioHQ/composio/blob/next/fern/pages/examples/src/sample-webserver/index.ts).
30
+
31
+In this tutorial, we will create an auth config, initiate a connection, and send an email.
32
+
33
+### Setup
34
+
35
+Basic scaffolding for the server.
36
+
37
+<ExampleCode
38
+ src="fern/pages/examples/src/sample-webserver/index.ts"
39
+ startLine={1}
40
+ endLine={10}
41
+ title="Setup"
42
+ highlightStart={9}
43
+ highlightEnd={9}
44
+/>
45
+
46
+
47
+### Auth Configs
48
+
49
+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).
50
+
51
+For this example, we will create a basic GMAIL auth config.
52
+
53
+You are recommend to store these config ids, so that you can reuse them later.
54
+
55
+<ExampleCode
56
+ src="fern/pages/examples/src/sample-webserver/index.ts"
57
+ startLine={12}
58
+ endLine={16}
59
+ title="Auth Configs"
60
+/>
61
+
62
+
63
+### Initiating a connection
64
+
65
+Connections are your users authorizing your tools.
66
+
67
+Here we start the connection process, once your user uses the `redirectUrl` from the response,
68
+they will be redirected to the auth flow.
69
+Once they have authorized the connection, you can use the `id` to get and execute the tools as the user.
70
+
71
+<ExampleCode
72
+ src="fern/pages/examples/src/sample-webserver/index.ts"
73
+ startLine={16}
74
+ endLine={27}
75
+ title="Initiating a connection"
76
+ highlightStart={19}
77
+ highlightEnd={19}
78
+/>
79
+
80
+You can test your server by running `bun run index.ts` and then running the following command:
81
+
82
+```bash
83
+curl -X POST http://localhost:3000/create-connection \
84
+-H "Content-Type: application/json" \
85
+-d '{"userId": "123"}'
86
+```
87
+
88
+### Sending an email
89
+
90
+Now let's send that email.
91
+
92
+<ExampleCode
93
+ src="fern/pages/examples/src/sample-webserver/index.ts"
94
+ startLine={29}
95
+ endLine={59}
96
+ title="Sending an email"
97
+ highlightStart={56}
98
+ highlightEnd={56}
99
+/>
100
+
101
+Using the `id` from the connection, we can now send an email.
102
+
103
+```bash
104
+curl -X POST http://localhost:3000/send-email \
105
+-H "Content-Type: application/json" \
106
+-d '{"userId": "123", "feedback": "These docs are great!"}'
107
+```
108
+
109
+### What just happened?
110
+
111
+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.
112
+
113
+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.
114
+
115
+### What's next?
116
+
117
+This is a fairly basic example, but you can do a lot more with our sdk.
118
+
119
+Recommended further reading:
120
+
121
+- [Authenticating tools](/authenticating-tools) - understand how to control tool authorization
122
+- [Executing Tools](/executing-tools) - understand how tool execution works
123
+- [Providers](/providers) - want to use something other than OpenAI? we probably support it
124
+- [Tools](/tools) - understand what tools we have
125
+
126
+