Netlify Functions

Guide for writing Netlify serverless functions. Use when creating API endpoints, background processing, scheduled tasks, or any server-side logic

What Is This?

Overview

Netlify Functions is a serverless computing feature built into the Netlify platform that allows developers to run server-side code without managing infrastructure. Functions are deployed alongside your frontend application and automatically scale based on demand. They support modern JavaScript and TypeScript syntax, making them accessible to frontend developers who want to add backend capabilities to their projects.

The platform supports several function types, including synchronous API functions, background functions for long-running tasks, scheduled functions for cron-style automation, and streaming functions for real-time data delivery. Each type follows a consistent modern syntax pattern using default exports and a Config object, which replaces the older legacy handler approach.

Netlify Functions integrates directly with your Git-based deployment workflow. When you push code, your functions are built and deployed automatically alongside your static assets, removing the need for separate backend deployment pipelines or server configuration.

Who Should Use This

  • Frontend developers who need to add API endpoints or server-side logic without managing a separate backend
  • Full-stack developers building JAMstack applications that require dynamic functionality
  • Teams automating workflows with scheduled tasks such as data syncing, report generation, or cache invalidation
  • Developers building webhook receivers or third-party API integrations
  • Engineers who need background processing for tasks that exceed standard HTTP timeout limits
  • TypeScript developers who want type-safe serverless functions with minimal configuration

Why Use It?

Problems It Solves

  • Eliminates the need to provision, configure, or maintain servers for simple API logic
  • Removes the complexity of separate backend deployments by co-locating functions with frontend code
  • Handles long-running operations through background functions, bypassing the 10-second synchronous timeout
  • Simplifies scheduled automation without requiring external cron services or task schedulers
  • Reduces infrastructure costs by charging only for actual function invocations rather than idle server time

Core Highlights

  • Modern default export syntax with Config object for clean, readable function definitions
  • Full TypeScript support with type definitions for request and response objects
  • Path-based routing that maps function files directly to URL endpoints
  • Background functions that support processing times up to 15 minutes
  • Scheduled functions using standard cron expressions for time-based automation
  • Streaming support for delivering real-time or chunked responses to clients
  • Method routing to handle GET, POST, PUT, and DELETE within a single function file
  • Automatic scaling with no cold-start configuration required

How to Use It?

Basic Usage

Always use the modern default export pattern with a Config object. Avoid the legacy exports.handler syntax.

import type { Config, Context } from "@netlify/functions";

export default async (req: Request, context: Context) => {
  const data = { message: "Hello from Netlify Functions" };
  return Response.json(data);
};

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

Specific Scenarios

Scheduled Function for Automated Tasks

import type { Config } from "@netlify/functions";

export default async () => {
  await syncDatabaseRecords();
  console.log("Sync complete");
};

export const config: Config = {
  schedule: "0 6 * * *",
};

Background Function for Long-Running Processing

import type { Config } from "@netlify/functions";

export default async (req: Request) => {
  const body = await req.json();
  await processLargeDataset(body.datasetId);
};

export const config: Config = {
  path: "/api/process",
  background: true,
};

Real-World Examples

A common use case is building a contact form handler that receives POST data, validates input, and sends an email through a third-party service like SendGrid. Another example is a webhook receiver that listens for Stripe payment events and updates a database record accordingly.

When to Use It?

Use Cases

  • Building REST API endpoints for frontend applications
  • Processing form submissions with server-side validation
  • Receiving and responding to third-party webhooks
  • Running scheduled data synchronization or cleanup jobs
  • Generating dynamic content such as OG images or PDFs
  • Proxying requests to external APIs to protect credentials
  • Handling authentication flows including OAuth callbacks

Important Notes

Requirements

  • A Netlify account with a connected Git repository
  • Node.js 18 or later for modern syntax and Web API compatibility
  • The @netlify/functions package installed as a development dependency

Usage Recommendations

Do:

  • Use the default export with Config pattern for all new functions
  • Add TypeScript types for request and context parameters
  • Return standard Response objects for consistent behavior
  • Use background functions for any task that may exceed 10 seconds

Do Not:

  • Use the legacy exports.handler syntax in new projects
  • Store secrets directly in function code or version control
  • Perform blocking synchronous operations that delay response time
  • Ignore error handling, as unhandled exceptions will return 500 errors to clients