Building Basic RESTful API with Flask-RESTful
Software Engineer | Tech Writer
What is Flask
Flask is a Python micro-framework for building web applications and web APIs. Being lightweight, easy to adopt, well-documented, and popular, Flask is a very good option for developing RESTful APIs
API - Application Programming Interface
When Create an API
In general, consider an API if:
- Your data set is large, making download via FTP unwieldy or resource-intensive.
- Your users will need to access your data in real time, such as for display on another website or as part of an application.
- Your data changes or is updated frequently.
- Your users only need access to a part of the data at any one time.
- Your users will need to perform actions other than retrieve data, such as contributing, updating, or deleting data.
REST stands for Representational State Transfer. A RESTFUL API allows for interaction with RESTful web services by conforming to the constraints of REST architectural style.
Libraries required: Flask-Restful is an extension of Flask that offers an extension to enable writing a clean object-oriented code for fast API development
Environment setup
- Ensure that you have python installed. To confirm that Python is installed successfully, first open the command line
python --version
pip --version
If not, to download Python, follow this link
- Create a directory to store your Flask web application and move into the directory
mkdir my_app && cd my_app
- Inside the my_app directory, create a new file named my_app.py
Create a virtual environment and activate
python3 -m venv venv
. venv/bin/activate
Installation Install flask
pip install flask
Once you have Flask installed in your virtual environment, you are ready to go to the next step of the development phase
Minimal Flask App
from flask import Flask #Import the Flask class
app = Flask(__name__) #Create an instance of the class
@app.route('/hello/', methods=['GET', 'POST'])
def welcome():
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True)
Running the Application Once you’re in your project directory, run the Flask application with the command:
python api.py
You should see the output:
* Running on http://127.0.0.1:5000/
Creating the API There are two ways of creating a REST API in Flask:
Using Flask without any external libraries Using flask_restful library
In this article, I am going to use the flask_restful library
Using flask-restful Flask Restful is an extension for Flask that adds support for building REST APIs in Python using Flask as the back-end. It encourages best practices and is very easy to set up. Flask restful is very easy to pick up if you’re already familiar with flask.
The main building block provided by Flask-RESTful are resources. Resources are built on top of Flask pluggable views, giving you easy access to multiple HTTP methods just by defining methods on your resource.
install the flask-restful library.
pip install flask-restful
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
# Create some test data for our catalog in the form of a list of dictionaries.
books = [
{'id': 0,
'title': 'A Fire Upon the Deep',
'author': 'Vernor Vinge',
'first_sentence': 'The coldsleep itself was dreamless.',
'year_published': '1992'},
{'id': 1,
'title': 'The Ones Who Walk Away From Omelas',
'author': 'Ursula K. Le Guin',
'first_sentence': 'With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea.',
'published': '1973'},
{'id': 2,
'title': 'Dhalgren',
'author': 'Samuel R. Delany',
'first_sentence': 'to wound the autumnal city.',
'published': '1975'}
]
@app.route('/', methods=['GET'])
def home():
return '''<h1>Distant Reading Archive</h1>
<p>A prototype API for distant reading of science fiction novels.</p>'''
# A route to return all of the available entries in our catalog.
@app.route('/api/v1/resources/books/all', methods=['GET'])
def api_all():
return jsonify(books)
# driver function
if __name__ == '__main__':
app.run(debug = True)
Finding Specific Resources



