Skip to content

Search docs

Jump between documentation pages.

Browse docs

Netlify

Netlify gives you two runtime options for an API: Edge Functions (Deno-based, global) and Functions v2 (Node, fetch-style). Both call your default export as (request, context) => Response, so the DaloyJS adapter is the same helper in both places. Only the file location and the optional config export change.

One app, two Netlify targets
identical appYour DaloyJS AppRequest -> Response
deno · globalEdge Functionsnetlify/edge-functions/ · toNetlifyHandler(app)
node · recommendedFunctions v2netlify/functions/*.mts · toNetlifyHandler(app)
legacy v1Lambda-shape handlertoLambdaHandler(app)
Edge and Functions v2 both speak (request, context) -> Response, so they share toNetlifyHandler. Only the file location and config export change. The v1 lambda shape is legacy and only for codebases that cannot move to v2.

When to choose Netlify

  • You want a platform-managed REST API with Edge or Node function options.
  • You want preview deploys per branch with zero config.
  • You're fine with platform-managed cold starts.

What the adapter adds

toNetlifyHandler(app) from @daloyjs/core/netlify (since 1.16.0) is a thin wrapper over app.fetch, but it reads two things from Netlify's context that a generic fetch handler cannot see:

  • context.ip is the client address Netlify's edge observed on the TCP connection. The adapter attaches it as the request's connection info, so behindProxy: "none" (the right posture when Netlify is the only thing in front of you) resolves the real client for rateLimit(), autoBan(), ipRestriction(), and audit logs instead of putting every caller in one shared bucket. It is never read from a spoofable header, and a value that is not an IP literal is ignored, not trusted.
  • context.waitUntil keeps the invocation alive until the OTLP telemetry flush finishes, instead of the flush being dropped when the function returns.

If you put a CDN in front of Netlify, declare it with behindProxy: { cidrs: [<CDN egress ranges>] }, not { hops: 1 }. Only the cidrs form checks that the socket peer — context.ip here — is one of your proxies before trusting a forwarded header; hops reads the header whenever it is present. Because a Netlify site stays reachable on its *.netlify.app origin behind any CDN, a direct caller could otherwise spoof X-Forwarded-For and rotate their rate-limit or ban identity, which is exactly what context.ip was added to prevent.

The adapter has no dependency on @netlify/functions or @netlify/edge-functions; the NetlifyContext type it accepts is structurally compatible with both.

Netlify Edge Functions (Deno)

Edge Functions run on a Deno-based runtime. Use toNetlifyHandler; the input/output shape is identical to Functions v2.

ts
// netlify/edge-functions/api.ts
import { toNetlifyHandler } from "@daloyjs/core/netlify";
import type { Config } from "@netlify/edge-functions";
import { app } from "../../src/server.ts";

export default toNetlifyHandler(app);

export const config: Config = {
  path: "/api/*",
};

Netlify Functions v2 (Node, fetch-style)

Functions v2 is GA and is the recommended way to write Node functions on Netlify. The old v1 lambda-style handler (exports.handler = (event, context) => ...) is legacy. It still works for compatibility but you should write new code in v2.

ts
// netlify/functions/api.mts
import { toNetlifyHandler } from "@daloyjs/core/netlify";
import type { Config } from "@netlify/functions";
import { app } from "../../src/server.js";

export default toNetlifyHandler(app);

export const config: Config = {
  path: "/api/*",
};

toWebHandler(app) from @daloyjs/core/vercel still works on Netlify if you only need Request -> Response, but it cannot see context.ip, so every client lands in one rate-limit bucket unless you declare a proxy posture.

If you have an existing v1 codebase you can't migrate yet, the Lambda adapter still works against the v1 event shape:

ts
// netlify/functions/api.ts (legacy v1, only if you can't move to v2)
import { toLambdaHandler } from "@daloyjs/core/lambda";
import { app } from "../../src/server.js";

export const handler = toLambdaHandler(app);

netlify.toml

For a REST-API-only project there is no static site to publish. Omit the publish key (or point it at an empty directory) so Netlify doesn't try to serve your compiled server code as static files.

toml
[build]
  command = "pnpm build"

[functions]
  node_bundler = "esbuild"

# only needed if you don't use the config export above
[[edge_functions]]
  function = "api"
  path = "/api/*"

Deploy

bash
pnpm netlify dev
pnpm netlify deploy --build
pnpm netlify deploy --build --prod

Gotchas

  • Edge Functions don't expose node:*, the same caveat as Vercel and Cloudflare Workers.
  • Functions v2 returns a Response directly. Don't use the v1 statusCode/body object shape in v2 code.
  • context.waitUntil is available in v2 for fire-and-forget work; the adapter already uses it for the telemetry flush.

See also

Edit this page on GitHub

Last updated