For the complete Netlify documentation index, see llms.txt. Markdown versions of any documentation page are available by appending .md to its URL. This page provides a full reference of the Netlify Serverless Functions API.
Function signature
Section titled “Function signature”A function file exports a default handler that runs in response to an event. The signature depends on the type of event the function responds to.
Web requests
Section titled “Web requests”A function that responds to web requests receives a Request representing the incoming request and a Context object with Netlify-specific metadata. The handler returns a Response.
Two equivalent forms are supported:
import type { Context } from "@netlify/functions"
export default async (req: Request, context: Context) => { return new Response("Hello, world!")}import type { NetlifyFunction } from "@netlify/functions"
export default { fetch: (req, context) => { return new Response("Hello, world!") },} satisfies NetlifyFunctionThe second form is a Fetchable module, a common shape for fetch-style handlers across JavaScript frameworks and runtimes.
Background mode
Section titled “Background mode”When config.background is set to true, the platform sends a 202 response to the client immediately and the function continues running in the background, up to the background execution limit. The handler's return value is discarded.
For more details, see Background Functions.
Streaming responses
Section titled “Streaming responses”A function can stream data to clients as it becomes available, instead of returning a buffered payload after the function finishes. To stream a response, return a ReadableStream as the body of the Response.
Streaming functions have a 60-second execution limit and a 20 MB response size limit.
Emit a list of timestamped items over the course of several seconds:
export default async () => { const encoder = new TextEncoder() const formatter = new Intl.DateTimeFormat("en", { timeStyle: "medium" }) const body = new ReadableStream({ start(controller) { controller.enqueue(encoder.encode("<html><body><ol>")) let i = 0 const timer = setInterval(() => { controller.enqueue(encoder.encode(`<li>Hello at ${formatter.format(new Date())}</li>\n\n`)) if (i++ >= 5) { controller.enqueue(encoder.encode("</ol></body></html>")) controller.close() clearInterval(timer) } }, 1000) }, })
return new Response(body)}