Create RESTful API using Python and MySQL

REST stands for Representational State Transfer. RESTful Web services are one way of providing interoperability between computer systems on the Internet. Rest API help to communicate between the client app and the server application.REST is an architecture style for designing networked applications. A REST API defines a bunch of functions in which developers can perform requests and receive responses via HTTP protocol such as GET and POST.
In this tutorial, you will learn how to create CRUD (create, read, update, delete) operation REST API using Python and MySQL. This is a very simple example, you can copy-paste, and change it according to your requirement.
Before started to implement the REST API using Python and MySQL, look files structure:
  • restful-api-using-python-mysql
    • app.py
    • config.py
    • main.py
Understanding REST API
REST provides a block of HTTP methods which are used to alter the data. The following are common HTTP methods:
  • GET — is used for reading and retrieving data.
  • POST — is used for inserting data.
  • PUT/PATCH — is used for updating data.
  • DELETE — is used for deleting data.
Step 1: Create MySQL Database and Table
For this tutorial, you need a MySQL database with the following table:
Step 2: Import Flask Modules
We handle REST API functionality using Flask and MySQL, so we will need both the modules. The module Flask works as a web framework while the MySQL module requires making a connection with the MySQL database. We will create a project directory restful-api-using-python-mysql and move inside run cd command. We will install flask module by running below command.
Step 3: Run flask-cors
Then we will install the flask-cors extension for handling Cross-Origin Resource Sharing (CORS)
Step 4: Create file
So now we will create the app.py Python script and import the flask module and create the flask instance to use with the MySQL module. We will also import flask-cors extension for cross-origin (CORS).
Step 5: Create a MySQL Connection
We will install Flask-MySQL extension using below command.
We will create config.py Python file to initialize MySQL database connection details to make connection with MySQL database. We will import the app script to handle MySQL database connection with Flask-MySQL module.
Step 6: finally create REST API CRUD Operation
We will create a file main.py script and import the app and config modules. We will connect to the MySQL database and implement CRUD operations by defining all REST URIs.
Step 7: Run Application
Now we will go the project restful-api-using-python-mysql and run command python main.py and the server will start on default PORT 5000. We will use Postman to run Python RESTful API with (POST, GET, PUT or DELETE) methods to run it.
We will run the below URL and create new employee record with the POST HTTP method.
The request body will be following:
We will run the below URL with HTTP GET method to get all employee data returned in JSON format.
The request body will be following:
We will get the employee data using the below URL with HTTP GET method.
The request body will be following:
We will update existing employee data using HTTP PUT method.
The request body will be following:
We will delete existing employee data using HTTP DELETE method.
The request body will be following: