Make your first Hello World program with Fast API ⚡
This blog will show you how you can create your first hello world program with fast api!!
PermalinkHello Everyone 👋
PermalinkToday I will show you how you can create a fast API program to display hello world!!
PermalinkBut First, what is Fast API?
PermalinkFast API is a software framework for developing web applications in Python. And as its name suggests, this is blazingly fast ⚡. As a web developer, this is a pretty great and needed skill, it can be your entry point to APIs in the backend!!
PermalinkPerquisites
Permalink- Basic knowledge of python
PermalinkDependencies (to install with pip)
Permalink- Fast API
Permalink- Uvicorn (to start the server in localhost)
PermalinkFirst Step:
PermalinkFirst, we will make a directory in the desired folder and cd into that
mkdir fastapi
cd fastapi
PermalinkNow we will make a python file named main.py
touch main.py
PermalinkNow let's install fast api and uvicorn with pip:
pip install fastapi uvicorn
PermalinkNow our file tree should look like this (yep, just a python file):
|
|-main.py
PermalinkNow let's open the directory in your code editor, for me its VS Code
code .
PermalinkLet's import fastapi now
-main.py
from fastapi import FastAPI
app = FastAPI()
PermalinkNow we can create a pretty simple api to return hello world
-main.py
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
async def root():
return {'msg': 'hello world'}
Here in the above example, in the get method, we are adding '/' as a parameter, this defines that the function should return hello world at the base URL of the server!!
PermalinkViewing the results in localhost using uvicorn
Permalinklet's go to the directory of the project and run this command
uvicorn main:app --reload