I found out that I could use Python to create a serverless function inside a Next.js project. Once deployed to Vercel, it will get converted into a serverless function.
I went through the docs and found a simple example that outputs the date:
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
They offer a live working example here.
Apparently all that is needed is to place the file date.py
inside the api
folder of a bootstrapped Next.js project and you're off to the races. When deployed, Vercel will detect the Python file and serve it as a serverless function.
The deploy succeeded and I placed the file inside the pages/api
folder as required. However, the function is never picked up (image below):
Older versions apparently required the configuration of serverless functions by adding a vercel.json
file. But this doesn't seem necessary now.
What am I missing?