Building REST APIs with Flask
# 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
jsonifyfunction 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 userendertemplate. To build an API, we skip HTML entirely and use a built-in Flask function called jsonify.
*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 callingjsonify.
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.
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.jsonifyonly understands basic Python datatypes (strings, integers, lists, dictionaries). It has absolutely no idea how to convert a complex SQLAlchemyUserobject into text. You must serialize (translate) the object into a dictionary first.
10. Exercises
-
1.
Explain the fundamental difference between building an application using Server-Side Rendering (using
rendertemplate) versus building an API (usingjsonify). 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:num1andnum2. Extract these numbers, multiply them, and return the result as JSON:{"result": 50}.
12. MCQs with Answers
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?
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 thejsonify function, we serialized Python dictionaries into universal JSON data structures. We learned how to intercept incoming JSON payloads from modern frontend clients using request.getjson(), and we explored the theoretical importance of organizing our endpoints following RESTful architectural standards.