DEV Community

Cover image for Building a RESTful API with Flask
Louiza Mak
Louiza Mak

Posted on

Building a RESTful API with Flask

I'm going to try my hand at doing a basic but comprehensive tutorial this time. After working on my first full stack project, I've realized how much I've enjoyed the backend and creation of an API. It was a super neat experience and I feel that I've learned a lot on what I'm developing and why it has to be done a certain way. So here we go!

If you're working on a Flask project or just trying to learn the next step after Python basics, you're in the right place. A crucial skill for any web developer, beginner or otherwise, is being able to build a RESTful API. RESTful APIs are interfaces that two computer systems use to communicate through HTTP requests. These requests can perform CRUD - standard database functions like creating, reading, updating, and deleting records. To put it simply, it is a way to access the web in a simple and flexible way. For this blog, we will use Flask, a lightweight web framework for Python that makes it easier to create APIs quickly and efficiently. In addition, we'll cover the basics of RESTful principles, set up a dev environment, and create API endpoints!

RESTful Principles

REST AKA Representational State Transfer is an architectural style for designing networks applications. Key principles of REST include:

  1. Statelessness: Each request from a client to a server must contain all the information needed to understand and process the request. This way, requests are independent of all previous requests and are stored client side. The API can then be scaled to millions of concurrent users while maintaining simplicity for development, testing, and maintenance.

  2. Uniform Interface: This interface constraint simplifies and decouples the architecture of the API by standardizing how resources are sent. API should use standard HTTP methods such as GET, POST, PUT, and DELETE. In addition, data sent from the server is typically represented in JSON or XML format. This representation of the data should also include enough information to modify or delete this resource.

  3. Client-Server Architecture: Client and server should both be independent and able to change separately. Keep it simple. This is the current standard practice of web dev.

  4. Layered System: API architecture is composed of layers with their own responsibilities. For example, the API can be deployed on layer A, data is stored on layer B, and authentication requests in layer C. Each of these layers do not know anything about the other servers and this allows load-balancing and improved system availability.

Development Environment

A development environment is a tool that creates an isolated environment for developers to make changes without affecting a live environment. It usually contains its own set of processes and programming tools as well.

Make sure Python is installed before we start coding. We will also use some other libraries such as pip, Python package installer, and Flask.

First, install Flask:

$ pip install Flask
Enter fullscreen mode Exit fullscreen mode

Then, create a project directory:

$ mkdir flask_restful_api
$ cd flask_restful_api
Enter fullscreen mode Exit fullscreen mode

Lastly, install Flask-RESTful:

$ pip install Flask-RESTful
Enter fullscreen mode Exit fullscreen mode

API Endpoints

For this example, we will help out a shelter and make a simple API to manage their current dogs. We will also create endpoints to do CRUD operations. First, let's create a simple project structure. Create these files if you haven't already.

flask_restful_api/
├── app.py
├── models.py
└── config.py
Enter fullscreen mode Exit fullscreen mode

Next, define the Dog model. We will give each dog basic traits to keep track of - name, breed, and age. We will also create an array of three dogs for testing purposes.

Dog model

Second, we need to create the API Resources. Below is an example of the primary HTTP methods which correspond to the CRUD operations of create, read, update, and delete.

Dog Resources

Finally, we will initialize the Flask application.

App Config

Testing

For testing our application endpoints, you can use an API testing tool like Postman or Insomnia.

Running the App

We are finally ready to run the application. You will be able to access it at http://127.0.0.1:5000/. In your project directory, execute the following command:

python app.py
Enter fullscreen mode Exit fullscreen mode

Conclusion

These are the basics of creating a RESTful API with Flask. Using this as a launching off point, you'll be able to build a more robust API and soon connect it to a database and a frontend like React. For future suggestions on what to look into, check out the following below.

REACT: An open-source frontend JavaScript library. React is great for developing single-page applications and easy to pick-up.

SQLAlchemy: Python SQL toolkit and ORM (Object Relational Mapper). Use it to work with databases with Python objects.

Using React and SQLAlchemy in tandem with Flask-RESTful will allow you to create your first full-stack project with your own API. There are also many other powerful tools similar to the listed above that can be used. Happy exploration and coding!

Top comments (0)