Serverless functions · Pro and Team

A few API routes, beside the pages.

Put a JavaScript file under _functions/ and it answers the path it is named after — receive a webhook, call an API with a key the browser never sees, handle a form your way. It publishes with the rest of the folder and runs per request on the edge that serves your pages. There is no server to keep running.

_functions/api/hello.js → /api/hello
export async function onRequestPost({ request, env }) {
  const { name } = await request.json();
  return Response.json({ greeting: `Hello, ${name}` });
}

Included on Pro and Team. Every rule, in the documentation →

What it is for

The few things a page cannot do on its own.

Everything else on the site stays a file served from the edge. A function is the small piece that has to happen somewhere other than the visitor's browser.

  • webhook

    Receive a webhook

    Point Stripe, GitHub or a form service at /api/webhook on your own site, check the signature with your secret, and act on what arrived.

  • env.API_KEY

    Call an API with your key

    The page calls your route; your route calls the API with a key read from env. The key is set in the dashboard, never published, and never reaches a visitor.

  • onRequestPost

    Handle a form your way

    Email it, post it to Slack or a CRM, check it against an API. Built-in form collection stores submissions for you; a function is for when you want something else to happen.

How it works

A file is a route, and it ships with the version.

Named after its path

_functions/api/hello.js answers /api/hello; api/users/[id].js answers /api/users/42. Export onRequestGet, onRequestPost and so on, or onRequest for every method. A file starting with _ is a helper the routes import.

Keys are set, never published

Each key is a variable on the site, set in its Functions tab, with drop2run set env, or by an agent through the MCP server. A value is encrypted when saved, never shown again, and a change reaches every route in about a minute without publishing again.

Versioned with the pages

Functions publish in the same version as the files beside them, so rolling back brings back that version's functions too. Your code itself is never served — /_functions/api/hello.js answers 404.

npm packages, bundled for you

npx drop2run deploy bundles each route with what it imports, npm packages and TypeScript included — run npm install first. A folder dropped in the browser is published as it is, so imports between your own files work there and package imports do not.

Proxy or function?

If the API is already yours, you may not need code.

  • _redirects

    The API proxy, from Starter up

    Forwards /api/* to an API you already run and attaches its key on the way. One line in _redirects, no code to write or maintain — the right answer when all the browser lacks is the key. How it works →

  • _functions/

    A function, on Pro and Team

    Code you write: receive a webhook, change the request before it goes on, call two APIs and combine the answers, decide what to send back. The right answer when the work is more than forwarding.

Before you start

Small routes, not a server.

Better to know now than after the first one is written.

  • ✕No database beside it, and nothing kept between calls. A function can call a hosted database over its HTTP API with a key from env; it cannot keep data of its own.
  • ✕Nothing on a schedule. A function runs when a request arrives; nothing wakes it on a clock, and there is no job queue to hand work to.
  • ✕No Node built-ins, and a short CPU budget per call. Web APIs — fetch, Request, crypto.subtle — are there; node:fs is not. Time spent waiting on fetch does not count against the budget.
  • ✕A webhook cannot reach a site behind a password or invite-only. Those checks come first, so put routes that receive webhooks on a public site. Version preview links serve the pages and not the functions.
Limits

Counted by the call, per account.

Calls are counted across every site of the account. Past the month's allowance every function answers 429 until the month turns — the pages keep serving. A plan also bounds how many sites may run functions at once. Every tier, side by side →

  • ✓Pro — 150,000 calls a month, on up to 3 sites
  • ✓Team — 450,000 calls a month, +150,000 for each seat, on up to 10 sites

Start with one file.

Add _functions/api/hello.js to the folder, publish, and call it from the page. It answers on your site's own address.