Skip to main content
Before connecting to a real database, building a CRUD API backed by in-memory Python dictionaries is the fastest way to master FastAPI’s routing, validation, and response model patterns without any database overhead. In this lesson, you’ll implement all five standard CRUD operations — list, retrieve, create, update, and delete — for an Employee Management System. Every pattern you learn here translates directly to database-backed APIs later.

In-Memory Data Flow

Since you’re not using a database yet, your application’s state lives in the server’s RAM. Incoming HTTP requests read from or write to a global Python dictionary:
Because this state lives in RAM, restarting Uvicorn or triggering a hot-reload will wipe the in-memory state and reset data to its initial mock values. This is expected behaviour at this stage — you’ll replace it with database persistence in a later lesson.

Setting Up the Pydantic Schemas

Good schema design separates incoming data (EmployeeCreate), partial updates (EmployeeUpdate), and the outgoing response (EmployeeOut):

The Complete CRUD Implementation

Create a file named ems_app.py in your project folder:

Running and Testing the API

1

Start the server

2

Open Swagger UI

Navigate to http://127.0.0.1:8000/docs in your browser to see all five endpoints listed and interactive.
3

Test the full CRUD lifecycle

  1. POST /employees — add a new employee. You should receive a 201 Created response with the new record and its assigned id.
  2. GET /employees — verify the new employee appears in the list.
  3. GET /employees/1 — retrieve a single employee by ID.
  4. PUT /employees/1 — update the employee’s department. Only send the field(s) you want to change.
  5. DELETE /employees/2 — remove an employee. You should receive a 204 No Content with no body.

Key Patterns to Remember

The exclude_unset=True argument to model_dump() is essential for PUT and PATCH endpoints. Without it, any optional field the client didn’t send would be treated as None and could overwrite existing data.

Endpoint Reference