#custom functions with http actions
8 messages · Page 1 of 1 (latest)
Hi @mighty moat. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You can define custom functions with HTTP actions in Convex to build your own HTTP API endpoints. HTTP actions allow you to handle incoming HTTP requests, process them however you like, and return custom responses. This is useful for integrating with third-party services, handling webhooks, or exposing a public API for your app.
How to Define Custom HTTP Actions
-
Create an HTTP Action:
Use thehttpActionconstructor to define your handler. The handler receives a context (ctx) and aRequestobject, allowing you to parse the request, run queries/mutations/actions, and return a customResponse.import { httpAction } from "./_generated/server"; export const doSomething = httpAction(async (ctx, request) => { // Your custom logic here return new Response("Hello from custom HTTP action!"); }); -
Expose the Action via a Route:
Use thehttpRouterto define routes and expose your custom HTTP actions.import { httpRouter } from "convex/server"; import { doSomething } from "./myHttpActions"; const http = httpRouter(); http.route({ path: "/doSomething", method: "POST", handler: doSomething, }); export default http; -
Custom Logic:
Inside your handler, you can:- Parse the request body or query parameters.
- Run Convex queries, mutations, or actions.
- Implement custom authentication, validation, or business logic.
- Return any custom response (JSON, text, file, etc.).
Example of parsing JSON and running a mutation:
import { httpAction } from "./_generated/server";
import { internal } from "./_generated/api";
export const postMessage = httpAction(async (ctx, request) => {
const { author, body } = await request.json();
await ctx.runMutation(internal.messages.sendOne, {
body: `Sent via HTTP action: ${body}`,
author,
});
return new Response(null, { status: 200 });
});
-
Advanced Use Cases:
- You can use middleware patterns, libraries like Hono for advanced routing, or add CORS/authentication headers as needed.
- For example, you can add CORS headers to your responses to allow cross-origin requests, or handle file uploads/downloads via HTTP actions.
In summary: Convex HTTP actions are highly flexible and allow you to define custom backend logic accessible via HTTP endpoints, making it easy to build custom APIs, webhook handlers, and more.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@prisma tundra Kapa.ai is still learning and improving, please let me know how I did by reacting below.
can i use customfunctions from convex-helpers with httpactions @mossy nebula
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources do not explicitly mention using customFunctions from convex-helpers (such as customQuery, customMutation, or customAction) directly with Convex HTTP actions (httpAction). The examples and documentation for customFunctions focus on customizing queries, mutations, and actions, but do not reference HTTP actions or show usage with them.
However, you can use similar patterns inside your HTTP action handlers—such as calling your custom queries or mutations from within an HTTP action using ctx.runQuery, ctx.runMutation, or ctx.runAction. But there is no direct evidence in the provided sources that customFunctions can be used as a drop-in replacement for httpAction or to wrap HTTP actions themselves.
If you need advanced routing or middleware-like behavior for HTTP endpoints, the sources suggest using libraries like Hono with Convex HTTP actions, but again, this is separate from the customFunctions helpers pattern Advanced HTTP Endpoints: Convex ❤️ Hono.