17 March 2026
In today’s tech-driven world, APIs (Application Programming Interfaces) are the unsung heroes facilitating seamless communication between different software components. Whether you're checking your bank balance on a mobile app or using a third-party login service, there's a good chance an API is working behind the scenes. One of the most common types of APIs is RESTful APIs, which are designed to take advantage of HTTP protocols.
In this article, we’ll walk you through the process of building a RESTful API using Python’s Flask framework. Whether you’re a beginner or brushing up on your Flask skills, this tutorial will help you create flexible and scalable APIs with ease.

Flask’s flexibility and simplicity make it ideal for creating RESTful APIs. Plus, it has a vibrant community and countless plugins that can help you extend its functionality as your API grows in complexity.
Here’s a simple analogy: think of RESTful APIs as waiters in a restaurant. You (the client) make a request (your order), and the waiter (the API) goes to the kitchen (the server) to fetch your food (data). The waiter only brings you what you asked for and doesn't bring anything unnecessary. That’s how RESTful APIs work!
Now, let’s get our hands dirty and build one using Flask.

bash
python --version
If you don’t have Python installed, head over to python.org and download the latest version.
Next, let’s install Flask. Open up your terminal or command prompt and run:
bash
pip install Flask
This command will install Flask and all its dependencies, setting the stage for our API-building adventure.
bash
mkdir flask_api
cd flask_api
Create a Python file inside the folder for your project. You can call it `app.py`:
bash
touch app.py
python
from flask import Flask, jsonifyapp = Flask(__name__)
@app.route('/')
def index():
return jsonify({"message": "Welcome to the Book API"})
if __name__ == '__main__':
app.run(debug=True)
In this snippet, we’re importing Flask and the `jsonify` method, which helps convert Python dictionaries into JSON format. We then define a simple route (`/`) that returns a message when we access the root URL.
Run your Flask app by typing:
bash
python app.py
If everything works as expected, you should see the message: `Welcome to the Book API` when you go to `http://127.0.0.1:5000/` in your browser.
Add the following code to your `app.py` file:
python
from flask import Flask, jsonify, requestapp = Flask(__name__)
Sample data
books = [
{"id": 1, "title": "1984", "author": "George Orwell"},
{"id": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee"}
]@app.route('/')
def index():
return jsonify({"message": "Welcome to the Book API"})
Get all books
@app.route('/books', methods=['GET'])
def get_books():
return jsonify({"books": books})if __name__ == '__main__':
app.run(debug=True)
In this snippet, we’ve added a list of books hardcoded into our app. Then we created a new route `/books` which returns all the books when accessed via the GET method.
Try accessing `http://127.0.0.1:5000/books` in your browser. You should see a JSON response with the list of books. Nice, right?
python
Get a single book by ID
@app.route('/books/', methods=['GET'])
def get_book(book_id):
book = next((book for book in books if book["id"] == book_id), None)
if book:
return jsonify(book)
return jsonify({"message": "Book not found"}), 404
Here, we’re using Flask’s dynamic routing to accept a `book_id` parameter. We then search for the book in our list using Python’s `next()` function. If the book exists, we return it, otherwise, we return a 404 error.
Test it out by going to `http://127.0.0.1:5000/books/1` or a different book ID.
python
Add a new book
@app.route('/books', methods=['POST'])
def add_book():
new_book = request.get_json()
new_book["id"] = len(books) + 1
books.append(new_book)
return jsonify(new_book), 201
In this code, we’re using Flask’s `request.get_json()` to extract the JSON data from the incoming POST request. We then assign a new ID to the book and append it to our books list.
You can test this using a tool like Postman or `curl`. Here’s a sample `curl` request:
bash
curl -X POST http://127.0.0.1:5000/books -H "Content-Type: application/json" -d '{"title": "The Great Gatsby", "author": "F. Scott Fitzgerald"}'
python
Update a book
@app.route('/books/', methods=['PUT'])
def update_book(book_id):
updated_book = request.get_json()
book = next((book for book in books if book["id"] == book_id), None)
if book:
book.update(updated_book)
return jsonify(book)
return jsonify({"message": "Book not found"}), 404
This function works similarly to the GET method but instead, it updates the book's information. Try it out by sending a PUT request to update a book.
python
Delete a book
@app.route('/books/', methods=['DELETE'])
def delete_book(book_id):
global books
books = [book for book in books if book["id"] != book_id]
return jsonify({"message": "Book deleted"})
This code uses list comprehension to filter out the book with the given ID and deletes it. Simple and effective!
- `GET /books`: Retrieve all books
- `GET /books/{id}`: Retrieve a single book by ID
- `POST /books`: Create a new book
- `PUT /books/{id}`: Update a book by ID
- `DELETE /books/{id}`: Delete a book by ID
So, what are you waiting for? Grab your keyboard, and start building your own APIs! The sky’s the limit.
all images in this post were generated using AI tools
Category:
ProgrammingAuthor:
Adeline Taylor
rate this article
1 comments
Blaze Hodge
Great article! The step-by-step guide on building a RESTful API with Flask is clear and informative. I appreciate the practical examples; they really help illustrate the concepts effectively.
March 17, 2026 at 4:41 AM