For the complete Netlify documentation index, see llms.txt. Markdown versions of this page are available by appending .md to the URL. This page walks you through creating your first Netlify function. For the conceptual overview of how functions work, see Functions overview.
Prepare project
Section titled “Prepare project”Add the @netlify/functions module to your project. It exports the types you need to write type-safe functions.
npm install @netlify/functionsYou don’t need any additional tooling. If you want to extend TypeScript’s base configuration, you can provide your own tsconfig.json. Our build system loads tsconfig.json files from your functions directory, the repository root, or the base directory.
Create your first function
Section titled “Create your first function”Create a TypeScript file in your functions directory, which defaults to netlify/functions/. You can put the file directly in that folder or in a subdirectory; if you use a subdirectory, the entry file must be named index or match the subdirectory name.
For example, any of these would create a function called hello:
netlify/functions/hello.mtsnetlify/functions/hello/hello.mtsnetlify/functions/hello/index.mts
A function file has a default export with a handler that receives a web platform Request and a Netlify-specific Context, and returns a web platform Response.
import type { Config, Context } from "@netlify/functions"
export default async (req: Request, context: Context) => { return new Response("Hello, world!")}
export const config: Config = { path: "/hello",}Once deployed, the function is available at https://<YOUR DOMAIN>/hello. For additional routing configuration options, see Configuration → Routing.
Building with an AI agent
Section titled “Building with an AI agent”If you’d rather describe what your function should do than write the code yourself, an AI coding agent can scaffold the function for you. You can use a local agent like Claude Code or Codex against your project, or use Netlify’s Agent Runners, which run AI coding agents directly on your Netlify project and commit the changes for you.
A prompt as simple as the following will produce a working function:
Add a Netlify function at `/api/joke` that returns a random programming joke as JSON.For more, see the Agent Runners overview and example prompts.
Test locally
Section titled “Test locally”Most modern frameworks include native support for Netlify Functions in their dev server, so you can run and test your function locally without any extra tooling.
If you’re using a Vite-based framework, like Astro, Nuxt, TanStack Start, or React Router, just run your framework’s dev server. The @netlify/vite-plugin emulates the Netlify platform inside the dev server, so functions, edge functions, blobs, environment variables, and other primitives behave the same locally as in production.
Alternatively, the Netlify CLI starts a framework server (if one is detected) and handles redirects, proxy rules, environment variables, and Netlify Functions through a simulated Netlify production environment.
Next steps
Section titled “Next steps”Push your function source files to your Git provider for continuous deployment where Netlify’s build system automatically detects, builds, and deploys your functions. You can also deploy manually with the Netlify CLI or API.
Monitor function logs and metrics in the Netlify UI to observe and help troubleshoot your deployed functions.
Netlify function logs are found in the Netlify UI. You can also stream Netlify function logs to the console with the Netlify CLI.
Prepare project
Section titled “Prepare project”Optionally add the @netlify/functions module to your project. It’s not required for JavaScript functions, but it provides helper exports if you ever opt in.
npm install @netlify/functionsCreate your first function
Section titled “Create your first function”Create a JavaScript file in your functions directory — by default that’s netlify/functions/. You can put the file directly in that folder or in a subdirectory; if you use a subdirectory, the entry file must be named index or match the subdirectory name.
For example, any of these would create a function called hello:
netlify/functions/hello.mjsnetlify/functions/hello/hello.mjsnetlify/functions/hello/index.mjs
A function file has a default export with a handler that receives a web platform Request and a Netlify-specific context, and returns a web platform Response.
export default async (req, context) => { return new Response("Hello, world!")}
export const config = { path: "/hello",}Once deployed, the function is available at https://<YOUR DOMAIN>/hello. For additional routing configuration options, see Configuration → Routing.
Building with an AI agent
Section titled “Building with an AI agent”If you’d rather describe what your function should do than write the code yourself, an AI coding agent can scaffold the function for you. You can use a local agent like Claude Code or Codex against your project, or use Netlify’s Agent Runners, which run AI coding agents directly on your Netlify project and commit the changes for you.
A prompt as simple as the following will produce a working function:
Add a Netlify function at `/api/joke` that returns a random programming joke as JSON.For more, see the Agent Runners overview and example prompts.
Test locally
Section titled “Test locally”Most modern frameworks include native support for Netlify Functions in their dev server, so you can run and test your function locally without any extra tooling.
If you’re using a Vite-based framework, like Astro, Nuxt, TanStack Start, or React Router, just run your framework’s dev server. The @netlify/vite-plugin emulates the Netlify platform inside the dev server, so functions, edge functions, blobs, environment variables, and other primitives behave the same locally as in production.
Alternatively, the Netlify CLI starts a framework server (if one is detected) and handles redirects, proxy rules, environment variables, and Netlify Functions through a simulated Netlify production environment.
Next steps
Section titled “Next steps”Push your function source files to your Git provider for continuous deployment where Netlify’s build system automatically detects, builds, and deploys your functions. You can also deploy manually with the Netlify CLI or API.
Monitor function logs and metrics in the Netlify UI to observe and help troubleshoot your deployed functions.
Netlify function logs are found in the Netlify UI. You can also stream Netlify function logs to the console with the Netlify CLI.
Use the Lambda-compatible API for Go
Section titled “Use the Lambda-compatible API for Go”To write functions in Go, use the Lambda-compatible functions API.
Did you find this doc useful?
Your feedback helps us improve our docs.