Within the /api
directory of your projects, Vercel will automatically recognize the languages listed on this page, through their file extensions, and serve them as Serverless Function.
/pages/api
for Node.js functions and use/api
for all other languages.Supported Languages:
Node.js
File Extensions: .js
, .ts
Default Version: 12.x (or defined)
Node.js files through a JavaScript file or a TypeScript file within the api
directory, containing a default exported function, will be served as Serverless Functions.
For example, the following would live in api/hello.js
:
module.exports = (req, res) => { const { name = 'World' } = req.query res.status(200).send(`Hello ${name}!`) }
Example deployment: https://node-api.now-examples.now.sh/api/hello?name=reader
Using TypeScript
Deploying a Node.js function with the .ts
extension will automatically be recognized as a TypeScript file and compiled to a Serverless Function.
As an example; a file called hello.ts
in the api
directory, and importing types for the Vercel platform Request and Response objects from the @vercel/node
module:
import { NowRequest, NowResponse } from '@vercel/node' export default (request: NowRequest, response: NowResponse) => { const { name = 'World' } = request.query response.status(200).send(`Hello ${name}!`) }
Example deployment: https://ts-api.now-examples.now.sh/api/hello?name=reader
You can install the @vercel/node
module for type definitions through npm:
npm i -D @vercel/node
You can also define a tsconfig.json
to configure the Vercel TypeScript compiler.
Advanced Node.js Usage
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.
Go
File Extension: .go
Default Version: Go 1.x
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) }
When deployed, the example function above will be served as a Serverless Function, returning the latest date. See it live with the following link: https://go-api.now-examples.now.sh/api/date
Advanced Go Usage
For more advanced usage of Go on the Vercel platform, including dependencies, build configuration, private dependencies, or advanced configuration, see the Go runtime documentation.
Python
File Extension: .py
Default Version: Python 3.6
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
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://python-api.now-examples.now.sh/api/date
Advanced Python Usage
For more advanced usage of Python on the Vercel platform, including dependencies, WSGI applications, ASGI applications, or advanced configuration, see the Python runtime documentation.
Ruby
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 |req, res| res.status = 200 res['Content-Type'] = 'text/text; charset=utf-8' res.body = "Current Time: #{Time.new}" end
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.now.sh/api/date
Advanced Ruby Usage
For more advanced usage of Ruby on the Vercel platform, including dependencies, or Rack interfaces, see the Ruby runtime documentation.
Related
For more information on what to do next, we recommend the following articles: