INTRODUCTION TO FASTAPI — part 1
By: Ieshaka Karunaratne
“FastAPI” is the new buzzword in the world of python. It’s a web framework for building APIs with python 3.7 or above.
The key features of FastAPI are: [KM1]
· Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic), one of the fastest Python frameworks available.
· Fast to code: Increase the speed to develop features by about 200% to 300%. *
· Fewer bugs: Reduce about 40% of human (developer) induced errors. *
· Intuitive: Great editor support. Completion everywhere. Less time debugging.
· Easy: Designed to be easy to use and learn. Less time reading docs.
· Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
· Robust: Get production-ready code. With automatic interactive documentation.
· Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
(reference: FastAPI (tiangolo.com))
The following parts of the article will be a guide to creating a very simple API using FastAPI.
Step 1: Install FastAPI and uvicorn libraries
“pip install fastapi uvicorn”
[KM1]Is have the points below in bullet points (as a direct copy-paste from the source given) ok to start the blog? Doesn’t feel like a blog and more of a guide document to me overall.
Step 2: Create a file called “main.py” in a folder location that you prefer.
Step 3: write the below code snippet in the “main.py” file
Step 4: To run the code, open a command prompt, direct to the folder location that the “main.py” file is, and run the following command
“uvicorn main:app –reload”
Step 5: When you open http://127.0.0.1:8000 in the browser, you’ll be directed to the index route of the app.
Another important part is that there is an automatic interactive API documentation based on “Swagger UI”, where one can test the API and its functionality easily.
“http://127.0.0.1:8000/docs” open this link in the browser once the app is running to access the above-mentioned API documentation.
By using the above window, you can play with the API and see the results. This is how easy it is to build a web app using FastAPI. Part 2 of this article will contain to serve a machine[KM1] learning model using FastAPI.