Skip to main content
Flask Basics Tutorial
CHAPTER 13 Beginner

Building REST APIs with Flask

Updated: May 14, 2026
35 min read

# CHAPTER 13

Building REST APIs with Flask

1. Introduction

Modern web architecture is highly decentralized. Often, your backend must serve data not just to web browsers via HTML templates, but to iOS apps, Android apps, and separate React/Vue frontend applications. These clients do not understand HTML; they require raw data formatted as JSON. To achieve this, we build REST APIs. Because Flask is minimal and fast, it is widely considered one of the best frameworks in the world for building APIs. In this chapter, we will learn how to return JSON data, parse incoming JSON requests, and build a RESTful endpoint.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the difference between rendering templates and returning JSON.
  • Use Flask's jsonify function to serialize Python dictionaries.
  • Extract incoming JSON payloads using request.getjson().
  • Structure endpoints following REST architectural principles.

3. Beginner-Friendly Explanation

Imagine you own a weather station (The Database).
  • Standard Web App (HTML): A local citizen asks for the weather. You paint a beautiful picture of a sun, write "72 Degrees" in fancy calligraphy, frame it (HTML), and hand it to them.
  • REST API (JSON): A news corporation from another country asks for the weather so they can display it on their own TV broadcast. They don't want your fancy picture; it won't fit on their TV. They just want the raw numbers. You send them a simple clipboard with raw text: {"temperature": 72, "conditions": "sunny"}. They take that raw data and paint their *own* beautiful picture on their TV.

An API simply delivers the raw data (JSON), allowing the client (React, iOS, etc.) to decide how it looks.

4. Step 1: Returning JSON Data

In standard Flask routes, we use render
template. To build an API, we skip HTML entirely and use a built-in Flask function called jsonify.
python
12345678910111213141516
from flask import Flask, jsonify

app = Flask(__name__)

# A simple API endpoint
@app.route('/api/status', methods=['GET'])
def get_status():
    # 1. Create a standard Python dictionary
    data = {
        "status": "online",
        "version": "1.0.4",
        "active_users": 42
    }
    
    # 2. Convert the dictionary to JSON and return it!
    return jsonify(data)

*If you visit /api/status in your browser, you won't see a webpage. You will see raw text data perfectly formatted in JSON.*

5. Step 2: Extracting Database Data (Serialization)

When an API requests all users, we must fetch the SQLAlchemy objects and convert them into dictionaries before calling jsonify.
python
1234567891011121314151617
@app.route('/api/users', methods=['GET'])
def get_users():
    users = User.query.all()
    
    # Create an empty list
    user_list = []
    
    # Loop through the database objects and convert them to dictionaries
    for user in users:
        user_data = {
            "id": user.id,
            "username": user.username,
            "email": user.email
        }
        user_list.append(user_data)
        
    return jsonify({"users": user_list})

6. Step 3: Accepting JSON Payloads (POST)

When a React frontend or Mobile App sends data to your server to create a user, they do NOT use HTML forms (request.form). They send raw JSON data.
python
123456789101112131415161718
from flask import request

@app.route('/api/users/create', methods=['POST'])
def create_api_user():
    # Extract the raw JSON from the incoming request
    data = request.get_json()
    
    # Access the data exactly like a Python dictionary
    uname = data.get('username')
    uemail = data.get('email')
    
    # Save to database...
    new_user = User(username=uname, email=uemail)
    db.session.add(new_user)
    db.session.commit()
    
    # Return a success message and an HTTP 201 Created status code
    return jsonify({"message": "User successfully created!", "id": new_user.id}), 201

7. Backend Workflow: REST Principles

When designing an API, professional developers follow the REST (Representational State Transfer) architecture. You should use standard HTTP verbs to interact with a single URL endpoint.

For example, manipulating "Posts":

  • GET /api/posts (Fetch all posts)
  • POST /api/posts (Create a new post)
  • GET /api/posts/5 (Fetch post #5)
  • PUT /api/posts/5 (Completely overwrite post #5)
  • DELETE /api/posts/5 (Delete post #5)

Notice we did not use URLs like /api/posts/create or /api/posts/delete/5. The URL represents the *resource* (/api/posts), and the HTTP Method (GET, POST, DELETE) dictates the *action*.

8. Best Practices

  • Marshmallow: Manually looping through database objects to convert them into dictionaries (as seen in Step 2) is incredibly tedious. In production, Flask developers use an extension called Flask-Marshmallow. It acts as an automated translator, turning complex database models into JSON schemas in just two lines of code.

9. Common Mistakes

  • TypeErrors with JSON: Beginners often try to return jsonify(User.query.all()). This will immediately crash the server. jsonify only understands basic Python datatypes (strings, integers, lists, dictionaries). It has absolutely no idea how to convert a complex SQLAlchemy User object into text. You must serialize (translate) the object into a dictionary first.

10. Exercises

  1. 1. Explain the fundamental difference between building an application using Server-Side Rendering (using rendertemplate) versus building an API (using jsonify). Who handles the HTML generation in each scenario?

11. Coding Challenges

  • Challenge: Write an API endpoint @app.route('/api/math/multiply', methods=['POST']). It should expect an incoming JSON payload with two keys: num1 and num2. Extract these numbers, multiply them, and return the result as JSON: {"result": 50}.

12. MCQs with Answers

Question 1

Which Flask function is responsible for taking a Python dictionary, serializing it into a JSON string, and attaching the correct application/json HTTP headers to the response?

Question 2

When a React frontend application sends a POST request containing JSON data to a Flask backend, which Flask method is used to extract that specific payload?

13. Interview Questions

  • Q: Explain REST architectural principles. Provide an example of how you would structure the endpoints for a generic "Product" resource using standard HTTP verbs (GET, POST, PUT, DELETE).
  • Q: What does the term "Serialization" mean in the context of building a Python API? Why is it a necessary step before returning database records over the network?

14. FAQs

Q: How do I test my API without a frontend? I can't trigger a POST request from my Chrome browser address bar! A: Developers use API testing tools. The industry standard is Postman (a desktop app) or Thunder Client (a VS Code extension). These tools allow you to manually construct and send GET, POST, and DELETE requests with custom JSON payloads directly to your local server.

15. Summary

In Chapter 13, we transformed our Flask application from a traditional website into a headless data provider. By utilizing the jsonify function, we serialized Python dictionaries into universal JSON data structures. We learned how to intercept incoming JSON payloads from modern frontend clients using request.get
json(), and we explored the theoretical importance of organizing our endpoints following RESTful architectural standards.

16. Next Chapter Recommendation

Our APIs handle raw text perfectly, but modern applications also require images. Proceed to Chapter 14: File Uploads and Static Files.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·