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, 06:00 UTC
| 1 | # Connected Accounts Example |
| 2 | |
| 3 | This example demonstrates how to work with connected accounts in the Composio SDK, including authorization flows and waiting for connections to become active. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | Connected accounts in Composio enable your application to interact with third-party services on behalf of your users. This example showcases: |
| 8 | |
| 9 | 1. Creating connection requests |
| 10 | 2. Directing users to authorization pages |
| 11 | 3. Waiting for connections to become active |
| 12 | 4. Using connected accounts with tools |
| 13 | |
| 14 | ## Example Files |
| 15 | |
| 16 | - **toolkit-authorize.ts**: Demonstrates authorizing a toolkit and waiting for the connection |
| 17 | - **index.ts**: Basic entry point that imports the examples |
| 18 | |
| 19 | ## Using the waitForConnection Method |
| 20 | |
| 21 | The `waitForConnection` method is a crucial part of the connection flow. It allows your application to: |
| 22 | |
| 23 | - Poll the Composio API until a connection becomes active |
| 24 | - Handle connection failures gracefully |
| 25 | - Set appropriate timeouts for your use case |
| 26 | |
| 27 | ### Syntax |
| 28 | |
| 29 | ```typescript |
| 30 | // From a ConnectionRequest object |
| 31 | await connectionRequest.waitForConnection(timeout?: number): Promise<ConnectedAccountRetrieveResponse> |
| 32 | |
| 33 | // From the ConnectedAccounts class |
| 34 | await composio.connectedAccounts.waitForConnection( |
| 35 | connectedAccountId: string, |
| 36 | timeout?: number |
| 37 | ): Promise<ConnectedAccountRetrieveResponse> |
| 38 | ``` |
| 39 | |
| 40 | ### Example Usage |
| 41 | |
| 42 | ```typescript |
| 43 | // Create a connection request |
| 44 | const connectionRequest = await composio.toolkits.authorize('default', 'github'); |
| 45 | |
| 46 | // If there's a redirect URL, show it to the user |
| 47 | if (connectionRequest.redirectUrl) { |
| 48 | console.log(`Please visit: ${connectionRequest.redirectUrl}`); |
| 49 | } |
| 50 | |
| 51 | // Wait for the connection to be established (with default 60s timeout) |
| 52 | try { |
| 53 | const connectedAccount = await connectionRequest.waitForConnection(); |
| 54 | console.log(`Connection successful! ID: ${connectedAccount.id}`); |
| 55 | } catch (error) { |
| 56 | if (error instanceof ConnectionRequestTimeoutError) { |
| 57 | console.error('Connection timed out. Please try again.'); |
| 58 | } else if (error instanceof ConnectionRequestFailedError) { |
| 59 | console.error(`Connection failed: ${error.message}`); |
| 60 | } |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ### With Custom Timeout |
| 65 | |
| 66 | ```typescript |
| 67 | // Wait for up to 3 minutes |
| 68 | const connectedAccount = await connectionRequest.waitForConnection(180000); |
| 69 | ``` |
| 70 | |
| 71 | ### Handling Connection States |
| 72 | |
| 73 | The `waitForConnection` method handles different connection states: |
| 74 | |
| 75 | - `ACTIVE`: Returns the connected account |
| 76 | - `FAILED`, `EXPIRED`, `DELETED`: Throws a `ConnectionRequestFailedError` |
| 77 | - Timeout exceeded: Throws a `ConnectionRequestTimeoutError` |
| 78 | |
| 79 | ## Running This Example |
| 80 | |
| 81 | 1. Install dependencies: |
| 82 | |
| 83 | ```bash |
| 84 | pnpm install |
| 85 | ``` |
| 86 | |
| 87 | 2. Set your API key: |
| 88 | |
| 89 | ```bash |
| 90 | export COMPOSIO_API_KEY=your_api_key |
| 91 | ``` |
| 92 | |
| 93 | 3. Run the example: |
| 94 | ```bash |
| 95 | pnpm start |
| 96 | ``` |