A complete REST API built with Flask featuring JWT token authentication, user management, and CRUD operations for stores, items, and tags.
- 🔐 JWT Authentication - Secure access and refresh tokens
- 👤 User Management - Registration, login, and account management
- 🏪 Store Management - Create and manage multiple stores per user
- 📦 Item Management - Add products to stores with pricing
- 🏷️ Tag System - Categorize stores with custom tags
- 🗑️ Cascade Deletion - Automatic cleanup of related resources
- 🔒 Authorization - Users can only access their own resources
- Flask - Web framework
- SQLAlchemy - ORM for database operations
- Flask-JWT-Extended - JWT token management
- Marshmallow - Data validation and serialization
- SQLite - Development database (PostgreSQL ready for production)
- Docker - Containerization support
flask_api/
├── app/
│ ├── __init__.py # App factory and JWT setup
│ ├── models.py # Database models
│ ├── schemas.py # Marshmallow schemas
│ └── resources/ # API endpoints
│ ├── users.py # User routes
│ ├── stores.py # Store routes
│ ├── items.py # Item routes
│ └── tags.py # Tag routes
├── config.py # Configuration settings
├── run.py # Application entry point
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose setup
└── INSOMNIA_TESTING_GUIDE_*.txt # Complete testing guides
-
Clone the repository
git clone https://github.com/v1Rtu3-h05t/flask-rest-api-jwt.git cd flask-rest-api-jwt -
Create virtual environment
python -m venv venv
-
Activate virtual environment
- Windows:
.\venv\Scripts\activate - Mac/Linux:
source venv/bin/activate
- Windows:
-
Install dependencies
pip install -r requirements.txt
-
Run the application
python run.py
-
Access the API
- Server runs at:
http://127.0.0.1:5000
- Server runs at:
-
Build and run with Docker Compose
docker-compose up --build
-
Access the API
- Server runs at:
http://localhost:5000 - Uses PostgreSQL database
- Server runs at:
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /user/register |
None | Create new user account |
| POST | /user/login |
None | Login and get tokens |
| POST | /user/logout |
Access Token | Logout and revoke token |
| POST | /user/refresh |
Refresh Token | Get new access token |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| GET | /user/<id> |
Access Token | Get user details |
| DELETE | /user/<id> |
Access Token | Delete user account |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /store/ |
Access Token | Create new store |
| GET | /store/<id> |
Access Token | Get store by ID |
| GET | /store/s |
Access Token | Get all user's stores |
| DELETE | /store/<id> |
Access Token | Delete store |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /item/ |
Access Token | Create new item |
| GET | /item/<id> |
Access Token | Get item by ID |
| GET | /item/s |
Access Token | Get all user's items |
| PUT | /item/<id> |
Access Token | Update item |
| DELETE | /item/<id> |
Access Token | Delete item |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /tag/store/<store_id> |
Access Token | Create tag for store |
| GET | /tag/<id> |
Access Token | Get tag by ID |
| GET | /tag/store/<store_id>/s |
Access Token | Get all tags in store |
| DELETE | /tag/<id> |
Access Token | Delete tag |
This API uses JWT (JSON Web Tokens) for authentication:
- Access Token: Short-lived (15 minutes) - used for API requests
- Refresh Token: Long-lived (30 days) - used to get new access tokens
Authorization: Bearer <your_access_token>
POST http://127.0.0.1:5000/user/register
Content-Type: application/json
{
"username": "johndoe",
"password": "secure_password"
}POST http://127.0.0.1:5000/user/login
Content-Type: application/json
{
"username": "johndoe",
"password": "secure_password"
}Response:
{
"access_token": "eyJ0eXAiOiJKV1Qi...",
"refresh_token": "eyJ0eXAiOiJKV1Qi..."
}POST http://127.0.0.1:5000/store/
Authorization: Bearer <access_token>
Content-Type: application/json
{
"name": "My Store"
}POST http://127.0.0.1:5000/item/
Authorization: Bearer <access_token>
Content-Type: application/json
{
"name": "Product Name",
"price": 29.99,
"store_id": 1
}For detailed step-by-step testing instructions, see:
INSOMNIA_TESTING_GUIDE_PART1.txt- Authentication, Stores, and ItemsINSOMNIA_TESTING_GUIDE_PART2.txt- Tags, Token Management, Troubleshooting, and Pro Tips
These guides include:
- Complete testing workflow
- Example requests and responses
- Common errors and solutions
- Best practices and pro tips
- Start the server
- Register a user:
POST /user/register - Login:
POST /user/login→ Save tokens - Create a store:
POST /store/(use access token) - Create an item:
POST /item/(use access token)
id: Integer (Primary Key)username: String (Unique)password_hash: String- Relationships: One-to-Many with Stores
id: Integer (Primary Key)name: Stringuser_id: Integer (Foreign Key)- Relationships: One-to-Many with Items and Tags
id: Integer (Primary Key)name: Stringprice: Floatstore_id: Integer (Foreign Key)
id: Integer (Primary Key)name: Stringstore_id: Integer (Foreign Key)
SECRET_KEY: Secret key for JWT tokens (default: 'your-secret-key-change-me')DATABASE_URL: Database connection string (PostgreSQL for production)DEV_DATABASE_URL: Development database (SQLite by default)
- Access Token: 15 minutes
- Refresh Token: 30 days
- ✅ Password hashing with Werkzeug
- ✅ JWT token authentication
- ✅ Token blacklist for logout
- ✅ User authorization checks
- ✅ Passwords never returned in responses
- ✅ Users can only access their own resources
Connection Refused
- Make sure Flask server is running:
python run.py - Check the URL:
http://127.0.0.1:5000
401 Unauthorized
- Check token format:
Bearer <token>(note the space) - Token may have expired (15 min for access tokens)
- Use refresh token to get a new access token
404 Not Found
- Verify the resource ID exists
- Check endpoint spelling
For more detailed troubleshooting, see the testing guides.
- Many-to-many relationship between Items and Tags
- Search and filter functionality
- Pagination for large datasets
- Rate limiting
- API documentation with Swagger/OpenAPI
- Email verification
- Password reset functionality
- Admin user roles
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit changes:
git commit -m 'Add feature' - Push to branch:
git push origin feature-name - Submit a pull request
This project is open source and available under the MIT License.
Anthony Morales
For complete testing instructions and examples, please refer to the included testing guides:
INSOMNIA_TESTING_GUIDE_PART1.txtINSOMNIA_TESTING_GUIDE_PART2.txt