Composio TS SDK v3: Zod error 'api_key should be a string, but you provided a undefined'
I'm using the Composio TypeScript SDK v3 (
@composio/core0.1.36-next.0) in a Next.js app. After creating a connection withAuthScheme.APIKey({ api_key }), bothcomposio.connectedAccounts.list({ userIds: [userID] })andcomposio.connectedAccounts.waitForConnection(connectionId)fail with a ZodValidationError: 'The state.val.api_key should be a string, but you provided a undefined'. The connection gets created but the API key isn't saved. How can I fix this or work around it?Originally asked on GitHub: https://github.com/ComposioHQ/composio/issues/1711
6 answers
Fixed: a Composio maintainer confirmed the issue is resolved in
@composio/core0.1.36-next.10, and an earlier fix was announced in 0.1.36-next.5. Upgrade from any older-nextbuild before callingconnectedAccounts.list()orconnectedAccounts.waitForConnection()on an API-key connection.While on a broken build, the reporter's workaround was to bypass the SDK and call the v3 REST API directly:
tsconst response = await fetch( `https://backend.composio.dev/api/v3/connected_accounts?user_ids=${projectId}`, { headers: { 'x-api-key': `${process.env.COMPOSIO_API_KEY}`, 'Content-Type': 'application/json', }, }, ); const connectedAccounts = await response.json();The likely cause, from a commenter's code reading (not a maintainer confirmation):
connectedAccounts.initiate()never appears to pass theAuthScheme.APIKey({ api_key })value into theconnectedAccounts.create()payload, so the connection is stored without the key. The reporter also observed in the dashboard that the API key never gets created. Whenlist()orwaitForConnection()later parses the stored connection, the response schema expectsstate.val.api_keyto be a string and throws the ZodValidationError.Other commenters reported the same failure with any auth type, and one chose to keep using raw API calls until the SDK leaves the
nexttag.General advice: the workaround sends
COMPOSIO_API_KEYin thex-api-keyheader, so keep it in a server-side environment variable and don't run this fetch from client-side code.Did this work for you?What a GitHub user tried on GitHub (Worked)
Worked around the SDK failure by calling
https://backend.composio.dev/api/v3/connected_accounts?user_ids=<id>directly withfetch, sending thex-api-keyandContent-Typeheaders, and readingresponse.json(). The reporter presented this as their working workaround.Source: https://github.com/ComposioHQ/composio/issues/1711 (opens external site)
Did this work for you?What @haxzie tried on GitHub (Outcome unclear)
A Composio contributor stated the issue is fixed in
@composio/core0.1.36-next.10; no user in the thread explicitly confirmed the upgrade worked.Source: https://github.com/ComposioHQ/composio/issues/1711#issuecomment-3055703549 (opens external site)
Did this work for you?What @itlpps tried on GitHub (Outcome unclear)
A maintainer (via Discord) announced a fix in
@composio/core0.1.36-next.5; the reporter only thanked the person relaying the message, without explicit confirmation.Source: https://github.com/ComposioHQ/composio/issues/1711#issuecomment-3027770117 (opens external site)
Did this work for you?What @alizenhom tried on GitHub (Outcome unclear)
Read the
initiate()implementation and suspected theapi_keyoption is never passed into theconnectedAccounts.create()payload, which would explain why the stored connection lacks the key. Diagnosis only.Source: https://github.com/ComposioHQ/composio/issues/1711#issuecomment-3015192586 (opens external site)
Did this work for you?What @alizenhom tried on GitHub (Partly worked)
Followed the v3 docs for connecting an API-key toolkit: a connection was created successfully but the actual
api_keywas not saved in the connected account.Source: https://github.com/ComposioHQ/composio/issues/1711#issuecomment-3021201860 (opens external site)
Did this work for you?
Related questions
- Does Composio support self-hosting / on-prem deployment?
- Composio initiateConnection error: Cannot read properties of null (reading 'join')
- How to fix ImportError: cannot import name 'Action' from 'composio'?
- Can I use Composio in a commercial app? Terms of use ambiguity
- How to fix ValueError: non-default argument follows default argument in composio_gemini?