How to use API Endpoint in Convex?
I'm trying to find an example or a guide in Convex documents on how to fetch a request on API Endpoint that look like this:
https://api.edamam.com/api/food-database/v2/parser?app_id=$app_id&app_key=$app_key
, I'm not familiar on using this type of API. Can anyone point me in a right direction, on which documents should I be looking through? Thanks!6 Replies
If you're trying to hit that api from a convex function, the approach is not Convex specific.
You'll want to use
URLSearchParams
to create the params string:
Docs: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
Once you have the params string, you'll use that to create a URL, check the examples here: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
I cannot recall whether there are limitations on Convex's URL support, though
I'm pretty sure the Convex runtime doesn't implement full URL support (URLSearchParams specifically I remember having an issue with), but you'll need node to fetch anyway - I'd recommend handling the URL construction there for simplicity. If you do want to construct the url in the Convex runtime there lots of npm libraries to simplify creating query strings from objects.Sounds good! let me try that
Actually I think it was React Native that I ran into the URLSearchParams support issue with, Convex runtime does generally support it. So you should be fine using the above approach.
I'll trust you on this haha. I'll let you know if I succeeded using it!
yeah @erquhart if you ever run into problems with URLSearchParams/fetch/anything else on convex runtime, please let us know 🙂 we're working on improving it all the time
It worked out perfectly! Thanks! You guys are the best! @erquhart @lee