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/functionspackage 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
Responseobjects for consistent behavior - Use background functions for any task that may exceed 10 seconds
Do Not:
- Use the legacy
exports.handlersyntax 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
More Skills You Might Like
Explore similar skills to enhance your workflow
Netlify AI Gateway
Guide for using Netlify AI Gateway to access AI models. Use when adding AI capabilities or selecting/changing AI models. Must be read before
Bats Testing Patterns
- Implementing test-driven development (TDD) for scripts
Terraform Module Library
Production-ready Terraform module patterns for AWS, Azure, GCP, and OCI infrastructure
App Router
This skill should be used when the user asks to "create a Next.js route", "add a page", "set up layouts", "implement loading states", "add error bound
Canva Automation
Automate Canva tasks via Rube MCP (Composio): designs, exports, folders, brand templates, autofill. Always search tools first for current schemas
Opportunity Solution Tree
Build an Opportunity Solution Tree from outcomes to opportunities, solutions, and tests. Use when a stakeholder request needs problem framing