Skip to content

Calling functions

A server calls a Convex function over HTTP, at the deployment’s URL. Anyone with the URL can call a public function, and no key is needed. An internal function only answers other functions in the same deployment. Your functions decide what each caller may do, from the identity it sent.

RunQueryinternalproddeploymentApiyour Workermessages:listpublic querymessages:notifyinternal actionRunQueryinternalproddeploymentApiyour Workermessages:listpublic querymessages:notifyinternal action
The Worker reaches the public query with the deployment's URL alone. An internal function only answers the deployment's own functions, like a mutation that schedules it.

Bind the deployment, then pass the function’s declaration. Arguments, result and failure are typed by it:

src/api.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Convex from "@alchemy.run/convex";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Backend } from "./backend.ts";
import { ListMessages } from "./messages.ts";
export default class Api extends Cloudflare.Worker<Api>()(
"Api",
{ main: import.meta.url },
Effect.gen(function* () {
const list = yield* Convex.RunQuery(Backend);
return {
fetch: Effect.gen(function* () {
const messages = yield* list(ListMessages, { channel: "general" });
return yield* HttpServerResponse.json(messages);
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(Convex.RunQueryHttp)),
) {}

The arguments are encoded with the declaration’s schema and the result decoded, so a Date field arrives as a Date. RunQuery grants nothing: the Worker gets the deployment’s URL.

RunMutation and RunAction work the same way, and a declared failure arrives as its own tagged error:

const add = yield* Convex.RunMutation(Backend);
yield* add(AddMessage, { channel: "", body: "hi" }).pipe(
Effect.catchTag("EmptyChannel", () => Effect.log("pick a channel")),
);

Provide Convex.RunMutationHttp and Convex.RunActionHttp beside RunQueryHttp. A mutation or action isn’t retried, because a lost response could run it twice.

A function written without Alchemy, like one in another app, is called by its path. Arguments and results use Convex’s JSON format, so Int64 and Bytes arrive as tagged objects:

const list = yield* Convex.RunQuery(Backend);
const rows = yield* list("messages:list", { channel: "general" });
const batch = yield* Convex.RunQueryBatch(Backend);
const [messages, user] = yield* batch([{ path: "messages:list" }, { path: "users:me" }]);

Every query sees the same snapshot of the database.

A function that throws, or doesn’t exist, fails with ConvexFunctionError:

yield* list("messages:list").pipe(
Effect.catchTag("ConvexFunctionError", (error) => Effect.log(error.path, error.message)),
);
const usage = yield* Convex.GetCurrentUsage(Backend);
const { metrics } = yield* usage();

The host gets a deploy key limited to deployment:usage:view on this deployment.

const audit = yield* Convex.ListAuditLogEvents(Backend);
const page = yield* audit({ from: Date.now() - 3_600_000 });

The host gets a deploy key limited to deployment:auditLog:view on this deployment. On a plan that keeps no audit logs, this fails with AuditLogsTooOld.

const data = yield* Convex.DataSync(Backend);
const page = yield* data.sync({});

The host gets a deploy key limited to deployment:data:view on this deployment, which reads every table. This needs the Professional plan. Below it, calls fail with StreamingExportNotEnabled.

To use the convex package’s client in a Worker, give it the deployment’s URLs with Connect.