DEV Community

Cover image for How to Deploy FastAPI App or API For Free On Vercel For Free
Free Python Code
Free Python Code

Posted on

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

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 ๐Ÿ™‚

Top comments (1)

Collapse
 
timothy102 profile image
Timothy102

Error: Unable to find any supported Python versions.?