- Implement RESTful API endpoints using Flask.
- Handle HTTP POST, PATCH, and DELETE methods to manage resource data.
- Accept and process JSON input using
request.get_json()
. - Simulate persistent data using in-memory Python objects.
- Follow RESTful route conventions and return structured JSON responses.
In this lab, you will build a Full CRUD API to manage a list of events. The API will allow users to:
- Create new events using
POST
- Update existing events using
PATCH
- Delete events using
DELETE
You’ll simulate database-like behavior with in-memory Python class objects and respond to all client requests with properly formatted JSON and appropriate status codes.
This lab reinforces essential backend development skills including route design, data mutation, error handling, and RESTful conventions.
- Go to the provided GitHub repository link.
- Fork the repository to your GitHub account.
- Clone the forked repository to your local machine:
git clone <repo-url>
cd course-8-module-5-flask-full-crud-api-lab
Ensure Python is installed:
python --version
Install Flask and dependencies using pipenv:
pipenv install
pipenv shell
Or with pip:
pip install flask
You’re building a basic event management API. It should:
- Accept event creation via
POST /events
- Allow updating event titles via
PATCH /events/<id>
- Delete events using
DELETE /events/<id>
- Respond with structured JSON and appropriate HTTP status codes
The Flask API should be structured as follows:
- Use
@app.route()
with correct HTTP method decorators - Accept input using
request.get_json()
- Represent data using a custom
Event
class - Store events in an in-memory list
- Use
jsonify()
for consistent JSON responses
Create app.py
and start with the following structure:
from flask import Flask, jsonify, request
app = Flask(__name__)
# Event class
class Event:
def __init__(self, id, title):
self.id = id
self.title = title
def to_dict(self):
return {"id": self.id, "title": self.title}
# In-memory data store
events = [
Event(1, "Tech Meetup"),
Event(2, "Python Workshop")
]
# TODO: POST /events - Create a new event from JSON input
# TODO: PATCH /events/<id> - Update the title of an event
# TODO: DELETE /events/<id> - Remove an event from the list
if __name__ == "__main__":
app.run(debug=True)
Start the Flask development server:
python app.py
Test your endpoints using Postman or curl:
POST http://localhost:5000/events
- Body:
{ "title": "Hackathon" }
- Body:
PATCH http://localhost:5000/events/1
- Body:
{ "title": "Hackathon 2025" }
- Body:
DELETE http://localhost:5000/events/2
- Use RESTful nouns in routes (e.g.,
/events
) - Validate incoming JSON and handle missing keys gracefully
- Use helper functions to reduce code repetition
- Return:
201 Created
for successful POST200 OK
or204 No Content
for PATCH and DELETE404 Not Found
if a resource doesn't exist
- Include inline comments to explain logic
1. Input Validation
- Ensure the
title
field is provided. - Return a
400 Bad Request
if missing.
2. Event Not Found
- Return
404 Not Found
with a clear message when the event ID doesn't exist.
3. Reusable Logic
- Consider writing a helper function to look up events by ID.
4. Scalability
- While using a single file works here, separate concerns into modules as your API grows.
After completing this lab, you will:
✅ Know how to handle incoming JSON with Flask
✅ Build routes that implement full CRUD behavior
✅ Simulate persistent resource changes in memory
✅ Return proper HTTP status codes and structured responses
This is a critical step in your backend developer journey. Next up: persistent databases!