Skip to content
Back to Cloud Services

Cloudflare Pages Functions Full-Stack

Add API routes to Pages sites via functions/ directory — file-based routing with edge bindings.

CloudflareIntermediate

Why

Pages Functions turn a static site into a full-stack app without leaving Pages — same deploy, same preview URLs, same bindings (KV/D1/R2). No separate Worker to manage.

Cloudflare's network spans 330+ cities in 125+ countries, serving 20% of all internet traffic. Workers have 0ms cold starts (isolate-based) and the free tier covers 100k requests/day.

When

Use when your frontend and API belong together (Next.js, Astro, Remix). Avoid for APIs shared across multiple frontends (use a standalone Worker).

How

1. Create functions/ at repo root — functions/api/users.ts → route /api/users 2. Methods — export onRequestGet, onRequestPost, onRequestPut, onRequestDelete 3. Catch-all — functions/api/[id].ts → /api/:id, access via context.params.id 4. Middleware — functions/_middleware.ts runs before all routes in that dir 5. Bindings — wrangler.toml [[d1_databases]] etc., available via context.env 6. Static assets — _routes.json controls which paths invoke Functions vs static 7. Deploy — git push triggers build + Functions deploy together

Example

// functions/api/feedback.ts — POST creates, GET lists export const onRequestGet = async ({ env }) => { const { results } = await env.DB.prepare("SELECT * FROM feedback ORDER BY created_at DESC").all(); return Response.json(results); }; export const onRequestPost = async ({ request, env }) => { const { message, email } = await request.json(); await env.DB.prepare("INSERT INTO feedback (message, email) VALUES (?, ?)").bind(message, email).run(); return Response.json({ ok: true }, { status: 201 }); }; // functions/_middleware.ts — auth check for all /api routes export const onRequest = async (context) => { const token = context.request.headers.get("Authorization"); if (!token) return new Response("Unauthorized", { status: 401 }); return context.next(); };

Common Problems

  • ⚠functions/ in wrong location — must be repo root, not inside src/ or public/
  • ⚠Dynamic routes syntax — [id].ts not :id.ts, params accessed via context.params
  • ⚠Static assets invoking Functions — use _routes.json exclude to skip /assets/*

Best Practices

  • ✓Use _routes.json to limit Function invocations — skip static paths, save CPU costs
  • ✓Co-locate middleware — functions/api/_middleware.ts scopes auth to /api only
  • ✓Type context with @cloudflare/workers-types — autocomplete for env, params, next

FAQ

What is Cloudflare Pages Functions Full-Stack and what problem does it solve?

Add API routes to Pages sites via functions/ directory — file-based routing with edge bindings. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with Cloudflare Pages Functions Full-Stack in 5 minutes?

Two steps to run it: (1) Create functions/ at repo root — functions/api/users.ts → route /api/users; (2) Methods — export onRequestGet, onRequestPost, onRequestPut, onRequestDelete. Validate on the free tier first, then wire into production.

What is the #1 pitfall with Cloudflare Pages Functions Full-Stack?

The most frequent issue is: functions/ in wrong location — must be repo root, not inside src/ or public/. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for Cloudflare Pages Functions Full-Stack?

Best practices for Cloudflare Pages Functions Full-Stack include: Use _routes.json to limit Function invocations — skip static paths, save CPU costs; Co-locate middleware — functions/api/_middleware.ts scopes auth to /api only; Type context with @cloudflare/workers-types — autocomplete for env, params, next. The first one — "Use _routes.json to limit Function invocations — skip static paths, save CPU costs" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with Cloudflare Pages Functions Full-Stack?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most Cloudflare Pages Functions Full-Stack options offer a free tier — validate within free quota before scaling.