ts/examples/connected-accounts/README.md
This example demonstrates how to work with connected accounts in the Composio SDK, including authorization flows and waiting for connections to become active.
Connected accounts in Composio enable your application to interact with third-party services on behalf of your users. This example showcases:
The waitForConnection method is a crucial part of the connection flow. It allows your application to:
// From a ConnectionRequest object
await connectionRequest.waitForConnection(timeout?: number): Promise<ConnectedAccountRetrieveResponse>
// From the ConnectedAccounts class
await composio.connectedAccounts.waitForConnection(
connectedAccountId: string,
timeout?: number
): Promise<ConnectedAccountRetrieveResponse>
// Create a connection request
const connectionRequest = await composio.toolkits.authorize('default', 'github');
// If there's a redirect URL, show it to the user
if (connectionRequest.redirectUrl) {
console.log(`Please visit: ${connectionRequest.redirectUrl}`);
}
// Wait for the connection to be established (with default 60s timeout)
try {
const connectedAccount = await connectionRequest.waitForConnection();
console.log(`Connection successful! ID: ${connectedAccount.id}`);
} catch (error) {
if (error instanceof ConnectionRequestTimeoutError) {
console.error('Connection timed out. Please try again.');
} else if (error instanceof ConnectionRequestFailedError) {
console.error(`Connection failed: ${error.message}`);
}
}
// Wait for up to 3 minutes
const connectedAccount = await connectionRequest.waitForConnection(180000);
The waitForConnection method handles different connection states:
ACTIVE: Returns the connected accountFAILED, EXPIRED, DELETED: Throws a ConnectionRequestFailedErrorConnectionRequestTimeoutErrorInstall dependencies:
pnpm install
Set your API key:
export COMPOSIO_API_KEY=your_api_key
Run the example:
pnpm start