How to Deploy FastAPI App or API For Free On Vercel For Free

Free Python Code - Jan 28 - - Dev Community

Hi πŸ™‚πŸ–
In this post, I will show you how to Deploy the FastAPI App or API Free On Vercel For free

Step 1

Install Vercel cli
from here

Step 2

Login to Vercel
How to login

Create a project

app--
  api.py

main.py
requirements.txt
vercel.json
Enter fullscreen mode Exit fullscreen mode

app.py


from fastapi import FastAPI

app = FastAPI()

@app.get('/')
def home():
    return {'msg': 'Welcome in my api'}
Enter fullscreen mode Exit fullscreen mode

main.py

from dotenv import load_dotenv
import os
import uvicorn

load_dotenv()

PORT = int(os.get('PORT', 8000))
HOST = '0.0.0.0'

if __name__ == '__main__':
    uvicorn.run('app.api:app', host = HOST, port = PORT, reload = True)
Enter fullscreen mode Exit fullscreen mode

requirements.txt

You can generate requirements.txt using pipreqs
https://pypi.org/project/pipreqs/

vercel.json

{
    "builds": [{
        "src": "app/api.py",
        "use": "@vercel/python",
        "config": { "maxLambdaSize": "15mb" }
    }],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "app/api.py"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Step 3

Go to your project directory and open the cmd or terminal

type

vercel .
Enter fullscreen mode Exit fullscreen mode

You will see things like this
Make the same answers

Vercel CLI 33.3.0
❗️  Your Project was either deleted, transferred to a new Team, or you don’t have access to it anymore.
? Set up and deploy β€œ~\OneDrive\Desktop\python new videos\How to Deploy FastAPI App or API For Free On Vercel”? [Y/n] y
? Which scope do you want to deploy to? username
? Link to existing project? [y/N] n
? What’s your project’s name? testapi
? In which directory is your code located? ./
πŸ”—  Linked to username/testapi (created .vercel)
πŸ”  Inspect: https://vercel.com/username/testapi/CdFMgqWYpMmxPi7fiPx9jxvbnSNR [2s]
βœ…  Production: https://testapi-k0lnh2w00-username.vercel.app [2s]
πŸ“  Deployed to production. Run `vercel --prod` to overwrite later (https://vercel.link/2F).
πŸ’‘  To change the domain or build command, go to https://vercel.com/username/testapi/settings
Enter fullscreen mode Exit fullscreen mode

Now you can use your API 😎

Now we're done πŸ€—

Don't forget to like and follow πŸ™‚

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .