Vercel Serverless Functions can be deployed in multiple languages:
File Extensions: .js
, .ts
Default Version: 16.x (or defined)
API Routes with a JavaScript file or a TypeScript file within the pages/api
directory, containing a default exported function, will be served as Serverless Functions.
import { NextApiRequest, NextApiResponse } from 'next';
export default function handler(
request: NextApiRequest,
response: NextApiResponse,
) {
// pages/api/[name].ts -> /api/lee
// request.query.name -> "lee"
const { name } = request.query;
return response.end(`Hello ${name}!`);
}
A Vercel Serverless Function using Node.js.
For more advanced usage of Node.js on the Vercel platform, including information about the request and response objects, extended helpers for Node.js on Vercel, private npm packages, or advanced configuration, see the Node.js runtime documentation.
File Extension: .go
Default Version: Go 1.16 (or defined)
Go files in the api
directory that export a function matching the net/http
Go API will be served as Serverless Functions.
For example, the following would live in api/date.go
:
package handler
import (
"fmt"
"net/http"
"time"
)
func Handler(w http.ResponseWriter, r *http.Request) {
currentTime := time.Now().Format(time.RFC850)
fmt.Fprintf(w, currentTime)
}
An example Go function that returns the current date.
When deployed, the example function above will be served as a Serverless Function, returning the latest date.
For more advanced usage of Go on the Vercel platform, including dependencies, build configuration, private dependencies, or advanced configuration, see the Go runtime documentation.
File Extension: .py
Default Version: Python 3.9
Python files within the api
directory, containing an handler variable that inherits from the BaseHTTPRequestHandler
class or an app
variable that exposes a WSGI or ASGI application, will be served as Serverless Functions.
For example, the following would live in api/date.py
:
from http.server import BaseHTTPRequestHandler
from datetime import datetime
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')).encode())
return
An example Python function that returns the current date.
When deployed, the example function above will be served as a Serverless Function, returning the current date and time.
For more advanced usage of Python on the Vercel platform, including dependencies, WSGI applications, ASGI applications, or advanced configuration, see the Python runtime documentation.
File Extension: .rb
Default Version: Ruby 2.7.x
Ruby files that define a singular HTTP handler, within the api
directory, will be served as Serverless Functions.
For example, the following would live in api/date.rb
:
Handler = Proc.new do |request, response|
response.status = 200
response['Content-Type'] = 'text/text; charset=utf-8'
response.body = "Current Time: #{Time.new}"
end
An example Ruby function that returns the current date.
When deployed, the example function above will be served as a Serverless Function, returning the current date and time. See it live with the following link: https://ruby-date.vercel.app/api/date
For more advanced usage of Ruby on the Vercel platform, including dependencies, or Rack interfaces, see the Ruby runtime documentation.
For more information on what to do next, we recommend the following articles: