Netlify
02 / 02

Functions, Edge Functions & Forms

Functions, Edge Functions & Forms

Netlify Functions

// netlify/functions/hello.js — runs as an AWS Lambda under the hood
exports.handler = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello, ${event.queryStringParameters?.name || 'World'}!` }),
  };
};
// Available at: /.netlify/functions/hello

// Proxying a secret API key server-side — never expose it in client-side JS
exports.handler = async (event) => {
  const res = await fetch('https://api.example.com/data', {
    headers: { Authorization: `Bearer ${process.env.SECRET_API_KEY}` },
  });
  return { statusCode: 200, body: await res.text() };
};

Edge Functions

// netlify/edge-functions/geo-redirect.js — Deno runtime, runs at CDN edge locations
export default async (request, context) => {
  const country = context.geo?.country?.code;
  if (country === 'FR') {
    return Response.redirect(new URL('/fr', request.url), 302);
  }
  return context.next(); // pass through to normal content serving
};

// netlify.toml
// [[edge_functions]]
//   path = "/*"
//   function = "geo-redirect"

// Edge Functions suit fast, latency-sensitive logic (geo redirects, A/B tests,
// auth checks) run before content is served — not heavy compute/DB work,
// which belongs in a regular Function instead.

Forms

<form name="contact" method="POST" data-netlify="true">
  <input type="hidden" name="form-name" value="contact" />
  <label>Email <input type="email" name="email" /></label>
  <label>Message <textarea name="message"></textarea></label>
  <button type="submit">Send</button>
</form>
<!-- Netlify's build step detects this attribute and auto-provisions a
     submission-handling endpoint — no backend code needed. -->

Build Plugins

// netlify/plugins/lighthouse-check/index.js
module.exports = {
  onPostBuild: async ({ utils }) => {
    const score = await runLighthouseAudit();
    if (score < 90) {
      utils.build.failBuild(`Performance score too low: ${score}`);
    }
  },
};

// netlify.toml
// [[plugins]]
//   package = "./netlify/plugins/lighthouse-check"

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free