@devries-cameron20 / composio-1 / commits / 1765dd60f
Fork 0

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 documentation related to connectedAccounts.waitforconnection

haxzie committed May 20, 2025 at 19:00 UTC 1765dd60f46340664de8e075e1859da6a72fb81c
3 files changed +141
README.md
+24
@@ -296,6 +296,30 @@ const connectionRequest = await composio.createConnectedAccount(
296 const connectedAccount = await connectionRequest.waitForConnection();
297 ```
298
299 +### Waiting for Connection to be Established
300 +
301 +The SDK provides a convenient way to wait for a connection to be established using the `waitForConnection` method:
302 +
303 +```typescript
304 +// From a ConnectionRequest instance (after creating a connected account)
305 +// With default timeout (60 seconds)
306 +const connectedAccount = await connectionRequest.waitForConnection();
307 +
308 +// With custom timeout (2 minutes)
309 +const connectedAccount = await connectionRequest.waitForConnection(120000);
310 +
311 +// Directly from the ConnectedAccounts class (with an existing connected account ID)
312 +const connectedAccount = await composio.connectedAccounts.waitForConnection('conn_abc123', 60000);
313 +```
314 +
315 +The method polls the Composio API until:
316 +
317 +- The connection becomes `ACTIVE` (returns the connected account)
318 +- The connection enters a terminal state (`FAILED`, `EXPIRED`, `DELETED`) and throws an error
319 +- The timeout is exceeded (throws a timeout error)
320 +
321 +This is particularly useful for OAuth flows where the user needs to authorize the connection in a browser.
322 +
323 ### Managing Connected Accounts
324
325 ```typescript
examples/connected-accounts/README.md new
+97
@@ -0,0 +1,97 @@
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 +- **magic-flow-demo.ts**: Shows how to use the magic flow for a streamlined connection experience
18 +- **index.ts**: Basic entry point that imports the examples
19 +
20 +## Using the waitForConnection Method
21 +
22 +The `waitForConnection` method is a crucial part of the connection flow. It allows your application to:
23 +
24 +- Poll the Composio API until a connection becomes active
25 +- Handle connection failures gracefully
26 +- Set appropriate timeouts for your use case
27 +
28 +### Syntax
29 +
30 +```typescript
31 +// From a ConnectionRequest object
32 +await connectionRequest.waitForConnection(timeout?: number): Promise<ConnectedAccountRetrieveResponse>
33 +
34 +// From the ConnectedAccounts class
35 +await composio.connectedAccounts.waitForConnection(
36 + connectedAccountId: string,
37 + timeout?: number
38 +): Promise<ConnectedAccountRetrieveResponse>
39 +```
40 +
41 +### Example Usage
42 +
43 +```typescript
44 +// Create a connection request
45 +const connectionRequest = await composio.toolkits.authorize('default', 'github');
46 +
47 +// If there's a redirect URL, show it to the user
48 +if (connectionRequest.redirectUrl) {
49 + console.log(`Please visit: ${connectionRequest.redirectUrl}`);
50 +}
51 +
52 +// Wait for the connection to be established (with default 60s timeout)
53 +try {
54 + const connectedAccount = await connectionRequest.waitForConnection();
55 + console.log(`Connection successful! ID: ${connectedAccount.id}`);
56 +} catch (error) {
57 + if (error instanceof ConnectionRequestTimeoutError) {
58 + console.error('Connection timed out. Please try again.');
59 + } else if (error instanceof ConnectionRequestFailedError) {
60 + console.error(`Connection failed: ${error.message}`);
61 + }
62 +}
63 +```
64 +
65 +### With Custom Timeout
66 +
67 +```typescript
68 +// Wait for up to 3 minutes
69 +const connectedAccount = await connectionRequest.waitForConnection(180000);
70 +```
71 +
72 +### Handling Connection States
73 +
74 +The `waitForConnection` method handles different connection states:
75 +
76 +- `ACTIVE`: Returns the connected account
77 +- `FAILED`, `EXPIRED`, `DELETED`: Throws a `ConnectionRequestFailedError`
78 +- Timeout exceeded: Throws a `ConnectionRequestTimeoutError`
79 +
80 +## Running This Example
81 +
82 +1. Install dependencies:
83 +
84 + ```bash
85 + pnpm install
86 + ```
87 +
88 +2. Set your API key:
89 +
90 + ```bash
91 + export COMPOSIO_API_KEY=your_api_key
92 + ```
93 +
94 +3. Run the example:
95 + ```bash
96 + pnpm start
97 + ```
packages/core/README.md
+20
@@ -145,6 +145,26 @@ const connectionRequest = await composio.createConnectedAccount(
145 const connectedAccount = await connectionRequest.waitForConnection();
146 ```
147
148 +### Waiting for Connection Establishment
149 +
150 +The `waitForConnection` method is available on both the `ConnectionRequest` and `ConnectedAccounts` classes. It allows you to poll for a connection to become active:
151 +
152 +```typescript
153 +// From a ConnectionRequest instance (returned by createConnectedAccount)
154 +const connectedAccount = await connectionRequest.waitForConnection(120000); // 2 minute timeout
155 +
156 +// From the ConnectedAccounts class (using a connected account ID)
157 +const connectedAccount = await composio.connectedAccounts.waitForConnection('conn_abc123', 60000); // 1 minute timeout
158 +```
159 +
160 +The method continuously polls the Composio API until the connection:
161 +
162 +- Becomes `ACTIVE` (returns the connected account)
163 +- Enters a terminal state like `FAILED`, `EXPIRED`, or `DELETED` (throws an error)
164 +- Exceeds the specified timeout (throws a timeout error)
165 +
166 +If the connection does not complete within the provided timeout (default: 60 seconds), a `ConnectionRequestTimeoutError` is thrown.
167 +
168 ### Managing Connected Accounts
169
170 ```typescript