A full-featured Instagram-like backend REST API built with Python Flask framework, featuring real-time chat, stories, posts, and comprehensive social media functionality.
- User Registration & Login: Email/phone-based authentication with JWT tokens
- Profile Management: Update profile pictures, personal information, and settings
- Password Management: Forgot password with OTP verification, password reset
- Account Security: JWT token management with blocklist support
- User Validation: Email, phone, and username validation
- Post Creation: Upload multiple images with captions and location
- Hashtag Support: Automatic hashtag parsing and trending
- Post Interactions: Like, unlike, save, and unsave posts
- Image Processing: Automatic image compression and optimization
- Post Management: Edit, delete, and retrieve user posts
- Follow System: Follow/unfollow users with activity tracking
- Feed Generation: Personalized feed based on following relationships
- Explore Page: Discover new content from non-followed users
- Activity Tracking: Comprehensive activity logging for social interactions
- Story Creation: Upload and share 24-hour stories
- Story Views: Track who viewed your stories
- Story Discovery: View stories from followed users
- Story Management: Automatic expiration after 24 hours
- Comment System: Add comments to posts with pagination
- Reply Comments: Nested comment replies
- Comment Likes: Like/unlike comments and replies
- Comment Management: Edit and delete comments
- Direct Messaging: Private conversations between users
- Chat Rooms: Create and manage chat sessions
- Real-time Updates: WebSocket-based instant messaging
- Message History: Persistent chat storage and retrieval
- Activity Feed: Track all social interactions
- Push Notifications: Firebase integration for mobile notifications
- Real-time Updates: Live notification delivery
- PostgreSQL Database: Robust relational database with SQLAlchemy ORM
- Database Migrations: Alembic-based schema management
- Background Tasks: Celery integration for async operations
- Image Optimization: Automatic image compression and processing
- RESTful API: Comprehensive API documentation with Swagger/OpenAPI
- JWT Authentication: Secure token-based authentication
- File Upload: Secure file handling with validation
- Pagination: Efficient data retrieval with pagination support
- Error Handling: Comprehensive error handling and validation
- API Documentation: Auto-generated Swagger UI documentation
- Backend Framework: Flask 3.0.0
- Database: PostgreSQL with SQLAlchemy ORM
- Authentication: JWT with Flask-JWT-Extended
- Real-time: Flask-SocketIO for WebSocket support
- Background Tasks: Celery for async operations
- File Processing: Pillow for image manipulation
- API Documentation: Flask-Smorest with OpenAPI/Swagger
- Push Notifications: Firebase Admin SDK
- Database Migrations: Alembic
- Image Storage: Local file system with compression
Before running this application, make sure you have:
- Python 3.8+ installed
- PostgreSQL database server running
- Redis server (for Celery background tasks)
- Firebase Admin SDK credentials file (
firebase-adminsdk.json)
git clone <repository-url>
cd instagram-flask-mainpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the root directory with the following variables:
# Flask Configuration
FLASK_APP=app
FLASK_DEBUG=True
# Database Configuration
DATABASE_URL=postgresql://username:password@localhost:5432/instagram_db
# JWT Configuration
JWT_SECRET_KEY=your-secret-key-here
JWT_ACCESS_TOKEN_EXPIRES=15m
JWT_REFRESH_TOKEN_EXPIRES=30d
# API Configuration
API_TITLE=Instagram Flask API
API_VERSION=1.0.0
OPENAPI_VERSION=3.0.2
OPENAPI_URL_PREFIX=/
# Celery Configuration
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0
# Firebase Configuration
FIREBASE_CREDENTIALS_PATH=firebase-adminsdk.json# Create PostgreSQL database
createdb instagram_db
# Run database migrations
flask db upgrade- Download your Firebase Admin SDK service account key
- Rename it to
firebase-adminsdk.json - Place it in the root directory
redis-servercelery -A app.celery worker --loglevel=infoflask runpython -c "from app import create_app; from socket_utils import mysocket; app = create_app(); mysocket.run(app, debug=True)"POST /signup- User registrationPOST /login- User loginPOST /logout- User logoutPOST /refresh- Refresh JWT tokenPOST /forgot-password- Request password resetPOST /verify-otp- Verify OTP for password resetPOST /reset-password- Reset password with OTP
GET /profile- Get user profilePUT /profile- Update user profilePUT /profile-pic- Update profile picturePOST /change-password- Change passwordGET /username-check/<username>- Check username availability
POST /posts- Create a new postGET /posts- Get user postsGET /post/<post_id>- Get specific postDELETE /post/<post_id>- Delete postPOST /post/<post_id>/like- Like a postDELETE /post/<post_id>/like- Unlike a postPOST /post/<post_id>/save- Save a postDELETE /post/<post_id>/save- Unsave a post
POST /stories- Create a new storyGET /stories- Get feed storiesGET /my-stories- Get user's storiesGET /user-stories/<user_id>- Get specific user's storiesPOST /story/<story_id>/view- Mark story as viewed
POST /post_comment/<post_id>- Add comment to postGET /post_comment/<post_id>- Get post commentsPUT /comment/<comment_id>- Edit commentDELETE /comment/<comment_id>- Delete commentPOST /comment/<comment_id>/like- Like a commentDELETE /comment/<comment_id>/like- Unlike a comment
POST /reply_comment/<comment_id>- Reply to a commentGET /reply_comment/<comment_id>- Get comment repliesPUT /replies/<reply_id>- Edit replyDELETE /replies/<reply_id>- Delete replyPOST /reply/<reply_id>/like- Like a replyDELETE /reply/<reply_id>/like- Unlike a reply
POST /follow/<user_id>- Follow a userDELETE /unfollow/<user_id>- Unfollow a userGET /follow/<user_id>- Get user's following list
GET /feed- Get personalized feedGET /explore- Get explore page contentGET /search- Search users and hashtagsGET /activity- Get user activity feed
GET /viewchats- Get user's chat roomsPOST /newchat/<user_id>- Create new chat roomGET /chat/<room_id>- Get chat messagesPOST /chat/<room_id>- Send message
- PostgreSQL: Configure connection string in
.env - Migrations: Use
flask dbcommands for schema management
- Token Expiry: Configure access and refresh token lifetimes
- Secret Key: Set secure JWT secret key
- Supported Formats: Images (JPEG, PNG, GIF)
- File Size Limits: Configurable upload limits
- Storage Path: Local file system with compression
- Broker: Redis for task queue
- Workers: Configure number of worker processes
- Task Timeouts: Set appropriate task timeouts
# Install test dependencies
pip install pytest pytest-flask
# Run tests
pytest- Use the built-in Swagger UI at
/swagger-uifor API testing - Import the OpenAPI specification into tools like Postman
- Environment Variables: Set production environment variables
- Database: Use production PostgreSQL instance
- Redis: Configure production Redis server
- Web Server: Use Gunicorn with proper WSGI configuration
- Static Files: Configure proper static file serving
- Security: Enable HTTPS and security headers
# Build Docker image
docker build -t instagram-flask .
# Run with Docker Compose
docker-compose up -d- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the API documentation at
/swagger-ui - Review the error logs
- Check database connectivity
- Verify environment configuration
- Open an issue on GitHub
- Video Support: Add video upload and processing
- Live Streaming: Real-time video streaming capabilities
- Advanced Analytics: User engagement and content analytics
- Machine Learning: Content recommendation algorithms
- Microservices: Break down into microservice architecture
- GraphQL: Add GraphQL API support
- Mobile SDK: Native mobile application SDKs
Note: This is a comprehensive Instagram-like backend API. Make sure to configure all required services (PostgreSQL, Redis, Firebase) before running the application.