---
title: "How can you implement a simple REST API using Flask?"  
description: "How can you implement a simple REST API using Flask?"  
author: "Anubhav Sharma"  
published: 2025-11-06  
updated: 2025-12-01  
canonical: https://www.mindstick.com/forum/161985/how-can-you-implement-a-simple-rest-api-using-flask  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# How can you implement a simple REST API using Flask?

**How can you implement a [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) [REST API](https://www.mindstick.com/forum/160553/how-to-authenticate-rest-api-in-c-sharp) using [Flask](https://www.mindstick.com/blog/303630/unlocking-the-power-of-flask-understanding-its-purpose-and-applications)?**

## Replies

### Reply by Anubhav Sharma

> A **clean, beginner-friendly** explanation and example of how to implement a **simple [REST](https://www.mindstick.com/forum/12745/how-do-i-use-webapi-rest-correctly-when-other-params-are-needed) [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph) using Flask**.

### 1. Install Flask

```plaintext
pip install flask
```

### 2. Create the app structure

```plaintext
project/
 └── app.py
```

### 3. Basic REST API Example (CRUD)

Here is a complete working example that implements:

- **GET** (read all)
- **GET by ID**
- **POST** (create)
- **PUT** (update)
- **DELETE** (remove)

#### app.py

```python
from flask import Flask, jsonify, request

app = Flask(__name__)

# Fake in-memory database
books = [
    {"id": 1, "title": "Book One", "author": "Author A"},
    {"id": 2, "title": "Book Two", "author": "Author B"},
]

# GET all books
@app.route("/books", methods=["GET"])
def get_books():
    return jsonify(books)

# GET book by ID
@app.route("/books/<int:book_id>", methods=["GET"])
def get_book(book_id):
    book = next((b for b in books if b["id"] == book_id), None)
    return jsonify(book) if book else ("Not Found", 404)

# POST → Create new book
@app.route("/books", methods=["POST"])
def add_book():
    data = request.get_json()

    new_book = {
        "id": books[-1]["id"] + 1 if books else 1,
        "title": data["title"],
        "author": data["author"]
    }

    books.append(new_book)
    return jsonify(new_book), 201

# PUT → Update book
@app.route("/books/<int:book_id>", methods=["PUT"])
def update_book(book_id):
    data = request.get_json()
    book = next((b for b in books if b["id"] == book_id), None)

    if not book:
        return ("Not Found", 404)

    book["title"] = data.get("title", book["title"])
    book["author"] = data.get("author", book["author"])

    return jsonify(book)

# DELETE → Remove book
@app.route("/books/<int:book_id>", methods=["DELETE"])
def delete_book(book_id):
    global books
    books = [b for b in books if b["id"] != book_id]
    return ("Deleted", 204)

if __name__ == "__main__":
    app.run(debug=True)
```

### 4. Run the API

```plaintext
python app.py
```

Flask will start at:

```plaintext
http://127.0.0.1:5000
```

### Test the API using cURL or Postman

#### GET all

```plaintext
GET http://127.0.0.1:5000/books
```

#### POST

```plaintext
POST http://127.0.0.1:5000/books
Content-Type: application/json

{
  "title": "New Book",
  "author": "New Author"
}
```


---

Original Source: https://www.mindstick.com/forum/161985/how-can-you-implement-a-simple-rest-api-using-flask

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
