OccultSlolem
OccultSlolem3y ago

Fetch api

Does Convex not support the fetch API?
22 Replies
nipunn
nipunn3y ago
Yeah! You can use it from an action
OccultSlolem
OccultSlolemOP3y ago
It says that fetch is not defined
nipunn
nipunn3y ago
Not from a query or mutation as those must be deterministic Check out actions
OccultSlolem
OccultSlolemOP3y ago
I'm using an HTTP endpoint
nipunn
nipunn3y ago
Actions allow you to call out to third party services You can call an action from there
OccultSlolem
OccultSlolemOP3y ago
Do HTTP endpoints have to be deterministic? I'm not using any queries or mutations The function has to be callable from an HTTP request
const promptModel = httpEndpoint(async ({ }, request) => {
const response = await fetch('https://api.openai.com/v1/models', { // ERR
method: 'GET',
headers: {
Authorization: `Bearer sk-`,
}
});
const data = await response.json();
return new Response(JSON.stringify(data), {
headers: {
"content-type": "application/json",
},
status: 200,
});
});
const promptModel = httpEndpoint(async ({ }, request) => {
const response = await fetch('https://api.openai.com/v1/models', { // ERR
method: 'GET',
headers: {
Authorization: `Bearer sk-`,
}
});
const data = await response.json();
return new Response(JSON.stringify(data), {
headers: {
"content-type": "application/json",
},
status: 200,
});
});
nipunn
nipunn3y ago
Thinking… I believe http endpoints do not have to be deterministic, but I don’t think they have fetch available like actions. Let me double check this Thanks for the question
nipunn
nipunn3y ago
ok yeah - check out limits here: https://docs.convex.dev/using/http-endpoints#limits. Namely: HTTP endpoints are run from the same Convex environment as queries and mutations, and have access to fewer built-in APIs. HTTP endpoints can call actions, which run in Node.js, if you want to use Node.js features. To do what you're trying to do, I would recommend calling an action from your http endpoint and then it should work
nipunn
nipunn3y ago
appreciate the feedback though! Maybe there's a way we can make this more clear from the error message.
OccultSlolem
OccultSlolemOP3y ago
Darn, that would make things a good bit less complex if I could just use all the JS APIs from there Thanks
nipunn
nipunn3y ago
Something like
const response = runAction("talkToOpenAI")
const response = runAction("talkToOpenAI")
And an action like this
export default action(async ({}) => {
const response = await fetch("http://openai...");
response.json() // etc
}
export default action(async ({}) => {
const response = await fetch("http://openai...");
response.json() // etc
}
OccultSlolem
OccultSlolemOP3y ago
Where do I specify the action name again?
nipunn
nipunn3y ago
https://docs.convex.dev/using/actions There's two options. 1) will default to the filename in convex/actions/
export default action(async ...
export default action(async ...
2) Reference it as "filename:functionName"
export functionName = action(async ...)
export functionName = action(async ...)
OccultSlolem
OccultSlolemOP3y ago
ahh right. Thank you!
nipunn
nipunn3y ago
There's actually a better example in the queries section https://docs.convex.dev/using/writing-convex-functions#defining-convex-functions - but same idea for actions
Writing Convex Functions | Convex Developer Hub
Functions describe server behavior.
nipunn
nipunn3y ago
hell ya happy hacking
OccultSlolem
OccultSlolemOP3y ago
What package is runAction() exported from? I can't seem to find it in the convex package or the generated files. Might just be overlooking it lolol.
nipunn
nipunn3y ago
It comes in the http context Check the http endpoint doc for stuff to copy paste. It comes in as an argument
OccultSlolem
OccultSlolemOP3y ago
Is it runRequest()? oh wait nvm
nipunn
nipunn3y ago
const postMessage = httpEndpoint(async ({ runAction }, request) => {
const postMessage = httpEndpoint(async ({ runAction }, request) => {
something like that
OccultSlolem
OccultSlolemOP3y ago
there we go :]

Did you find this page helpful?