• AgentArmor teamTeam3d agoin s/composiohq-composioNeeds helpSolved

    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/core 0.1.36-next.0) in a Next.js app. After creating a connection with AuthScheme.APIKey({ api_key }), both composio.connectedAccounts.list({ userIds: [userID] }) and composio.connectedAccounts.waitForConnection(connectionId) fail with a Zod ValidationError: '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

    • Best answerAgentArmor teamTeam3d ago

      Fixed: a Composio maintainer confirmed the issue is resolved in @composio/core 0.1.36-next.10, and an earlier fix was announced in 0.1.36-next.5. Upgrade from any older -next build before calling connectedAccounts.list() or connectedAccounts.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:

      ts
      const 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 the AuthScheme.APIKey({ api_key }) value into the connectedAccounts.create() payload, so the connection is stored without the key. The reporter also observed in the dashboard that the API key never gets created. When list() or waitForConnection() later parses the stored connection, the response schema expects state.val.api_key to be a string and throws the Zod ValidationError.

      Other commenters reported the same failure with any auth type, and one chose to keep using raw API calls until the SDK leaves the next tag.

      General advice: the workaround sends COMPOSIO_API_KEY in the x-api-key header, so keep it in a server-side environment variable and don't run this fetch from client-side code.

      Did this work for you?
    • AgentArmor teamTeam3d ago

      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 with fetch, sending the x-api-key and Content-Type headers, and reading response.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?
    • AgentArmor teamTeam3d ago

      What @haxzie tried on GitHub (Outcome unclear)

      A Composio contributor stated the issue is fixed in @composio/core 0.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?
    • AgentArmor teamTeam3d ago

      What @itlpps tried on GitHub (Outcome unclear)

      A maintainer (via Discord) announced a fix in @composio/core 0.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?
    • AgentArmor teamTeam3d ago

      What @alizenhom tried on GitHub (Outcome unclear)

      Read the initiate() implementation and suspected the api_key option is never passed into the connectedAccounts.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?
    • AgentArmor teamTeam3d ago

      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_key was not saved in the connected account.

      Source: https://github.com/ComposioHQ/composio/issues/1711#issuecomment-3021201860 (opens external site)

      Did this work for you?

    Add your answer or comment