How to test Netlify functions locally

How to test Netlify functions locally

Perttu Lähteenlahti
4 min read
netlify-functionslocal-developmentserverlesstesting
Share:

Why testing Netlify Functions locally really matters

Netlify Functions are one of the most useful features on Netlify. They let you run backend logic without setting up servers, and they work great for small APIs, utilities, and background tasks.

The problem is that testing changes directly in production can be stressful. If a function is already live and doing something important, like handling signups or payments, a small mistake can break real user flows.

That is why testing Netlify Functions locally is so important. It lets you make changes, try things out, and be confident before you deploy anything to your live Netlify site.

What Netlify Functions are, briefly

A Netlify Function is just a file that exports a handler. Netlify maps that handler to a URL automatically.

Once deployed, the function runs whenever someone visits its URL or sends a request to it. This makes them perfect for small backend tasks that support a static site.

Before deploying, though, you want a safe way to run that same code on your own machine.

Step 1: Install the Netlify CLI

To test Netlify Functions locally, you need the Netlify CLI.

Install it globally using npm:

npm install -g netlify-cli

After installation, you can check that it works by running:

netlify --version

If you see a version number, you are ready to continue.

Step 2: Serve Netlify Functions locally

Next, open a terminal and navigate to your project folder.

From there, run:

netlify functions:serve

This command starts a local server that runs your Netlify Functions exactly like Netlify does in production.

By default, the functions are available on port 9999.

Accessing a local Netlify Function

Once the local server is running, you can access a function using this URL pattern:

http://localhost:9999/.netlify/functions/<function-name>

For example, if you have a function called signup.ts, you can visit:

http://localhost:9999/.netlify/functions/signup

This works for GET requests in the browser and for POST requests using tools like Insomnia, Postman, or curl.

Example Netlify Function in TypeScript

Here is a simple Netlify Function written in TypeScript that you can test locally:

import { Handler } from "@netlify/functions"

export const handler: Handler = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      method: event.httpMethod,
      message: "Hello from local Netlify Functions"
    })
  }
}

When you call this locally, you will get the same response you would get after deploying to Netlify.

Testing POST requests locally

Many Netlify Functions handle POST requests. To test those locally, a browser is often not enough.

Tools like Insomnia or Postman are very useful here. They let you:

  • Send POST requests
  • Add headers
  • Include JSON bodies
  • Inspect responses easily

This makes it much easier to test real world scenarios before deploying.

Environment variables work locally too

One very useful detail is that local Netlify Functions can access Netlify environment variables.

If you have environment variables set in the Netlify dashboard, the CLI can load them locally so your function behaves the same way as it does in production.

This means fewer surprises after deployment and more confidence in your changes.

Why this makes deployments less risky

Testing Netlify Functions locally gives you a safe feedback loop.

You can:

  • Make changes without affecting users
  • Test edge cases
  • Verify request handling
  • Catch errors early

When you finally deploy, you are much more confident that everything will work as expected.

Conclusion

Testing Netlify Functions locally is one of the best habits you can build when working with Netlify.

To recap:

  • Install the Netlify CLI
  • Run netlify functions:serve
  • Access your functions at localhost:9999
  • Test GET and POST requests safely
  • Use the same environment variables as production

With this setup, developing Netlify Functions becomes calmer, safer, and far more enjoyable, especially when your functions power important parts of your site.

Manage Netlify on the go

Download Netli.fyi and monitor your sites, check deploys, and manage your projects from anywhere.

Related articles