puchesjr
puchesjr•8mo ago

Using Convex to expose an HTTP API

hey, so we are going to go 100% convex for a new service. We do not need a client exposed api yet, but we will in the future. one of the ideas we'd like to see is autogenerated docs "openapi" and validation for api endpoints if we use convex to host our rest api. if hosting the rest api, we would like the ability to add things like rate limits, etc. to prevent bad actors. if this is not the intended direction/use case of convex longterm, would like to understand the best way to consume the convex database from our external rest api - e.g., created in Go. @jamwt @james
15 Replies
RJ
RJ•8mo ago
Hey @puchesjr, this is all quite well-supported by Convex, IMO. You can wire up libraries like Hono (see, for example, this Stack post on the topic) which would then grant you access to middleware for generating OpenAPI specs and live API documentation (e.g. via Scalar. It looks like Hono has rate limiter middleware that you could use as well, or you could use Convex's rate limiter component. I also maintain and use a library (still in alpha) where I use Effect's version of Hono to accomplish the same thing, and I can confirm that the implementation is very straightforward. If you'd like to see a live example of public API documentation rendered like this feel free to DM me and I can send you a link. Either way, main takeaway is this is well within Convex's wheelhouse; if anything it's (IMO) something that Convex handles really well. And folks here can definitely help you get this set up when the time comes, too.
puchesjr
puchesjrOP•8mo ago
This is fantastic to hear, i'll check it out and thank you for the response. I want to see how fast we can go from ground zero to deploy. we start coding tomorrow.
RJ
RJ•8mo ago
Exciting, good luck!
Neco | nokta.dev
Neco | nokta.dev•6mo ago
@RJ justsaw confect looking interesting. planning to maintain it?
RJ
RJ•6mo ago
100% @Neco | nokta.dev, I use it in production myself. I've been working (very slowly, as I've been traveling abroad for basically the last 9 months) towards a 1.0 release as well.
devagr
devagr•5mo ago
Hey! Is there a way to run internal functions using confect? the context object seems to only expose auth, db, and storage, but not a way to run queries and mutations
RJ
RJ•5mo ago
Hey @devagr, if you're trying to run an internal query or mutation from another query or mutation, you shouldn't (generally) be using runQuery or runMutation to do this (see https://docs.convex.dev/understanding/best-practices/#use-ctxrunquery-and-ctxrunmutation-sparingly-in-queries-and-mutations). If you need runQuery or runMutation to use a component (or because you want partial rollbacks), let me know I can look into adding it!
devagr
devagr•5mo ago
thanks for the response! i figured something like that, switched the internal queries to be regular functions instead
RJ
RJ•5mo ago
No problem! If you have any other questions or run into any other issues, feel free to reach out 🙂
devagr
devagr•5mo ago
do you prefer chatting over DMs or another thread?
RJ
RJ•5mo ago
I'm happy to chat over DMs but if it's stuff that might be useful to other people it would probably be nice to discuss in a public thread
Otis
Otis•3mo ago
Hey @RJ I've read: https://docs.convex.dev/understanding/best-practices/#use-ctxrunquery-and-ctxrunmutation-sparingly-in-queries-and-mutations I have a situation where I have two queries that are used throughout my application. I also have one page where I want to call both queries. To be fair I am using Remix so I am calling them in the loader so it's very speedy anyways. I was looking at ways I could reduce it from two network calls to one and thought about using something like:
Promise.all([
ctx.runQuery(/* ... */),
ctx.runQuery(/* ... */),
]);
Promise.all([
ctx.runQuery(/* ... */),
ctx.runQuery(/* ... */),
]);
To basically call both on the Convex side and then return the results. I could totally use helper functions to handle this but wanted to understand the overhead that the above link mentions. Is it just TypeScript overhead i.e I need to do more type handling or is it actual run time overhead and has an impact on performance?
Best Practices | Convex Developer Hub
Essential best practices for building scalable Convex applications including database queries, function organization, validation, and security.
RJ
RJ•3mo ago
Hi @chopfitzroy, there is actual runtime overhead; each ctx.runQuery executes in a new isolated JS context and runs args/returns validation.
Otis
Otis•3mo ago
Thank you that makes a ton of sense!
RJ
RJ•3mo ago
No problem 🙂

Did you find this page helpful?