A backend system (Api system) for managing events and bookings, built using Flask and SQLAlchemy. This project focuses on clean architecture, authentication, authorization, and real-world backend patterns rather than UI.
The system supports user authentication, role-based access control, event ownership, secure booking workflows, and integrations with an external payment microservice.
- Python
- Flask
- Flask-JWT-Extended
- Flask-Migrate
- SQLAlchemy
- MySQL as database
- HTTP Requests (for Service-to-Service communication)
- FastAPI (External Payment Microservice integration)
- JWT-based authentication (access tokens)
- Secure user registration and login
- Protected routes using JWT decorators
- Token-based user identity handling
- Two roles for users (user, organiser)
- Role checks for sensitive operations
- Organizer-only routes
- Only organisers can create, update, and delete events
- Only event organisers can modify/delete their own events
- Authenticated access to view events
- Ownership checks enforced at important changes
- Only users with the 'user' role can book events (limited to 1 seat per transaction)
- Users can view and cancel their own bookings (with ownership checks)
- Event organisers can view bookings for their events
- Synchronous booking validation with transaction-safe seat decrement
- Integrated payment request creation sent directly to the payment microservice
- Outbound API calls to initialize payments on creation
- Inbound callback validation using a shared API key (
X_API_KEY) - Real-time confirmation of transactions (
CAPTUREDpayments confirm bookings) - Auto-handling of failed or expired payment attempts
- SQLAlchemy ORM for database modeling
- Flask-Migrate for schema migrations
- Clean separation of models and business logic
- RESTful API structure
- Modular route blueprints
- Clear request and response formats
- Proper HTTP status codes and error handling
- Simultaneous booking requests are serialized using SQLAlchemy's
with_for_update()which places a write-lock (SELECT ... FOR UPDATEin MySQL) on the event row. - This prevents race conditions, overbooking, and double-bookings when multiple users click the register button at the exact same millisecond.
- When a user tries to book, seats are temporarily marked as
PENDING_PAYMENTfor 15 minutes to allow time for the payment process on the microservice. - An automated
BookingCleanupServiceis provided to check and release expired holds, returning the seats back to their events. - This maintains high data consistency and prevents seats from being locked indefinitely.
- Direct, synchronous REST communication from the Flask app to the FastAPI payment microservice via
requests.postwith built-in timeouts. - Microservice calls back the Flask system asynchronously using secure token verification (
X_API_KEYheader) to update payment status and finalize bookings.
- User roles (user, organiser) are saved directly in the JWT as additional claims along with the user ID.
- This allows the backend to perform stateless, highly secure role checks on protected routes without hitting the database on every single request.
- The
/Eventsfeed endpoint utilizes server-side pagination with SQLAlchemy. - It returns events in chunks (page-by-page) which keeps response times fast and reduces server memory overhead.
- Built-in Cross-Origin Resource Sharing (CORS) security is configured.
- This makes sure only trusted frontend origins can communicate with our API and prevents external malicious domains from triggering endpoints.
- User registration (roles are chosen here, either a user or an organiser)
- User login
- JWT token generation (access and refresh tokens with clean claims separation on refresh)
- JWT tokens will save user Id as identity and users role as additional claim for future ownership and role checks
- First, user roles are checked (either user or organiser), and only organisers are allowed to access the features below
- Create event
- Update/Delete event (accessed only by the organiser who created that event)
- Get all events (accessed by all authenticated users)
- First, user roles are checked, and only users with the 'user' role are allowed to access the features below
- Book an event
- View user bookings
- View bookings for an event organiser
- Cancel a booking (restricted to the owner of the booking)
- Secure endpoints matching the shared
X_API_KEYheader:POST /internal/booking/<int:booking_id>/payment-created: Used by the payment microservice to log payment initialization details.POST /internal/bookings/<int:booking_id>/payment-confirmation: Confirms status changes (sets booking toCONFIRMEDonCAPTUREDpayments, or reverts to pending/failed).
- Clone the repo.
- Create and activate a virtual environment.
- Install Dependencies through this cli command
pip install -r requirments.txt- set environment variables (SECRET_KEY, JWT_SECRET_KEY, DATABASE_URL, API_KEY, PAYMENT_URL)
- Initialize database through migrations cli commands
flask db init
flask db migrate
flask db upgrade- Runserver
cd Backend
python app.py- Strengthen backend fundamentals
- Practice authentication and authorization
- Understand real-world API design
- Prepare for backend and AI/ML system deployment work
- Filtering and search functionalities
- Rate limiting to protect API endpoints
- Containerization & deployment using Docker
- Unit and integration testing for payment webhooks
Atharva Manale aka Apex Levo
