Using Python to Create an API
APIs are fun to use because you hit the endpoint, and it returns the exact data it promised. In software development, APIs are one of the basic blocks that makes up just about all software out there. In this article we are goin to go over how to create an API using flask and flask_restful. Last, we are goin to go over how to deploy the API on Heroku.
The data used for this demonstration are the daily headlines of ESPN. Typically, this data should be scraped automatically, saved to a database, and then a user would request the data from our API. We are going to quickly go over our scrape tool.
We are using requests to call the the ESPN website and get the raw HTML data. Then, we are using beautifulsoup to parse the raw HTML data, and we can find the top stories in a section tag with a class name “col-three”. Then, we use the find_all() attribute to find all a tags which contain the headline name and URL. Finally, we use a for-loop to iterate over each entry to extract the text and URL and store it in a dictionary which gets returned.
Now, the juicy part. Creating an API is relatively easy because flask and flask-restful have all the necessary frameworks to easily build it. The following code builds our API.
We import Flask from flask, and we import Resource and Api from flask_restful. We create a class instance from Flask, and we pass the module name by using the __name__ variable. Next, we create a class instance of Api, and we pass the app variable which is a class instance of Flask.
The fun part begins when we create our ESPN_API endpoint which inherits from Resource. We create a get function which handles all get calls to this endpoint. In the get function, we call our get_top_news() method from our ESPN module. This returns a dictionary which contains headlines and URLs to the headlines. We return this data in a dictionary with the key “espn”. Last, we use the add_resource method from api, and we pass it our class, and the route we want our endpoint to be reached at. In this example, our endpoint is found at https://aggregating-content.herokuapp.com/espn
Bet you thought it was going to a couple of files and hundreds of lines of code. Well, it could be! This is just one endpoint. As more endpoints are added, the file must evolve, get separated, and code will increase. Imagine 50 endpoints! That would be amazing, but that’s enough praise!
In order to prepare our API to be deployed to Heroku, we need two things:
Our requirements file is straightforward. We list the modules we use in order to make our app run. We talked about all of them except gunicorn which is python web server module. This helps run our API in Heroku.
Our Procfile tells Heroku what kind of server to start, which library to use, and where to find the entry point to it. Be the file name is Procfile with a capital P.
Once those files are in our repo, then we are ready to push it to Heroku through their CLI.
This is a quick tutorial on how to get started making APIs with python. If you are interested how to integrate a SQL database be sure to leave some claps and comments to let me know you want to see it. You can find the repository to all the code here. If you are interested in contributing be sure to let me know in the repo. Feel free to fork and star it. :D