Gatsby Functions help you build Express-like backends without running servers.

Hello World

JavaScript and Typescript files in src/api/* are mapped to function routes like files in src/pages/* become pages.

For example, the following Function is run when you visit the URL /api/hello-world

A Function file must export a single function that takes two parameters:

Dynamic routing is supported for creating REST-ful APIs and other uses cases

  • /api/users => src/api/users/index.js
  • /api/users/23 => src/api/users/[id].js

Learn more about dynamic routes

Typescript

Functions can be written in JavaScript or Typescript.

Common data formats are automatically parsed

Query strings and common body content types are automatically parsed and available at req.query and req.body

Read more about supported data formats.

Respond to HTTP Methods

Sometimes you want to respond differently to GETs vs. POSTs or only respond to one method.

Environment variables

Site environment variables are used to pass secrets and environment-specific configuration to Functions.