Flask REST API with JWT
A notes API with user accounts, JWT login and per-user data, structured like a production Flask app.
What you'll learn
- βFlask blueprints and the app factory
- βSQLAlchemy models and migrations
- βJWT authentication
What you'll build
- βRegister and login
- βCRUD for your own notes only
- βValidation and JSON errors
- βTests with pytest
Step-by-step guide
- 1
Install
pip install flask flask-sqlalchemy flask-migrate flask-jwt-extended.
- 2
App factory
create_app() configures the database and JWT secret from environment variables and registers auth and notes blueprints.
- 3
Models
User(id, email, password_hash) using werkzeug.security generate_password_hash; Note(id, user_id, title, body, created_at).
- 4
Migrations
flask db init, flask db migrate, flask db upgrade.
- 5
Auth routes
POST /auth/register and /auth/login; on login return create_access_token(identity=str(user.id)).
- 6
Notes routes
@jwt_required() on every route; always filter Note.query by user_id=get_jwt_identity() so users never see each other's notes.
- 7
Errors and tests
Register error handlers that return JSON; write pytest tests with app.test_client() for 401, 404 and the happy path.
π Make it stand out
- +Refresh tokens
- +Rate limiting with Flask-Limiter
- +OpenAPI docs