Serverless Functions Development for Websites (Netlify Functions)
Netlify Functions are serverless functions running on AWS Lambda under the hood, but deployed automatically with your site. Setup takes minutes: create a file in netlify/functions/, deploy — the function works.
Two Types of Functions
Sync Functions — standard request/response. Support Node.js 18+, Go, Rust. Timeout 10 seconds (free) / 26 seconds (Pro).
Background Functions — for long-running tasks (up to 15 minutes). Return 202 Accepted immediately, execution continues in the background. Filename: *.mts or *-background.ts.
Sync Function Example
File netlify/functions/contact.ts:
import type { Handler, HandlerEvent } from "@netlify/functions";
export const handler: Handler = async (event: HandlerEvent) => {
if (event.httpMethod !== "POST") {
return { statusCode: 405, body: "Method Not Allowed" };
}
const data = JSON.parse(event.body || "{}");
const { name, email, message } = data;
// Send via Netlify Email Integration or external API
await fetch("https://api.sendgrid.com/v3/mail/send", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
personalizations: [{ to: [{ email: "[email protected]" }] }],
from: { email: "[email protected]" },
subject: `Message from ${name}`,
content: [{ type: "text/plain", value: `${name} (${email}): ${message}` }],
}),
});
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ok: true }),
};
};
The function is available at /.netlify/functions/contact or via redirect in netlify.toml:
[[redirects]]
from = "/api/contact"
to = "/.netlify/functions/contact"
status = 200
Background Function for Reports
// netlify/functions/generate-report-background.ts
import type { BackgroundHandler } from "@netlify/functions";
export const handler: BackgroundHandler = async (event) => {
const { reportId } = JSON.parse(event.body || "{}");
// Long operation: generate PDF, process data
const pdf = await generatePDFReport(reportId);
// Save to S3 and notify user
await uploadToS3(pdf, `reports/${reportId}.pdf`);
await notifyUser(reportId);
};
netlify.toml
[build]
command = "npm run build"
publish = "dist"
functions = "netlify/functions"
[functions]
node_bundler = "esbuild"
included_files = ["templates/**"]
[dev]
command = "npm run dev"
port = 8888
node_bundler = "esbuild" speeds up builds and reduces bundle size compared to zip archives.
Environment Variables
Netlify Dashboard → Site Settings → Environment variables. For local development — .env file:
SENDGRID_API_KEY=SG.xxx
DATABASE_URL=postgresql://...
Netlify CLI picks up .env when running netlify dev.
Netlify Forms as Alternative
For simple contact forms, Netlify offers built-in handling without writing a function:
<form name="contact" method="POST" data-netlify="true">
<input type="hidden" name="form-name" value="contact" />
<!-- form fields -->
</form>
Netlify automatically accepts submissions, stores them in the dashboard, and sends notifications. Free up to 100 submissions per month.
Timeframe
Basic functions (form, webhook) with redirect configuration — 1–2 days.







