Skip to content

How-to guides

How to deploy an HTTP function

Write a handler that takes a Request and returns a Response, export it as the module's default fetch, and upload it to a function host.

Updated · Facts checked · By Runtime Cloud

3 min read

Read as Markdown

On Runtime, a Function serves the handler in /app/index.ts on port 8080. It pauses after 60 seconds with nothing happening, keeps its memory, and the next request resumes it. You pay $0.03 per busy vCPU-hour while it answers, and nothing for CPU while it is paused (pricing).

Write the handler

The Web-standard handler is a module whose default export has a fetch method. Cloudflare Workers, Bun and Deno take this shape, so one file runs on each of them. The handler gets a Request and returns a Response:

TypeScriptexport default {  async fetch(request: Request): Promise<Response> {    const url = new URL(request.url);    if (request.method === "POST" && url.pathname === "/echo") {      return Response.json(await request.json());    }    if (url.pathname === "/health") return new Response("ok");    return new Response("not found", { status: 404 });  },};

Route on url.pathname and request.method. Read the body once, with request.json(), request.text() or request.formData(). Return Response.json() for JSON, so the Content-Type header is set for you.

Deploy it on Runtime

This script creates a Function, writes the handler to /app/index.ts, restarts the Function's server so it loads the handler, and calls it through a preview. It uses curl and jq against the API, with your key in RUNTIME_API_KEY.

Terminalapi=https://api.withruntime.com/v2auth="Authorization: Bearer $RUNTIME_API_KEY"fn=$(curl -sf -X POST "$api/functions" -H "$auth" -H "Content-Type: application/json" -d '{}' | jq -r .id)trap 'curl -sf -X DELETE "$api/functions/$fn" -H "$auth" > /dev/null' EXIT# The handler: a module whose default export has fetch(request).cat <<'TS' | curl -sf -X PUT "$api/functions/$fn/files?path=/app/index.ts" -H "$auth" \  -H "Content-Type: application/octet-stream" --data-binary @- > /dev/nullexport default {  async fetch(request: Request) {    const url = new URL(request.url);    return Response.json({ method: request.method, path: url.pathname });  },};TS# Restart the function's server so it loads the new handler.curl -sf -X POST "$api/functions/$fn:exec" -H "$auth" -H "Content-Type: application/json" \  -d '{"cmd": "sudo pkill -f runtime-function || true; sudo -E -u runtime runtime-function", "wait": false}' > /dev/nullpreview=$(curl -sf -X POST "$api/sandboxes/$fn/previews" -H "$auth" \  -H "Content-Type: application/json" -d '{"port": 8080}')url=$(jq -r .url <<< "$preview")token=$(jq -r .token <<< "$preview")curl -sf --retry 40 --retry-all-errors --retry-delay 1 "${url}hello" -H "x-runtime-preview-token: $token"# {"method":"GET","path":"/hello"}

The trap deletes the Function when the script ends. Leave it out to keep it. A private preview needs its token in the x-runtime-preview-token header. To serve anyone, create the preview with "visibility": "public", which needs an account with a kept top-up (PreviewCreate).

Keep it fast and correct

  • Do the slow setup once. Code at the top of the module runs when the handler loads, not on every request. Open database pools and read configuration there.
  • Expect a pause between requests. A Runtime Function keeps its memory across a pause, so a cache in a module variable survives it. A host that starts a fresh instance per request does not keep it, so treat any in-memory state as a cache, never as the only copy.
  • Answer before the work is done, when it is long. Return 202 Accepted for a job that takes minutes, and hand the work to a queue or a scheduled job.
  • Keep keys out of the file. Read them from environment variables or secrets.

Sources

Facts on this page were checked on 17 November 2026.

Cut your cloud billby 66%.

7 days of Pro for $0, and no card to sign up. Billed by the second after.

Start your free trial