A simple REST API built with Flask for managing user data. This project demonstrates fundamental API development concepts including CRUD operations, HTTP methods, status codes, and JSON handling.
- Complete CRUD Operations: Create, Read, Update, Delete users
- RESTful Design: Follows REST API conventions
- JSON Support: All data exchange in JSON format
- Error Handling: Proper HTTP status codes and error messages
- Data Validation: Input validation and duplicate prevention
- In-Memory Storage: Simple dictionary-based data storage
Method | Endpoint | Description |
---|---|---|
GET | /users |
Get all users |
GET | /users/<id> |
Get user by ID |
POST | /users |
Create new user |
PUT | /users/<id> |
Update user |
DELETE | /users/<id> |
Delete user |
GET | /health |
Health check |
- Python 3.7 or higher
- Flask
-
Clone the repository
git clone https://github.com/yourusername/flask-rest-api.git cd flask-rest-api
-
Install Flask
pip install flask
-
Run the application
python app.py
-
Access the API
- Base URL:
http://localhost:5000
- Health check:
http://localhost:5000/health
- Base URL:
curl -X GET http://localhost:5000/users
Response:
{
"status": "success",
"data": [
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-15"
}
],
"count": 1
}
curl -X GET http://localhost:5000/users/1
curl -X POST http://localhost:5000/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice Johnson", "email": "alice@example.com"}'
Response:
{
"status": "success",
"message": "User created successfully",
"data": {
"id": 3,
"name": "Alice Johnson",
"email": "alice@example.com",
"created_at": "2024-09-16"
}
}
curl -X PUT http://localhost:5000/users/1 \
-H "Content-Type: application/json" \
-d '{"name": "John Updated", "email": "john.updated@example.com"}'
curl -X DELETE http://localhost:5000/users/1
- Import Collection: Create a new collection in Postman
- Set Base URL:
http://localhost:5000
- Test Each Endpoint:
- GET
/users
- No body required - POST
/users
- Set Content-Type toapplication/json
, add body with name and email - PUT
/users/1
- Same as POST, but update existing user - DELETE
/users/1
- No body required
- GET
- 200 OK: Successful GET, PUT requests
- 201 Created: Successful POST request
- 400 Bad Request: Invalid JSON or missing required fields
- 404 Not Found: User not found or invalid endpoint
- 405 Method Not Allowed: Invalid HTTP method for endpoint
- 409 Conflict: Email already exists
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-15"
}
{
"status": "success|error",
"message": "Description message",
"data": "Actual data object/array",
"count": "Number of items (for lists)"
}
- REST Architecture: Resource-based URLs, HTTP methods
- Flask Routing: Different routes for different operations
- JSON Handling:
request.get_json()
andjsonify()
- HTTP Methods: GET, POST, PUT, DELETE
- Status Codes: Appropriate codes for different scenarios
- Error Handling: Try-catch blocks and error responses
- Data Validation: Required field checking, duplicate prevention
- In-Memory Storage: List and dictionary data structures
- What is Flask? - Lightweight Python web framework
- What is REST? - Representational State Transfer architectural style
- GET vs POST? - GET retrieves data, POST creates data
- Flask Routes -
@app.route()
decorator maps URLs to functions - request.json - Accesses JSON data from HTTP request body
- Status Codes - 200 (success), 404 (not found), 201 (created), etc.
- Running Flask -
python app.py
orflask run
- JSON - JavaScript Object Notation, lightweight data format
- API Testing - Using curl, Postman, or browser dev tools
- Database Alternative - Yes, can use SQLite, PostgreSQL, MongoDB instead of in-memory storage
- Add database integration (SQLite/PostgreSQL)
- Implement authentication and authorization
- Add input sanitization and validation
- Include pagination for large datasets
- Add logging and monitoring
- Create unit tests
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and available under the MIT License.
Author: [Your Name]
Date: September 2024
Purpose: Flask REST API learning project