Developing RESTful APIs with Python and Flask
Tags: python, programming, tutorial
Published: 2022-09-28 16:54:34

> Original post written by [Juan Cruz Martinez](https://auth0.com/blog/authors/juan-cruz-martinez/) and [Bruno Krebs](https://auth0.com/blog/authors/bruno-krebs/) for Auth0 blog.

### Let's learn how to develop RESTful APIs with Python and Flask.

### <br>

**TL;DR:** Throughout this article, we will use Flask and Python to develop a RESTful API. We will create an endpoint that returns static data (dictionaries). Afterward, we will create a class with two specializations and a few endpoints to insert and retrieve instances of these classes. Finally, we will look at how to run the API on a Docker container. [The final code developed throughout this article is available in this GitHub repository](https://github.com/auth0-blog/flask-restful-apis). I hope you enjoy it!

<include src="TweetQuote" quoteText="Flask allows Python developers to create lightweight RESTful APIs."/>

## Summary

This article is divided into the following sections:

1. <a href="#why-python" target="_self">Why Python?</a>
2. <a href="#why-flask" target="_self">Why Flask?</a>
3. <a href="#bootstrapping-flask" target="_self">Bootstrapping a Flask Application</a>
4. <a href="#restful-flask" target="_self">Creating a RESTful Endpoint with Flask</a>
5. <a href="#python-classes" target="_self">Mapping Models with Python Classes</a>
6. <a href="#marshmallow-serilization" target="_self">Serializing and Deserializing Objects with Marshmallow</a>
7. <a href="#flask-on-docker" target="_self">Dockerizing Flask Applications</a>
8. <a href="#securing-python-apis" target="_self">Securing Python APIs with Auth0</a>
9. <a href="#next-steps" target="_self">Next Steps</a>

## <span id="why-python"></span> Why Python?

Nowadays, choosing Python to develop applications is becoming a very popular choice. [As StackOverflow recently analyzed](https://survey.stackoverflow.co/2022/#technology-most-popular-technologies), Python is one of the fastest-growing programming languages, having surpassed even Java in the number of questions asked on the platform. On GitHub, the language also shows signs of mass adoption, occupying the second position among the [top programming languages in 2021](https://octoverse.github.com/).

![Stack Overflow Trends showing Python growth](https://images.ctfassets.net/23aumh6u8s0i/2HG9CcCZvBw4I3jOOlrPHP/02991763f1b1fbf1070ab9d498a02ad2/python-trend-so.png)

The huge community forming around Python is improving every aspect of the language. More and more open source libraries are being released to address many different subjects, like [Artificial Intelligence](https://github.com/aimacode/aima-python), [Machine Learning](https://github.com/rasbt/python-machine-learning-book), and [web development](https://github.com/pallets/flask). Besides the tremendous support provided by the overall community, the [Python Software Foundation also provides excellent documentation](https://docs.python.org/3/), where new adopters can learn its essence fast.

## <span id="why-flask"></span> Why Flask?

When it comes to web development on Python, there are three predominant frameworks: [Django](https://github.com/django/django), [Flask](https://github.com/pallets/flask), and a relatively new player [FastAPI](https://fastapi.tiangolo.com/). Django is older, more mature, and a little bit more popular. On GitHub, this framework has around 66k stars, 2.2k contributors, ~ 350 releases, and more than 25k forks.

FastAPI is growing at high speed, with 48k stars on Github, 370 contributors, and more than 3.9k forks. This elegant framework built for high-performance and fast-to-code APIs is not one to miss.

Flask, although less popular, is not far behind. On GitHub, Flask has almost 60k stars, ~650 contributors, ~23 releases, and nearly 15k forks.

Even though Django is older and has a slightly more extensive community, Flask has its strengths. From the ground up, Flask was built with scalability and simplicity. Flask applications are known for being lightweight, mainly compared to their Django counterparts. Flask developers call it a microframework, where micro ([as explained here](http://flask.pocoo.org/docs/0.12/foreword/#what-does-micro-mean)) means that the goal is to keep the core simple but extensible. Flask won't make many decisions for us, such as what database to use or what template engine to choose. Lastly, Flask has [extensive documentation](http://flask.pocoo.org/docs/0.12/) that addresses everything developers need to start.
FastAPI follows a similar "micro" approach to Flask, though it provides more tools like automatic Swagger UI and is an excellent choice for APIs. However, as it is a newer framework, many more resources and libraries are compatible with frameworks like Django and Flask but not with FastAPI.

Being lightweight, easy to adopt, well-documented, and popular, Flask is a good option for developing RESTful APIs.

## <span id="bootstrapping-flask"></span> Bootstrapping a Flask Application

First and foremost, we will need to install some dependencies on our development machine. We will need to install [Python 3](https://docs.python.org/3/), [Pip (Python Package Index)](https://pypi.python.org/pypi/pip), and [Flask](http://flask.pocoo.org/).

### Installing Python 3

If we are using some recent version of a popular Linux distribution (like Ubuntu) or macOS, we might already have Python 3 installed on our computer. If we are running Windows, [we will probably need to install Python 3](https://www.python.org/downloads/windows/), as this operating system does not ship with any version.

After installing Python 3 on our machine, we can check that we have everything set up as expected by running the following command:

```bash
python --version
# Python 3.8.9
```

Note that the command above might produce a different output when we have a different Python version. What is important is that you are running at least `Python 3.7` or newer. If we get "Python 2" instead, we can try issuing `python3 --version`. If this command produces the correct output, we must replace all commands throughout the article to use `python3` instead of just `python`.


[Read more...](https://auth0.com/blog/developing-restful-apis-with-python-and-flask/?utm_source=devto&utm_medium=sc&utm_campaign=devto)