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.
Call a declared function
Section titled “Call a declared function”Bind the deployment, then pass the function’s declaration. Arguments, result and failure are typed by it:
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.
Run a mutation or an action
Section titled “Run a mutation or an action”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.
Call a function by path
Section titled “Call a function by path”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" });Run several queries together
Section titled “Run several queries together”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.
Handle a failure
Section titled “Handle a failure”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)),);Read usage
Section titled “Read usage”const usage = yield* Convex.GetCurrentUsage(Backend);const { metrics } = yield* usage();The host gets a deploy key limited to deployment:usage:view on this deployment.
Read the audit log
Section titled “Read the audit log”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.
Export data
Section titled “Export data”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.
Use Convex’s own client
Section titled “Use Convex’s own client”To use the convex package’s client in a Worker, give it the deployment’s URLs with Connect.