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 23, 00:30 UTC
Allow multiple connected account flag
haxzie committed
Jun 16, 2025 at 19:07 UTC
26a0fd14757b4eec020160f6e0dd8e728eef66cf
5 files changed
+46
-5
examples/connected-accounts/src/multiple-connected-accounts.ts
new
+11
@@ -0,0 +1,11 @@
1
+import { Composio } from '@composio/core';
2
+
3
+const composio = new Composio({
4
+ apiKey: process.env.COMPOSIO_API_KEY,
5
+});
6
+
7
+const connectionRequest = await composio.connectedAccounts.initiate('default', 'ac_NSC2s9WqTE4n', {
8
+ allowMultiple: true,
9
+});
10
+
11
+console.log(connectionRequest.redirectUrl);
packages/core/src/errors/ConnectedAccountsErrors.ts
+13
@@ -2,6 +2,7 @@ import { ComposioError, ComposioErrorOptions } from './ComposioError';
2
3
export const ConnectedAccountErrorCodes = {
4
CONNECTED_ACCOUNT_NOT_FOUND: 'CONNECTED_ACCOUNT_NOT_FOUND',
5
+ MULTIPLE_CONNECTED_ACCOUNTS: 'MULTIPLE_CONNECTED_ACCOUNTS',
6
} as const;
7
8
export class ComposioConnectedAccountNotFoundError extends ComposioError {
@@ -20,3 +21,15 @@ export class ComposioConnectedAccountNotFoundError extends ComposioError {
21
this.name = 'ComposioConnectedAccountNotFoundError';
22
}
23
}
24
+
25
+export class ComposioMultipleConnectedAccountsError extends ComposioError {
26
+ constructor(message: string = 'Multiple connected accounts found') {
27
+ super(message, {
28
+ code: ConnectedAccountErrorCodes.MULTIPLE_CONNECTED_ACCOUNTS,
29
+ possibleFixes: [
30
+ 'Use the allowMultiple flag to allow multiple connected accounts per user for an auth config',
31
+ ],
32
+ });
33
+ this.name = 'ComposioMultipleConnectedAccountsError';
34
+ }
35
+}
packages/core/src/models/ConnectedAccounts.ts
+17
@@ -25,6 +25,8 @@ import { ConnectionRequest } from './ConnectionRequest';
25
import { ValidationError } from '../errors/ValidationErrors';
26
import { telemetry } from '../telemetry/Telemetry';
27
import { transformConnectedAccountResponse } from '../utils/transformers/connectedAccounts';
28
+import { ComposioMultipleConnectedAccountsError } from '../errors';
29
+import logger from '../utils/logger';
30
/**
31
* ConnectedAccounts class
32
*
@@ -156,6 +158,21 @@ export class ConnectedAccounts {
158
authConfigId: string,
159
options?: CreateConnectedAccountOptions
160
): Promise<ConnectionRequest> {
161
+ // Check if there are multiple connected accounts for the authConfig of the user
162
+ const connectedAccount = await this.list({
163
+ userIds: [userId],
164
+ authConfigIds: [authConfigId],
165
+ });
166
+ if (connectedAccount.items.length > 0 && !options?.allowMultiple) {
167
+ throw new ComposioMultipleConnectedAccountsError(
168
+ `Multiple connected accounts found for user ${userId} in auth config ${authConfigId}`
169
+ );
170
+ } else if (connectedAccount.items.length > 0) {
171
+ logger.warn(
172
+ `[Warn:AllowMultiple] Multiple connected accounts found for user ${userId} in auth config ${authConfigId}`
173
+ );
174
+ }
175
+
176
const response = await this.client.connectedAccounts.create({
177
auth_config: {
178
id: authConfigId,
packages/core/src/models/Toolkits.ts
+4
-1
@@ -411,6 +411,9 @@ export class Toolkits {
411
412
// create the auth config
413
const composioConnectedAccount = new ConnectedAccounts(this.client);
414
- return await composioConnectedAccount.initiate(userId, authConfigIdToUse);
414
+ return await composioConnectedAccount.initiate(userId, authConfigIdToUse, {
415
+ // in this magic function we allow multiple connected accounts per user for an auth config
416
+ allowMultiple: true,
417
+ });
418
}
419
}
packages/core/src/types/connectedAccounts.types.ts
+1
-4
@@ -45,15 +45,12 @@ export const DefaultCreateConnectedAccountParamsSchema = z.object({
45
});
46
47
export const CreateConnectedAccountOptionsSchema = z.object({
48
+ allowMultiple: z.boolean().optional(),
49
callbackUrl: z.string().optional(),
50
config: ConnectionDataSchema.optional(),
51
});
52
export type CreateConnectedAccountOptions = z.infer<typeof CreateConnectedAccountOptionsSchema>;
53
export type CreateConnectedAccountParams = z.infer<typeof CreateConnectedAccountParamsSchema>;
53
-export type CreateConnectedAccountOptionsSchema = z.infer<
54
- typeof CreateConnectedAccountOptionsSchema
55
->;
56
-
54
/**
55
* Connected Account create response
56
*/