A simple social networking service API built with FastAPI that allows users to create, retrieve, update, and delete posts; add comments; and like/unlike posts.
- Post Management: Create, read, update, delete posts
- Comment System: Add, view, update, delete comments on posts
- Like System: Like and unlike posts
- SQLite Database: Persistent storage with automatic initialization
- OpenAPI Documentation: Swagger UI and ReDoc interfaces
- CORS Support: Enabled for all origins
python/
├── main.py # FastAPI application with all endpoints
├── models.py # Pydantic models for request/response validation
├── database.py # SQLite database operations and CRUD functions
├── requirements.txt # Python dependencies
├── sns_api.db # SQLite database (created automatically)
└── .venv/ # Python virtual environment
-
Create and activate virtual environment:
cd python python3 -m venv .venv source .venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
python main.py
The API will be available at http://localhost:8000
GET /api/posts- List all postsPOST /api/posts- Create a new postGET /api/posts/{postId}- Get a specific postPATCH /api/posts/{postId}- Update a postDELETE /api/posts/{postId}- Delete a post
GET /api/posts/{postId}/comments- List comments for a postPOST /api/posts/{postId}/comments- Create a commentGET /api/posts/{postId}/comments/{commentId}- Get a specific commentPATCH /api/posts/{postId}/comments/{commentId}- Update a commentDELETE /api/posts/{postId}/comments/{commentId}- Delete a comment
POST /api/posts/{postId}/likes- Like a postDELETE /api/posts/{postId}/likes- Unlike a post
FastAPI automatically generates interactive API documentation in multiple formats:
URL: http://localhost:8000/docs
Swagger UI is an interactive documentation interface that allows you to:
- 📖 Browse all available API endpoints
- 🧪 Test API calls directly from your browser (Try it out!)
- 📝 View request/response schemas and examples
- 🔍 See detailed parameter descriptions
- ✅ Execute API calls and see real responses
Use Case: Perfect for developers who want to quickly test and explore the API without writing code.
URL: http://localhost:8000/redoc
ReDoc is a clean, reading-focused documentation interface that provides:
- 📚 A more polished, professional documentation layout
- 📊 Better for sharing with non-technical stakeholders
- 🎨 Clean, three-panel layout for easy navigation
- 📖 Optimized for reading and understanding the API structure
- 🖨️ Print-friendly documentation
Use Case: Ideal for creating user-facing documentation or sharing API specifications with clients/teams.
URL: http://localhost:8000/openapi.json
The OpenAPI specification in JSON format:
- 🔧 Machine-readable API specification (OpenAPI 3.0.1 standard)
- 🤖 Used by tools to generate client SDKs, tests, and documentation
- 📋 Contains complete API contract: endpoints, schemas, responses, examples
- 🔄 Can be imported into tools like Postman, Insomnia, or API testing frameworks
- ✅ This endpoint serves the exact same specification as defined in
openapi.yaml
Use Case: For programmatic access, code generation tools, API testing tools, or importing into other documentation platforms.
Quick Comparison:
| Feature | Swagger UI (/docs) |
ReDoc (/redoc) |
OpenAPI JSON (/openapi.json) |
|---|---|---|---|
| Interactive Testing | ✅ Yes | ❌ No | ❌ No |
| Clean UI | ✅ Good | ✅ Excellent | N/A |
| Machine Readable | ❌ No | ❌ No | ✅ Yes |
| Best For | Testing & Exploration | Reading & Sharing | Tools & Integration |
curl -X POST http://localhost:8000/api/posts \
-H "Content-Type: application/json" \
-d '{"username": "john_doe", "content": "Hello, World!"}'curl http://localhost:8000/api/postscurl -X POST http://localhost:8000/api/posts/1/comments \
-H "Content-Type: application/json" \
-d '{"username": "jane_smith", "content": "Great post!"}'curl -X POST http://localhost:8000/api/posts/1/likes \
-H "Content-Type: application/json" \
-d '{"username": "alice"}' ✅ FastAPI Framework: High-performance web framework
✅ SQLite Database: Lightweight, file-based database with automatic initialization
✅ Exact OpenAPI Compliance: Serves the exact OpenAPI 3.0.1 specification from openapi.yaml
✅ CORS Enabled: Configured to allow all origins for development
✅ Port 8000: Application runs on the specified port
✅ Pydantic Validation: Request/response validation with detailed error messages
✅ Database Relationships: Foreign keys and referential integrity
✅ Automatic Counts: Post like_count and comment_count updated automatically
✅ Error Handling: Comprehensive error responses with proper HTTP status codes
✅ Clean Architecture: Separated concerns with models, database operations, and API routes