Geek Culture
Published in

Geek Culture

Using Python to Create an API

Photo by Ryunosuke Kikuno

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

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:

  1. Procfile
  2. requirements.txt

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




A new tech publication by Start it up (

Recommended from Medium

That time I failed at the basics

Value Stream Mapping: A Complete and Simple Guide

The Always Lurking Rewrite

2022. 03. 28 Today I did / Today I learned

My first open source project “Elastic SIEM”

DeepBrain Chain 2020 End of Year Report (#79)

Save the Date: Apollo Contributor Week

18 challenges for 2018

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alejandro Colocho

Alejandro Colocho

More from Medium

WTF is a Decorator??? (Python)

Python Puzzles — Word Capitalization!

HARMAN Python Training Day2

Iterators and Generators in Python