An Instagram-style REST API built with FastAPI, SQLAlchemy, and SQLite.
This project provides user authentication, posts, comments, image uploads, JWT-based authorization, and relational database management.
- 🔐 User registration and JWT authentication
- 🔑 Password hashing with
pwdlib - 👤 User management
- 📝 Create and delete posts
- 💬 Create, retrieve, and delete comments
- 🖼️ Image/file uploads
- 📁 Static file serving
- 🛡️ Protected endpoints using OAuth2 Bearer tokens
- 🗄️ SQLAlchemy ORM
- 🔗 User, post, and comment relationships
- 📦 Pydantic request and response schemas
- 💾 SQLite database
- Python 3
- FastAPI
- SQLAlchemy
- SQLite
- Pydantic
- JWT
- OAuth2
- pwdlib
- Uvicorn
fastapi-instagram-api/
│
├── auth/
│ ├── authentication.py
│ └── oauth2.py
│
├── db/
│ ├── database.py
│ ├── models.py
│ ├── db_user.py
│ ├── db_post.py
│ ├── db_comment.py
│ └── hash.py
│
├── routers/
│ ├── user.py
│ ├── post.py
│ └── comment.py
│
├── uploaded_file/
│
├── main.py
├── schemas.py
├── requirements.txt
├── .gitignore
└── README.md
The API uses OAuth2 Bearer authentication with JWT access tokens.
The authentication flow is:
User Login
↓
Validate Credentials
↓
Generate JWT
↓
Client Sends Bearer Token
↓
Validate JWT
↓
Get Current User
↓
Access Protected Endpoint
Passwords are hashed before being stored in the database.
The project uses SQLAlchemy ORM to model the relationships between users, posts, and comments.
User
│
├── 1 ──── N ──── Post
│ │
│ └── 1 ──── N ──── Comment
│
└── 1 ──── N ──── Comment
User
idusernameemailpassword
Post
idimage_urlimage_url_typecaptiontimestampuser_id
Comment
idtexttimestampuser_idpost_id
git clone https://github.com/ARK13-org/fastapi-instagram-api.git
cd fastapi-instagram-apipython -m venv .venvActivate it on Windows:
.venv\Scripts\Activate.ps1pip install -r requirements.txtuvicorn main:app --reloadThe API will be available at:
http://127.0.0.1:8000
Interactive API documentation:
http://127.0.0.1:8000/docs
POST /user
Create a new user.
POST /token
Authenticate a user and receive a JWT access token.
POST /post/create_post
GET /post/
POST /post/delete/{id}
POST /post/upload_file
POST /comment/create_comment
GET /comment/{id}
POST /comment/delete/{id}
The API supports uploading image files.
Uploaded files are stored in the:
uploaded_file/
directory and exposed through FastAPI's static file handling.
This project helped me practice:
- Building REST APIs with FastAPI
- Dependency injection with
Depends - OAuth2 Bearer authentication
- JWT access tokens
- Password hashing and verification
- SQLAlchemy ORM
- Foreign keys and relationships
- Pydantic schemas
- CRUD operations
- File uploads with
UploadFile - Static file serving
- API response models
- Organizing a FastAPI project into routers, database logic, authentication, and schemas
- PostgreSQL support
- Alembic database migrations
- Refresh tokens
- Better authentication and authorization
- User profile endpoints
- Like and follow functionality
- Pagination
- Search and filtering
- Image validation and processing
- Automated tests with Pytest
- Docker support
- Environment-based configuration
- Improved error handling
This project was built as a practical backend project to learn how different FastAPI components work together in a real-world style application.
It focuses on API design, authentication, database relationships, and backend architecture rather than reproducing the complete Instagram platform.