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 21, 04:45 UTC
| 1 | /** |
| 2 | * Tools Example |
| 3 | * |
| 4 | * This example demonstrates how to use Composio SDK for tools. |
| 5 | * |
| 6 | * Prerequisites: |
| 7 | * 1. Set up your COMPOSIO_API_KEY in the .env file |
| 8 | * 3. Run the example: pnpm start |
| 9 | */ |
| 10 | |
| 11 | import { Composio } from '@composio/core'; |
| 12 | import 'dotenv/config'; |
| 13 | import path from 'path'; |
| 14 | |
| 15 | /** |
| 16 | * Initialize Composio |
| 17 | */ |
| 18 | const composio = new Composio({ |
| 19 | apiKey: process.env.COMPOSIO_API_KEY, |
| 20 | allowTracking: false, |
| 21 | // Local file paths under fileUploadDirs are uploaded automatically when the tool expects a file |
| 22 | dangerouslyAllowAutoUploadDownloadFiles: true, |
| 23 | fileUploadDirs: [import.meta.dirname], |
| 24 | }); |
| 25 | |
| 26 | const userId = process.env.COMPOSIO_EXAMPLES_USER_ID; // a user with a Google Drive connection |
| 27 | if (!userId) { |
| 28 | throw new Error('Set COMPOSIO_EXAMPLES_USER_ID'); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Main function to run the example |
| 33 | */ |
| 34 | async function main() { |
| 35 | try { |
| 36 | console.log('🚀 Starting Upload file Example...'); |
| 37 | |
| 38 | console.log('🔄 Uploading file...'); |
| 39 | const result = await composio.tools.execute('GOOGLEDRIVE_UPLOAD_FILE', { |
| 40 | dangerouslySkipVersionCheck: true, |
| 41 | arguments: { |
| 42 | file_to_upload: path.join(import.meta.dirname, 'image.png'), |
| 43 | }, |
| 44 | userId, |
| 45 | }); |
| 46 | console.log('✅ File uploaded successfully...'); |
| 47 | console.log(JSON.stringify(result, null, 2)); |
| 48 | |
| 49 | console.log('🔄 Downloading file...'); |
| 50 | const result2 = await composio.tools.execute('GOOGLEDRIVE_DOWNLOAD_FILE', { |
| 51 | dangerouslySkipVersionCheck: true, |
| 52 | arguments: { |
| 53 | file_id: (result.data as unknown as { id: string }).id, |
| 54 | }, |
| 55 | userId, |
| 56 | }); |
| 57 | console.log('✅ File downloaded successfully...'); |
| 58 | console.log(JSON.stringify(result2, null, 2)); |
| 59 | } catch (error) { |
| 60 | console.error('❌ Error running example:', error); |
| 61 | process.exitCode = 1; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Run the example |
| 66 | main().catch(error => { |
| 67 | console.error(error); |
| 68 | process.exitCode = 1; |
| 69 | }); |