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!');
}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, 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 that exposes an app variable in
app.py, index.py, server.py, or main.py. Here's a FastAPI example:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Hello from Python on Vercel"}Vercel also supports file-based Python functions under /api. For that model,
define a handler class or an app variable in a .py file. For more details,
see Using the Python Runtime with Vercel Functions.
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": "[email protected]"
}
}
}In this case, the function at api/hello.ts would use the custom runtime specified.
For more information, see Community runtimes
Was this helpful?