Creating a messaging REST API in Python Flask


Exploring a Python microframework


Python Flask Logo

I've used Python for a lot of projects, from data acquisition, to analysis, to parsing. It's my favorite language at the moment; that esteemed position usually depends on the projects I'm developing at the moment. In the past, when I've been working with a lot of data viz, I like R; if it's Arduino development, I throw on my C++ hat; if I'm working on an Android project, it has to be Java; if I'm working with databases, usually SQL or some variant; and if I'm doing work in Linux, I like bash scripting.

Python has a lot of great things going for it -- I like the human readability, functional whitespace, and diverse libraries. Chances are, if you're developing a product, there's a library for it that's well-supported. I use mostly data libraries, but I wanted to stretch my Python knowledge... you never know what sort of tricks you can pick up from other libraries.

I know my way around HTML and CSS, so I decided to dive into a web framework to see how it works. I decided on Flask instead of Django because I like the extensible approach. Probably not that much of a surprise coming from someone who has a history of preferring light-weight Linux systems.

I decided to write an API that routes messages from one cell phone number to another through relays. In order to make it interesting, I included four subnet masks with different throughputs. For security, I used the HTTPBasicAuth module in Flask, which checks the user and password provided in the script at the bottom. For schema validation, I used the validictory library; an example schema is below.

The gist file below shows the guts of the web app. If you want to try it out, the endpoint is hosted at Heroku. You can find the code, along with a test at github.