Want to know how to implement a REST API service
Implement a production ready REST service using FastAPI
This guide will help you to implement a REST API service by creating a blog service in FastAPI

Prerequisites: Assuming that you already knew about Python and basic SQL
What we will be using during implementation:
- FastAPI
- SQLAlchemy
- JWT
- Bcrypt
After finish implementation, we can create a new blog or view a blog like below:


Alright, let’s jump right in.
Introduction to some technologies we will be using
1.FastAPI:
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
The key features are:
- 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.
- estimation based on tests on an internal development team, building production applications.
You can get more details from FastAPI from here
2.SQLAlchemy:
SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.
You can get details about SQLAlchemy in the SQLAlchemy documentation
3.JWT:
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
We will typically use jwt for authentication of our API service.
Details about jwt can be found in here.
4.Bcrypt:
Bcrypt is a crypt library used to hash the user password before putting it into the database for better security
Install the necessary packages
For better management, we will use pipenv to create a virtual environment for our project.
Below is the Pipfile which list all our needed packages:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
fastapi = "*"
uvicorn = "*"
sqlalchemy = "*"
mysql-connector-python = "*"
bcrypt = "*"
pytest = "*"
pyjwt = "*"
[requires]
python_version = "3.7"To install the packages above, simply run
pipenv updateAbout project structure
main.pyThis is the entry point for our FastAPI servicemodels.pyTo list all the models for working with databasedatabase.pyDefine how to access MySQL databasecrud.pyDefine crud method for working with MySQL via SQLAlchemy ORMapp_utils.pyDefine small utilities we will be using for our service





