# Netlify Functions

Serverless function discipline on Netlify.

## Rules

1. **Functions live in `netlify/functions/`.** One file per function, exported handler. Don't bury API logic in framework middleware unless the framework owns the deploy.

2. **Match runtime to workload.** Default Node for npm ecosystem. Edge functions for geo-routing and low-latency transforms. Pick deliberately.

3. **Environment variables per context.** Production, deploy preview, and branch deploys get scoped env vars. Secrets in the Netlify UI, not the repo.

4. **Cold starts are real.** Keep handlers lean. Heavy imports and synchronous setup at module scope slow first invocation.

5. **Return proper HTTP responses.** Status codes, JSON content-type, CORS headers where needed. Functions are APIs — treat them like `APIEndpointDesign` says.

## What This Replaces

Monolithic server code on a JAMstack host, missing CORS on functions, and cold-start latency from bloated handlers.

## Official Source

Distilled from Netlify's official agent skills.
Full upstream: https://officialskills.sh/netlify/skills/netlify-functions
