A sample REST API built with Flask, demonstrating typical REST principles similar to the PetStore API. This API provides a complete CRUD interface for managing books and authors with comprehensive OpenAPI/Swagger documentation.
- ✅ Full CRUD operations (Create, Read, Update, Delete)
- ✅ Multiple HTTP verbs (GET, POST, PUT, PATCH, DELETE)
- ✅ RESTful design principles
- ✅ Query parameter filtering
- ✅ OpenAPI 3.0 specification
- ✅ Interactive Swagger UI documentation
- ✅ CORS enabled
- ✅ Proper HTTP status codes
- ✅ In-memory data storage
Manage a collection of books with the following operations:
GET /api/v1/books- List all books (with optional filtering)GET /api/v1/books/{id}- Get a specific bookPOST /api/v1/books- Create a new bookPUT /api/v1/books/{id}- Update a book (full replacement)PATCH /api/v1/books/{id}- Partially update a bookDELETE /api/v1/books/{id}- Delete a book
Manage a collection of authors:
GET /api/v1/authors- List all authorsGET /api/v1/authors/{id}- Get a specific authorPOST /api/v1/authors- Create a new authorPUT /api/v1/authors/{id}- Update an authorDELETE /api/v1/authors/{id}- Delete an authorGET /api/v1/authors/{id}/books- Get all books by an author
GET /api/v1/health- Check API health status
- Python 3.7 or higher
- pip (Python package manager)
- Clone the repository:
git clone https://github.com/bookernath/sample-api.git
cd sample-api- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtStart the Flask development server:
python app.pyThe API will be available at: http://localhost:5000
For development with debug mode enabled (provides detailed error messages and auto-reload):
FLASK_DEBUG=True python app.pyWarning: Never use debug mode in production as it exposes the interactive debugger which can be a security risk.
Once the server is running, access the interactive Swagger UI documentation at:
http://localhost:5000/api/docs
The OpenAPI specification is also available as a standalone file: openapi.yaml
curl http://localhost:5000/api/v1/bookscurl http://localhost:5000/api/v1/books/1curl "http://localhost:5000/api/v1/books?min_price=10&max_price=15"curl -X POST http://localhost:5000/api/v1/books \
-H "Content-Type: application/json" \
-d '{
"title": "The Catcher in the Rye",
"author_id": "1",
"isbn": "978-0-316-76948-0",
"published_year": 1951,
"price": 11.99,
"stock": 50
}'curl -X PUT http://localhost:5000/api/v1/books/1 \
-H "Content-Type: application/json" \
-d '{
"title": "The Great Gatsby - Updated",
"author_id": "1",
"isbn": "978-0-7432-7356-5",
"published_year": 1925,
"price": 15.99,
"stock": 30
}'curl -X PATCH http://localhost:5000/api/v1/books/1 \
-H "Content-Type: application/json" \
-d '{
"price": 9.99,
"stock": 100
}'curl -X DELETE http://localhost:5000/api/v1/books/1curl http://localhost:5000/api/v1/authors/1/books- Resource-Based URLs: Clear, noun-based endpoints (
/books,/authors) - HTTP Methods: Proper use of GET, POST, PUT, PATCH, DELETE
- Status Codes: Appropriate HTTP status codes (200, 201, 204, 400, 404)
- Stateless: Each request contains all necessary information
- JSON Format: Standard JSON for request/response bodies
- Query Parameters: Filtering and pagination support
- Relationships: Author-to-books relationship endpoint
- Idempotency: PUT and DELETE operations are idempotent
- GET: Retrieve resource(s) - Safe and idempotent
- POST: Create new resource - Not idempotent
- PUT: Replace entire resource - Idempotent
- PATCH: Partially update resource - Not necessarily idempotent
- DELETE: Remove resource - Idempotent
The API comes pre-loaded with sample data:
Books:
- The Great Gatsby by F. Scott Fitzgerald
- To Kill a Mockingbird by Harper Lee
- 1984 by George Orwell
Authors:
- F. Scott Fitzgerald
- Harper Lee
- George Orwell
sample-api/
├── app.py # Main Flask application
├── requirements.txt # Python dependencies
├── openapi.yaml # OpenAPI 3.0 specification
├── README.md # This file
└── .gitignore # Git ignore rules
- Flask: Lightweight WSGI web application framework
- Flask-CORS: Handle Cross-Origin Resource Sharing
- Flasgger: Flask extension for automatic Swagger UI generation
To add a new resource:
- Define the data model in the in-memory store
- Create CRUD endpoints following REST conventions
- Add OpenAPI documentation in docstrings
- Update the OpenAPI YAML file
Currently, this API uses in-memory storage. Data is lost when the server restarts. For production use, consider integrating:
- SQLite for simple file-based persistence
- PostgreSQL/MySQL for full-featured database
- MongoDB for document-based storage
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For questions or support, please open an issue on GitHub.