How to Deploy a Python Serverless Function to Vercel | by Mahbub Zaman | Towards Data Science

Vercel is a platform that provides serverless runtimes, also known as function as a service (FaaS). But why do I need to go serverless? Well, there are no straight forward answers if you need to go serverless or not. Generally, serverless functions are scalable with no maintenance. Moreover, you’re only running the function when you need them.
I’ve been using serverless functions as API endpoints. I’m intrigued by the characteristics of serverless functions. For example, I don’t have to worry about the infrastructure; instead, I can focus on the problem. From this post, you’ll learn how to deploy a python serverless function to Vercel. Let’s get started!
In this post, I will be using GitHub and Vercel. First, we will create a git repository so that we can connect it with Vercel.
Here is the link to the repository I’ve created. After you have downloaded the code to your local machine, you can see a python file called index.py inside api folder. According to Vercel’s documentation for Python, if a Python file has a singular HTTP handler variable, inheriting from the BaseHTTPRequestHandler class handler within the api directory, Vercel will serve it as a serverless function.
Let’s break down the individual ingredients of the index.py file.
from http.server import BaseHTTPRequestHandlerfrom urllib import parseclass handler(BaseHTTPRequestHandler):    def do_GET(self):        s = self.path        dic = dict(parse.parse_qsl(parse.urlsplit(s).query))        self.send_response(200)        self.send_header('Content-type','text/plain')        self.end_headers()        if "name" in dic:            message = "Hello, " + dic["name"] + "!"        else:            message = "Hello, stranger!"        self.wfile.write(message.encode())        return
For this example, we want to handle the query string. If you look at the documentation, the path instance variable contains the request path. Now let’s parse the path variable into a dictionary for our convenience. Moreover, I’ve set the HTTP status code to 200 and the content type as plain text.
The following line looks for a key called name in the dictionary. If the key is present, the variable message will contain Hello, followed by the name!, otherwise Hello, stranger!. You can read more about advanced Python usage from here. Finally, I’m returning the encoded content of the message, which is utf-8 by default.
Now, let’s go to Vercel so we can import our project.
Once you have clicked Continue, you should see the following dialogue.
In my case, I’m going to use my git repository link, which is https://github.com/lifeparticle/vercel-python. Now click Continue and finally click Deploy.
Now, you should see a dialogue like the one below on your browser. Visit your serveless function by clicking the visit button. In this case, the address for my serverless function is https://vercel-python.lifeparticle.vercel.app/, which is generated by Vercel. We need to add api/file_name at the end of the base URL to access the function. After adding that, we have a URL that looks like https://vercel-python.lifeparticle.vercel.app/api/index.
Now if you visit the link...
For the first function call, we didn’t provide any query string. Hence it’s showing Hello, stranger!. In the second call, the name pineapple is provided.
Since we have linked our GitHub project with Vercel, any changes to the main branch will trigger a production deployment. If you want to choose a different branch as the production branch, follow this documentation.
Now you know how to deploy a python serverless function to Vercel! Welcome to the world of new possibilities. Visit the link below, If you want to deploy a Ruby serverless function to Vercel. Happy coding!