Calonz
Calonz•10mo ago

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
erquhart
erquhart•10mo ago
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:
// Search parameters can also be an object
const paramsObj = { foo: "bar", baz: "bar" };
const searchParams = new URLSearchParams(paramsObj);

console.log(searchParams.toString()); // "foo=bar&baz=bar"
// Search parameters can also be an object
const paramsObj = { foo: "bar", baz: "bar" };
const searchParams = new URLSearchParams(paramsObj);

console.log(searchParams.toString()); // "foo=bar&baz=bar"
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.
Calonz
CalonzOP•10mo ago
Sounds good! let me try that
erquhart
erquhart•10mo ago
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.
Calonz
CalonzOP•10mo ago
I'll trust you on this haha. I'll let you know if I succeeded using it!
lee
lee•10mo ago
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
Calonz
CalonzOP•10mo ago
It worked out perfectly! Thanks! You guys are the best! @erquhart @lee

Did you find this page helpful?