#Error when calling 3rd party API with GET method from action

4 messages · Page 1 of 1 (latest)

jade archBOT
#

Hi @zinc geyser. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the knowledge sources, I can help you with your issue regarding calling a 3rd party API from a Convex action.

The error message you're receiving suggests that the LiveKit API you're trying to call expects a POST request, but your code is attempting a GET request. This is not a limitation of Convex actions, but rather a requirement of the specific API you're calling.

To resolve this issue, you need to modify your API call to use the POST method instead of GET. Here's how you can adjust your action to make a POST request:

export const create = action({
  args: {
    agentName: v.string(),
    payload: v.optional(v.record(v.string(), v.any())),
  },
  handler: async (ctx, args) => {
    const url = process.env.LIVEKIT_URL
    const key = process.env.LIVEKIT_API_KEY
    const secret = process.env.LIVEKIT_API_SECRET
  
    if (!url || !key || !secret) {
      throw new Error('Missing LiveKit credentials')
    }
  
    try {
      const response = await fetch(`${url}/twirp/livekit.AgentDispatchService/CreateDispatch`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${key}:${secret}` // You may need to adjust this based on LiveKit's authentication requirements
        },
        body: JSON.stringify({
          room: "room",
          agentName: args.agentName,
          metadata: args.payload ? JSON.stringify(args.payload) : undefined
        })
      });

      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }

      const dispatch = await response.json();
      return dispatch.id || null;
    } catch (e) {
      console.error(new Error(`Failed creating dispatch for the agent: ${e}`))
      return null;
    }
  },
})
#

This modification should resolve the "unsupported method" error you're encountering.

It's important to note that Convex actions are the correct place to make API calls to third-party services. As mentioned in the Convex documentation, actions can call third-party services (https://docs.convex.dev/functions/actions#calling-third-party-apis-and-using-npm-packages) and are the appropriate function type for this use case.

Your initial approach of using an action was correct. The error you encountered was not due to a limitation of Convex actions, but rather due to the specific requirements of the LiveKit API you're calling.

If you continue to face issues, you may want to consult the LiveKit API documentation to ensure you're formatting the request correctly for their service.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@zinc geyser Kapa.ai is still learning and improving, please let me know how I did by reacting below.