# 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. **On Runtime, a [Function](/docs/functions) 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](/docs/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`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response): ```ts no-run export default { async fetch(request: Request): Promise { 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](/glossary/preview-url). It uses `curl` and `jq` against the [API](/docs/reference/functions), with your key in `RUNTIME_API_KEY`. ```bash check api=https://api.withruntime.com/v2 auth="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/null export 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/null preview=$(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](/docs/reference/previews#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](/docs/jobs). - **Keep keys out of the file.** Read them from environment variables or [secrets](/how-to/use-secrets-in-a-sandbox). ## Related - [What is function as a service (FaaS)?](/glossary/function-as-a-service) - [What is a cold start?](/glossary/cold-start) - [How to deploy a Node.js app](/how-to/deploy-a-nodejs-app), for a server that runs all the time - [How to receive webhooks](/how-to/receive-webhooks) ## Sources - [MDN, Request](https://developer.mozilla.org/en-US/docs/Web/API/Request), checked 17 November 2026 - [MDN, Response](https://developer.mozilla.org/en-US/docs/Web/API/Response), checked 17 November 2026 - [Bun, HTTP server](https://bun.com/docs/api/http), checked 17 November 2026 Facts on this page were checked on 17 November 2026.