How to Build CRUD API Using Flask MYSQL? – Devstringx

Back to Blog
How to build CRUD API

How to Build CRUD API Using Flask MYSQL? – Devstringx


  1. Python3
  2. The MYSQL server is downloaded and ready to run
  3. Flask

Setting Up Our Back-end Environment

We need to run the following commands to install the library:

pip install flask_restplus

pip install flask

pip install mysql

Pip is a package installer for Python and it comes with Python. Flask is a popular Python web framework, meaning it is a third-party Python library used for developing web applications.

Flask-RESTPlus is an extension for Flask that adds support for quickly building REST APIs. Flask-RESTPlus encourages best practices with minimal setup.

It provides a coherent collection of decorators and tools to describe your API and expose its documentation properly (using Swagger).

REST or RESTful Methods

HTTP methods mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to be created or updated, the basic patterns are listed as follows.

  • GET: Get/List/Retrieve an individual resource or a collection of resources.
  • POST: Create a new resource or resources.
  • PUT: Update an existing resource or collection of resources.
  • DELETE: Delete a resource or collection of resources.

Now as our project’s environment is ready, let’s get our hands dirty!

Step 1. Create the below script(py is the extension to indicate Python script) where we import the flask module. Notice how we create a flask instance.

from flask import Flask 

from flask_restplus import Api, Resource, fields

app = Flask(__name__)

Step 2.  Set up the MySQL database configurations for connecting to the database. We need to configure the database connection with the flask module and that’s why we have imported the app module and set up the MySQL configuration with the flask module.

from flask_mysqldb import MySQL

app.config['MYSQL_USER'] = 'root'

app.config['MYSQL_PASSWORD'] = ''

app.config['MYSQL_HOST'] = 'localhost'

app.config['MYSQL_DB'] = 'crud'

app.config['MYSQL_CURSORCLASS'] = 'DictCursor'

mysql = MySQL(app)

Step 3. Next, we will define all the models required for the request format. This we can integrate with the API and swagger. This will tell the user what API is expecting. 

<script src="" data-sid="41583"></script>

Step 4. Next, we will define all REST URLs for performing CRUD operations. It will also connect to the MySQL database server and query the database to read, insert, update and delete.

<script src="" data-sid="41582"></script>
Recommended to Read- Making DB Connection (MYSQL) Using C#
Let’s See The Things In Action

To run the python Script we need to execute the following command in the directory where the python script is present.

Command – python

The server will get started at


Swagger Url –

Flask Restplus CRUD

Post API

Post API



Related Post:-

Share this post

Back to Blog