Skip to content
App
Markdown

Server functions

A file-routed server/ directory beside your build output — forms and APIs with zero client JavaScript.

A static site that cannot take a form submission is a brochure. Server functions are the platform's native primitive for server behavior: a file-routed server/ directory beside your build output.

my-site/
├── dist/                  # your build output (assets)
├── server/
│   └── api/
│       ├── contact.ts     # answers /api/contact
│       └── orders/
│           └── [id].ts    # answers /api/orders/:id
└── pilvia.app.json
{
  "name": "my-site",
  "auth": "public",
  "assets": "dist",
  "server": "server"
}

Handlers

A file exports HTTP verb handlers:

// server/api/contact.ts
export async function POST(request: Request, env, ctx) {
  const form = await request.formData();
  // …
  return Response.redirect('/thanks/', 303);
}
  • GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD are recognized exports; a verb with no handler gets a 405. A HEAD with no HEAD export is answered by GET.
  • The platform never sends a body in answer to HEAD. Return the same response as for GET and the HEAD carries its Content-Length. A HEAD answered with no body is sent with no Content-Length, and so is a 304 or a 204, unless your response sets one itself.
  • ctx.params carries values from [param].ts path segments.
  • ctx.waitUntil(promise) finishes work after the response, within the request's time limit (see work after the response).
  • export default { fetch(request, env, ctx) } is the per-file escape hatch when you want one handler for everything.

The handler runs in an isolate created for that request and discarded after it — see Releases and isolates. This means a plain

<form method="post" action="/api/contact">

works with zero client JavaScript: the asset half of your site stays fully static, and the form still lands in your code.

Routes are derived, never hand-written

At deploy time the CLI walks the server/ tree, bundles it behind a generated router, and derives the release's route list from the file paths. You cannot get the list wrong because you never write it. The declared routes are also what protects a "spa" fallback from swallowing /api/* — requests resolve asset → declared route → fallback, in that order.

Route patterns match precisely:

  • /api/orders/:id (from [id].ts) matches exactly one path segment — /api/orders/7, but neither /api/orders nor /api/orders/7/items.
  • Catch-all files ([...rest].ts) are not supported: name the segments the handler takes.
  • Trailing slashes are equivalent — a form action with one still finds its handler.

What a handler can reach

Nothing, unless declared — the fail-closed rule applies to functions exactly as to full apps:

  • Who is signed in: on an auth: required app, env.user is the authenticated user, constructed by the host before your code runs. See Authentication.
  • Outbound network: absent egress means no network at all. Name what you reach: "egress": { "allow": ["api.eu.mailgun.net"] }.
  • Request bodies: capped at 1 MiB by default; raise max_body_mb explicitly (up to 64) if you take uploads.
  • Configuration and secrets: none arrive at runtime. See below.

Configuration and secrets

Server code has no environment variables, and it cannot read a value set with pilvia app secret. Those secrets are the app's own platform credentials, such as the trigger token of an auth: token app.

A key your handler needs, such as a mail provider's API key, has to be part of the code you deploy:

  • With Astro, compile it in from the build machine's environment.
  • In a plain server/ tree, import it from a file you keep out of version control.

Either way the value is stored with the release, so build and deploy from a machine you trust with it. A GitHub build has no variables of its own, so a site that needs a key deploys from the CLI.

One directory or one framework

server/ and a framework's server build ("build", e.g. the Astro adapter) are exclusive: a release has one server entry. Functions are for sites that need a handful of endpoints; a framework that renders on demand brings its own.

Type to search. ↑↓ to move, Enter to open, Esc to close.