Getting Started With FastAPI
hello welcome to my other blog post, in this post we will discuss another way to build API with python, we will talk about Fast API how to build API with this framework, any better way to learn this framework, and why use this framework, in this post, we will discuss it thoroughly, see the discussion below
what we will discuss
this is the outline of our discussion
- Introduction To FastApi
- Project Preparing
- Build Simple Project with FastAPI
Introduction To Fast API
Fast API is a modern web framework to build modern API based on python 3.6+ with this framework we can build high performance, easy to learn, fast to code, ready for a production project.
Projects Preparing
how to build some projects with the FastAPI framework..? and how you know that let’s discuss
How To Install FastAPI
FastAPI can download in pip https://pypi.org/project/fastapi/
pip install fastapi
install uvicorn or search on pypi https://pypi.org/project/uvicorn/
pip install 'uvicorn[standard]'
after all required package has installed, let’s create a sample project
Build Sample Project with FastAPI
for the first step, create a virtual environment to isolate the python package with this command
python -m venv getting_venv
then the virtual environment has created, activate with this command
source gettting_venv/bin/activate
the second steps create a file named main.py then fill it like this
from fastapi import FastAPI
# init app
app = FastAPI()
# route
@app.get('/')
def read_root():
return {'hello':'world from fast api'}
now we discuss, what we actually wrote earlier
in the first line, we import class FastApi
from fastapi
module, then initialize the app in the second line
app = FastAPI()
well, then we just create a route for our application
# route
@app.get('/')
def read_root():
return {'hello':'world from fast api'}
this route will return {'hello':'world from fast api'}
if you run app with uvicorn use this command
uvicorn main:app --reload
you will see
if you see the server response in a terminal you will see
Passing Dynamic Route
now we will create a new route for passing dynamic route, in some condition we need to add some query in route like http://example.com/somequery
it will change dynamically, the solution is to store a URL query in a variable.
let’s create a new route
# new route
@app.get('/item/{item_id}')
def get_item_name(item_id: int, query: Optional[str] = None):
return {
'item_id': item_id,
'query': query
}
let’s run and see the response, input http://localhost:8000/item/1?query=clotes for example you will see the response
The Amazing Feature of FastAPI
FastAPI framework have Amazing features, namely interactive documentation features if you type http://localhost:8000/docs you will be seen documentation of your API like this
so that when we connect our API with other applications, we can avoid route errors.
Conclusion
FastAPI is a future framework for building API, with an amazing feature, until here first the discussion this time, hopefully, useful and good luck written by Feri Lukmansyah