ZEIT Now deployments support various backend languages that you can deploy as Serverless functions.

Deploying Serverless Functions

To deploy serverless functions without any additional configuration, you can put files with extensions matching supported languages and exported functions in the api directory at your project's root.

Then, using Now CLI, run the following command to deploy from your terminal:

now

Deploying a project with Now CLI in one command.

Example Node.js API

To deploy a serverless Node.js API, provide a main export function like the following, in a .js file within the api directory:

module.exports = (request, response) => {
  const { name = 'World' } = request.query
  response.send(`Hello ${name}!`)
}

An example Node.js serverless function.

Then, all you need to do, to have a live API, is deploy with Now CLI from your terminal:

now

One-command deployment with Now CLI.

The resulting deployment will contain your Node.js endpoint as a serverless function and will provide you with a URL like the following, with your API ready to go: https://node-api.now-examples.now.sh/api/name?name=all

More Examples and Supported Languages

For all supported languages, see the Supported Languages for Severless Functions documentation.

More examples of applications you can deploy paired with serverless functions can be found in the ZEIT Now examples repository on GitHub.

You can use any of the ZEIT Now examples by using the following command from Now CLI in your terminal, then choosing an application to start with:

now init

Initializing a project with Now CLI.

Path Segments

Deploying serverless functions with ZEIT Now gives you the ability to use path segments through file names instead of a complex routes file.

Creating a file using any of the supported languages in the api directory and wrapping the filename in square brackets, such as [name].js will provide you with a file that takes a path segment and gives it to the function when requested with a value.

For example, creating a name directory (within /api) with a [name].js file will allow you to recieve the path segment entered when accessing /api/name/your-name.

The following Node.js example code could then use the path segment in its functionality when requested:

module.exports = (req, res) => {
  const {
    query: { name }
  } = req

  res.send(`Hello ${name}!`)
}

An example of a Node.js serverless function that takes a name path segement and returns a string using it.

The resulting deployment can be found here: https://path-segment-with-node.now-examples.now.sh/api/name/world

Note: Any supported language can utilize path segments.

Local Development

Now provides an additional command with Now CLI to help you develop serverless functions locally by replicating the production environment on Now with your localhost.

If you have an api directory like the above examples on this page, you can run the following command to start a local development environment that supports your serverless API and allows you to develop locally:

now dev

starting a local development environment using Now CLI, for serverless functions.

Note: The now dev command is still in beta. If you have any feedback, please let us know.

If you have a statically generated frontend that supports its own development environment, such as Next.js, we recommend creating a dev script within a package.json file at the root of your project that now dev can use and extend:

{
  ...
  "scripts": {
    "dev": "next"
  }
}

Defining a custom dev script for Next.js in a package.json file.

Advanced Usage

For an advanced configuration and structure, you can use a now.json file to set up Builds to configure your own structure and behaviour.

We do, however, recommend sticking to the api directory to keep things simple for your project.

Related

For more information on what to do next, we recommend the following articles: