Configuring the Runtime for Vercel Functions

Learn how to configure the runtime for Vercel Functions.
Table of Contents

The runtime of your function determines the environment in which your function will execute. Vercel supports various runtimes including Node.js, Edge, Python, Ruby, and Go. You can also configure other runtimes using the vercel.json file. Here's how to set up each:

By default, a function with no additional configuration will be deployed as a Serverless Function on the Node.js runtime. To explicitly set it, add the following highlighted code to your function:

Next.js (/app)
Next.js (/pages)
Other frameworks
app/api/my-function/route.ts
export const runtime = 'nodejs';
 
export function GET(request: Request) {
  return new Response(`I am a Serverless Function`, {
    status: 200,
  });
}

If you're not using a framework, you must either add "type": "module" to your package.json or change your JavaScript Functions' file extensions from .js to .mjs

To use the Edge runtime, add the following code to your function:

Next.js (/app)
Next.js (/pages)
Other frameworks
app/api/my-function/route.ts
export const runtime = 'edge'; // 'nodejs' is the default
 
export function GET(request: Request) {
  return new Response(`I am an Edge Function!`, {
    status: 200,
  });
}

If you're not using a framework, you must either add "type": "module" to your package.json or change your JavaScript Functions' file extensions from .js to .mjs

For Go, expose a single HTTP handler from a .go file within an /api directory at your project's root. For example:

/api/index.go
package handler
 
import (
  "fmt"
  "net/http"
)
 
func Handler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "<h1>Hello from Go!</h1>")
}

For Python, create a function by adding the following code to api/index.py:

api/index.py
from http.server import BaseHTTPRequestHandler
 
class handler(BaseHTTPRequestHandler):
 
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/plain')
        self.end_headers()
        self.wfile.write('Hello, world!'.encode('utf-8'))
        return

For Ruby, define an HTTP handler from .rb files within an /api directory at your project's root. Ruby files must have one of the following variables defined:

  • Handler proc that matches the do |request, response| signature
  • Handler class that inherits from the WEBrick::HTTPServlet::AbstractServlet class

For example:

api/index.rb
require 'cowsay'
 
Handler = Proc.new do |request, response|
  name = request.query['name'] || 'World'
 
  response.status = 200
  response['Content-Type'] = 'text/text; charset=utf-8'
  response.body = Cowsay.say("Hello #{name}", 'cow')
end

Don't forget to define your dependencies inside a Gemfile:

Gemfile
source "https://rubygems.org"
 
gem "cowsay", "~> 0.3.0"

You can configure other runtimes by using the functions property in your vercel.json file. For example:

vercel.json
{
  "functions": {
    "api/test.php": {
      "runtime": "vercel-php@0.5.2"
    }
  }
}

In this case, the function at api/hello.ts would use the custom runtime specified.

For more information, see Community runtimes

Last updated on May 29, 2024