In this study, we are going to take a look FastAPI and We will examine in detail with examples.
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
Why FastAPI
- Independent TechEmpower benchmarks show FastAPI applications running under Uvicorn as one of the fastest Python frameworks available
- Very high performance such as NodeJS and GO
- RESTFul API
- Fully supports asynchronous programming
- Can with run Uvicorn and Gunicorn.
- Fast to Development
- Fewer Bugs
- Completion everywhere. Less time debugging. Editor Supporting
- Concise Coding
- Robust, automatic interactive documentation.
- Fully compliant with Open API standards (Swagger), JSON Schema.
Uvicorn is an ASGI (async server gateway interface) compatible web server. Uvicorn is a lightning-fast ASGI server implementation.
Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX.
Installation
pip install fastapi
We will need an ASGI server. For this, uvicorn or hypercon should also be added.
pip install “uvicorn[standard]”
Example
- Create a file
main.py
from fastapi import FastAPIapp = FastAPI()@app.get(“/”)
def read_root():
return {“message”: “Hello World”}
that’s just all!
Run the server with:
$ uvicorn main:app --reloadINFO: Will watch for changes in these directories: ['/Users/***/Desktop/fastApi']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [19457] using WatchFiles
INFO: Started server process [19459]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:63444 - "GET / HTTP/1.1" 200 OK
Additional Information
$ uvicorn main:app --reload
- uvicorn : Uvicorn is an ASGI web server implementation for Python.
- main : the main python file we are running
- app : Fastpi instance field located in main file
- — reload: imports your code changes and runs the server again
You can easily stop the server with the Control-C key combination.
^CINFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [19459]
INFO: Stopping reloader process [19457]
Let’s add a new method to the api
from fastapi import FastAPIapp = FastAPI()
@app.get(“/”)def read_root(): return {“message”: “Hello World”}
@app.get(“/showMessage”)def show_message(message): return {“message”: “your message : “ + message }
works as new method get and takes a parameter. Actually this parameter is a querystring.
We are running our server again with the latest changes.
$ uvicorn main:app --reloadINFO: Will watch for changes in these directories: [‘/Users/starforce/Desktop/fastApi’]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [19708] using WatchFiles
INFO: Started server process [19710]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:63680 — “GET / HTTP/1.1” 200 OK
How can we create a document for the API
This answer is very simple, you don’t need to do anything.
Just add /docs to the end of the api url.
what about reDoc?
/redoc
You can also use the other http operations:
@app.post()
@app.put()
@app.delete()
@app.options()
@app.head()
@app.patch()
@app.trace()
We took a first look at the fast api. We will look issues such as routing and deployment later.
In summary, FastApi is really fast to develop, implement and work.
I hope this article will help you get started.
more to come..
Thanks for reading !
If you have any queries please this article in the comment section below.