Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Add example usage of user_id in the docs
haxzie committed
May 23, 2025 at 11:45 UTC
cb46a71b1e545e12818efd4fd3026730d917e284
3 files changed
+155
-7
README.md
+26
@@ -248,6 +248,9 @@ composio/
248
# Create a new provider
249
pnpm create:provider <provider-name> [--agentic]
250
251
+ # Create a new example
252
+ pnpm create:example <example-name>
253
+
254
# Check peer dependencies
255
pnpm check:peer-deps
256
@@ -255,6 +258,29 @@ composio/
258
pnpm update:peer-deps
259
```
260
261
+## Creating a New Example
262
+
263
+1. Use the create:example script:
264
+
265
+ ```bash
266
+ pnpm create:example my-example
267
+ ```
268
+
269
+2. The script will create a new example in `examples/my-example` with:
270
+
271
+ - `package.json` with minimal dependencies (`@composio/core` and `dotenv`)
272
+ - `tsconfig.json` for TypeScript configuration
273
+ - `.env.example` and `.env` files for environment variables
274
+ - `src/index.ts` with basic Composio SDK setup
275
+ - `README.md` with setup and usage instructions
276
+ - Dependencies automatically installed
277
+
278
+3. Next steps after creation:
279
+ - Edit `.env` and add your `COMPOSIO_API_KEY`
280
+ - Customize `src/index.ts` with your example logic
281
+ - Add any additional dependencies as needed
282
+ - Run with `pnpm start` or `pnpm dev` (with file watching)
283
+
284
## Creating a New Provider
285
286
1. Use the create:provider script:
docs/core-concepts.md
+128
-6
@@ -2,6 +2,128 @@
2
3
Composio SDK is built around a set of key concepts that work together to provide a seamless integration experience for third-party services and tools.
4
5
+## User IDs
6
+
7
+User IDs are the **core foundation** of accessing and executing tools in Composio. Every tool execution, connection authorization, and account management operation requires a `userId` parameter that identifies which user's context the operation should be performed in.
8
+
9
+### ⚠️ Critical Security Considerations
10
+
11
+User IDs are crucial for security and data isolation. You must be extremely careful when handling user IDs to ensure:
12
+
13
+- Users can only access their own connected accounts
14
+- Tool executions are performed in the correct user context
15
+- No unauthorized access to other users' data
16
+
17
+### The `default` User ID
18
+
19
+The `default` userId refers to the default account of your Composio project and should **only be used for**:
20
+
21
+- **Testing and development** environments
22
+- **Single-user applications** where external users don't connect their own accounts
23
+- **Internal tools** where all operations are performed by your system
24
+
25
+```typescript
26
+// ❌ Don't use 'default' in production for multi-user apps
27
+const tools = await composio.tools.get('default', {
28
+ toolkits: ['github'],
29
+});
30
+
31
+// ❌ This could expose other users' data
32
+const result = await composio.tools.execute('GITHUB_GET_REPO', {
33
+ userId: 'default',
34
+ arguments: { owner: 'example', repo: 'repo' },
35
+});
36
+```
37
+
38
+### Production User IDs
39
+
40
+In production applications with multiple users, always use unique identifiers for each user. The best practices are:
41
+
42
+#### ✅ **Recommended: Database UUID**
43
+
44
+```typescript
45
+// Use your database's user ID (UUID, primary key, etc.)
46
+const userId = user.id; // e.g., "550e8400-e29b-41d4-a716-446655440000"
47
+
48
+const tools = await composio.tools.get(userId, {
49
+ toolkits: ['github'],
50
+});
51
+
52
+const result = await composio.tools.execute('GITHUB_GET_REPO', {
53
+ userId: userId,
54
+ arguments: { owner: 'example', repo: 'repo' },
55
+});
56
+```
57
+
58
+#### ✅ **Acceptable: Unique Username/Identifier**
59
+
60
+```typescript
61
+// Use a unique, stable identifier from your system
62
+const userId = user.username; // e.g., "john_doe_123"
63
+// or
64
+const userId = user.externalId; // e.g., "auth0|507f1f77bcf86cd799439011"
65
+```
66
+
67
+#### ⚠️ **Not Recommended: Email Address**
68
+
69
+```typescript
70
+// While functional, emails can change and may cause issues
71
+const userId = user.email; // e.g., "user@example.com"
72
+```
73
+
74
+### Example: Multi-User Application Flow
75
+
76
+```typescript
77
+import { Composio } from '@composio/core';
78
+
79
+const composio = new Composio({
80
+ apiKey: process.env.COMPOSIO_API_KEY,
81
+});
82
+
83
+// 1. User initiates GitHub connection
84
+async function connectUserToGitHub(userId: string) {
85
+ const connectionRequest = await composio.toolkits.authorize(userId, 'github');
86
+ return connectionRequest.redirectUrl;
87
+}
88
+
89
+// 2. Get user's connected GitHub tools
90
+async function getUserGitHubTools(userId: string) {
91
+ return await composio.tools.get(userId, {
92
+ toolkits: ['github'],
93
+ });
94
+}
95
+
96
+// 3. Execute tool for specific user
97
+async function getUserRepos(userId: string) {
98
+ return await composio.tools.execute('GITHUB_LIST_REPOS', {
99
+ userId: userId,
100
+ arguments: {
101
+ per_page: 10,
102
+ },
103
+ });
104
+}
105
+
106
+// Usage in your API endpoint
107
+app.get('/api/github/repos', async (req, res) => {
108
+ const userId = req.user.id; // Get from your auth system
109
+
110
+ try {
111
+ const repos = await getUserRepos(userId);
112
+ res.json(repos.data);
113
+ } catch (error) {
114
+ res.status(500).json({ error: 'Failed to fetch repos' });
115
+ }
116
+});
117
+```
118
+
119
+### Best Practices
120
+
121
+1. **Always validate user IDs** before passing them to Composio methods
122
+2. **Use your authentication system** to ensure users can only access their own data
123
+3. **Keep user IDs consistent** across your application and Composio
124
+4. **Never expose user IDs** in client-side code or logs
125
+5. **Use meaningful, stable identifiers** that won't change over time
126
+
127
## Tools
128
129
Tools are the fundamental units of functionality in Composio. Each tool represents a specific action that can be performed, such as "Get GitHub Repository" or "Send Email". Tools have:
@@ -94,19 +216,19 @@ const customTool = await composio.tools.createCustomTool({
216
param1: {
217
type: 'string',
218
description: 'First parameter',
97
- required: true
98
- }
219
+ required: true,
220
+ },
221
},
222
outputParameters: {
223
result: {
224
type: 'string',
103
- description: 'The result of the operation'
104
- }
225
+ description: 'The result of the operation',
226
+ },
227
},
228
handler: async (params, context) => {
229
// Custom logic here
230
return { data: { result: 'Success!' } };
109
- }
231
+ },
232
});
233
```
234
@@ -119,4 +241,4 @@ Triggers allow your application to respond to events from external services. The
241
const triggers = await composio.triggers.get({
242
toolkits: ['github'],
243
});
122
-```
\ No newline at end of file
244
+```
examples/triggers/src/index.ts
+1
-1
@@ -29,7 +29,7 @@ async function main() {
29
// Get available tools
30
const tools = await composio.tools.get('default', {
31
// Specify the apps you want to use
32
- // apps: ['gmail', 'googlecalendar'],
32
+ toolkits: ['gmail', 'googlecalendar'],
33
limit: 10,
34
});
35