Create a RESTfull API using Python and Flask (Part 1)Kasun DissanayakeBlockedUnblockFollowFollowingMar 5RESTfull API ??A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
REST we called as REST(REpresentational State Transfer).
It is an architectural style and approach to communications in web services development.
A RESTful API explicitly takes advantage of HTTP methodologies like ,GET to retrieve a resourcePUT to change the state of or update a resource, which can be an object, file or blockPOST to create that resourceDELETE to remove it.
PYTHONPython is a full-fledged all-round language.
It’s an interpreted, interactive, object-oriented, extensible programming language.
For further reference check here.
FLASKFlask is a Python web framework which can be used to develop web applications.
Flask is unique in that it’s self-labeled as a “microframework”, meaning that the philosophy behind it is to provide a functional and concise core for your application, and to leave the rest up to you, the developer.
You can check more about this here.
Let’s GoHere, we’re going to learn,What an API is and when you should use one?.
How to build a web API that returns data to its users?.
Some principles of good API design, applying them to an API that draws book metadata from a database.
Pre-requisitesPython 3, the Flask web framework, and a web browser are required to build this API, and installation instructions for all platforms are described below.
I have used Windows environment here.
First you need to install Python.
To download Python, follow this link.
To confirm that Python has installed successfully, first open the command line.
Once your command line is open, enter these commands:python –versionpip –versionNext, you’ll need to install Flask.
Open up a terminal and type,pip install flaskThis will install Flask using the pip package manager for Python.
Congrats!!.Now you have setup the project configurations successfully.
Okay, Let’s get some knowledge about API’s.
Why do we need an API?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 change/update frequently.
The users who need your data may need a part of data at once.
The users will need to perform actions rather than retrieving data, such as updating, or deleting data.
When using or building APIs, you will encounter these terms frequently:HTTP (Hypertext Transfer Protocol) — The underlying protocol used by the World Wide Web(WWW) and this protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands.
URL (Uniform Resource Locator) — A unique identifier used to locate a resource on the internet.
It is also referred to as a web address.
URLs consist of multiple parts — including a protocol and domain name — that tell a web browser how and where to retrieve a resource.
It is easy for humans to read and write.
It is easy for machines to parse and generate.
Web services that conform to the REST architectural style, termed RESTful Web services, provide interoperability between computer systems on the Internet.
Our ProjectIn this project we’re going to build a prototype API using Python and the Flask web framework.
Our example API will take the form of a distant reading archive — get media details such as music tracks, playlists, artists, albums to those working on digital projects.
We’ll begin by using Flask to create a home page for our site.
In this step, we’ll learn the basics of how Flask works and make sure our software is configured correctly.
Once we have a small Flask application working in the form of a home page, we’ll iterate on this site, turning it into a functioning API.
Okay first we are going to name our project as myfirst_api.
Make folder and name it as myfirst_api.
Change current working directory to myfirst_api.
mkdir myfirst_apicd myfirst_apiThen create directory and name it as api.
mkdir apiYou can also create the myfirst_api and api folders using your operating system’s graphical user interface.
Now, choose your favourite IDE to do the implementation.
I have used Visual Studio Code here.
Save this code as api.
py in the api folder.
import flaskapp = flask.
config["DEBUG"] = True@app.
route('/', methods=['GET'])def home(): return '''<h1>Welcome to New Project</h1><p>A prototype API for distant reading of Music Albums and Playlists.
run()In the command line, navigate to your api folder:cd myfirst_api/apiNow, run the Flask application with the command:python api.
pyYou should see output similar to this:Type this URL in your browser and press enter http://127.
1:5000/ (Press CTRL+C to quit)Congratulations, you’ve created a working web application!In this tutorial, you saw how relatively easy it is to create a comprehensive REST API with Python.
Now that we have a running Flask application and know a little about what Flask does.
In Part 2 of this series, you’ll learn how to add our data as a list of Python dictionaries, Designing Requests and How to connect our API to a Database.