#HTTP Action route segments?

4 messages · Page 1 of 1 (latest)

jade juniper
#

Is it possible to have an HTTP Action that includes dynamic route segments? E.g: /receive/<string>, where <string> comes through to the action?

nocturne kindleBOT
#

Hi @jade juniper. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, it is possible to have an HTTP Action that includes dynamic route segments in Convex. However, the built-in router in Convex is quite simple and does not directly support dynamic routes or slug routes like /users/:userId.

To achieve this, you can leverage existing JavaScript libraries like Hono. Hono allows you to add more advanced features to your HTTP endpoints, including dynamic routes.

Here's an example of how you can extend Convex HTTP actions using Hono:

import { httpRouter, ROUTABLE_HTTP_METHODS } from "convex/server";
import { httpAction } from "./_generated/server";
import { HonoWithConvex } from "./lib/honoWithConvex";

const app: HonoWithConvex = new Hono();

// Add your routes to `app`.

const http = httpRouter();
for (const routableMethod of ROUTABLE_HTTP_METHODS) {
	http.route({
		pathPrefix: "/",
		method: routableMethod,
		handler: httpAction(async (ctx, request: Request) => {
			return await app.fetch(request, ctx);
		}),
	})
}
export default http;

In this example, all HTTP requests are handled with a single httpAction and complex routing is done within that action.

For more details, you can refer to the Convex documentation on Advanced HTTP Endpoints (https://stack.convex.dev/hono-with-convex#under-the-hood) and the Convex documentation on HTTP Actions (https://docs.convex.dev/functions/http-actions).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@jade juniper kapa.ai is still learning and improving, please let us know how it did by reacting below