Configuring the Runtime for Vercel Functions
The runtime of your function determines the environment in which your function will execute. Vercel supports various runtimes including Node.js, 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 Vercel Function on the Node.js runtime.
export function GET(request: Request) {
return new Response('Hello from Vercel!');
}export function GET(request) {
return new Response('Hello from Vercel!');
}export function GET(request: Request) {
return new Response('Hello from Vercel!');
}export function GET(request) {
return new Response('Hello from Vercel!');
}For Go, write a server in main.go, cmd/api/main.go, or
cmd/server/main.go. The server must listen on the PORT environment
variable:
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello from Go on Vercel")
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}Vercel also supports file-based Go functions under /api. For that model,
export an http.HandlerFunc from a .go file. For more details, see Using
the Go Runtime with Vercel Functions.
For Python, write an ASGI (Asynchronous Server Gateway Interface) or WSGI (Web Server Gateway Interface) application. Vercel includes framework presets for FastAPI, Flask, and Django, and loads your app from a supported Python entrypoint. Here's a FastAPI example:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Hello from Python on Vercel"}For existing projects that use file-based Python functions under /api, see
Python functions in the /api
directory.
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:
Handlerproc that matches thedo |request, response|signatureHandlerclass that inherits from theWEBrick::HTTPServlet::AbstractServletclass
For example:
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')
endDon't forget to define your dependencies inside a 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:
{
"$schema": "https://openapi.vercel.sh/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
Was this helpful?