# API Design Specification

---

**Document Version:** 1.0
**Project:** Student Bazaar
**Document Type:** API Design Specification
**Status:** Approved
**Last Updated:** 28 June 2026
**Author:** Nemmi Tangha

---

# 1. Purpose

This document defines the API design standards for Student Bazaar.

Student Bazaar is developed using the Flask framework and follows a hybrid architecture that combines server-side rendered HTML pages with JSON responses for dynamic functionality.

The purpose of this document is to establish consistent standards for URL design, request handling, responses, authentication, authorization, validation, and communication between application modules.

---

# 2. Application Architecture

Student Bazaar follows a **Hybrid Flask Architecture**.

The application primarily renders HTML pages using Jinja2 templates while using JSON responses for asynchronous (AJAX) operations.

Application flow:

```text
Browser
    │
    ▼
Flask Blueprint
    │
    ▼
Route
    │
    ▼
Service Layer
    │
    ▼
Database Model
    │
    ▼
MySQL Database
    │
    ▼
HTML Template or JSON Response
```

Business logic must remain inside the Service Layer.

Routes are responsible only for:

* Receiving requests
* Validating basic input
* Calling services
* Returning HTML pages or JSON responses

---

# 3. API Modules

The application is organized into feature-based modules.

| Module         | Responsibility                                 |
| -------------- | ---------------------------------------------- |
| Authentication | Registration, Login, Logout, Password Recovery |
| Marketplace    | Listings, Categories, Favorites                |
| Offers         | Price Negotiation                              |
| Chat           | Conversations and Messages                     |
| Meet-up        | Meet-up Scheduling                             |
| Notifications  | User Notifications                             |
| Premium        | Premium Membership                             |
| Advertisements | Banner and Rewarded Advertisements             |
| Admin          | Platform Administration                        |

Each module is implemented using an independent Flask Blueprint.

---

# 4. URL Structure

Student Bazaar uses clean, descriptive URLs.

Public Pages

```text
/
```

```text
/login
```

```text
/register
```

```text
/about
```

```text
/contact
```

---

Application Pages

```text
/app/dashboard
```

```text
/app/listings
```

```text
/app/listings/{public_id}
```

```text
/app/offers
```

```text
/app/chat
```

```text
/app/meetups
```

```text
/app/notifications
```

---

Administration

```text
/admin
```

```text
/admin/users
```

```text
/admin/reports
```

```text
/admin/advertisements
```

---

Future Public API

```text
/api/v1/
```

Reserved for future integrations such as mobile applications or third-party services.

---

# 5. URL Design Rules

The application follows these standards:

* Lowercase URLs
* Hyphen-separated words when required
* Noun-based endpoints
* UUID public identifiers in URLs
* Consistent naming across modules

---

# 6. HTTP Methods

| Method | Purpose                |
| ------ | ---------------------- |
| GET    | Retrieve resources     |
| POST   | Create resources       |
| PUT    | Replace resources      |
| PATCH  | Update specific fields |
| DELETE | Soft delete resources  |

Examples

GET

Retrieve Listings

POST

Create Listing

PATCH

Update Listing Status

DELETE

Soft Delete Listing

---

# 7. Request Validation

Every request must be validated before processing.

Validation includes:

* Required fields
* Email format
* Password requirements
* Numeric validation
* String length
* File validation
* Image validation
* Authorization checks

Validation occurs before business logic execution.

---

# 8. Response Types

Student Bazaar supports two response types.

## HTML Responses

Most browser requests return rendered HTML pages.

Examples include:

* Homepage
* Login
* Registration
* Marketplace
* Product Details
* Seller Dashboard
* Admin Dashboard

---

## JSON Responses

AJAX requests return JSON.

Examples include:

* Chat
* Notifications
* Live Search
* Favorites
* Offers
* Listing Updates

This approach provides a responsive user experience while maintaining a traditional Flask application.

---

# 9. JSON Response Format

Successful responses

```json
{
    "success": true,
    "message": "Operation completed successfully.",
    "data": {}
}
```

---

Error responses

```json
{
    "success": false,
    "message": "Validation failed.",
    "errors": {}
}
```

Every JSON response should follow a consistent structure.

---

# 10. Authentication

Authentication features include:

* Registration
* Login
* Logout
* Email Verification
* Forgot Password
* Password Reset

Flask-Login is used for session management.

Passwords are never returned in responses.

---

# 11. Authorization

Student Bazaar supports two roles.

Student

Can:

* Buy Products
* Sell Products
* Chat
* Submit Offers
* Schedule Meet-ups

---

Super Admin

Can:

* Manage Users
* Review Reports
* Moderate Listings
* Manage Advertisements
* Configure System Settings

Authorization is required before every protected operation.

---

# 12. File Upload APIs

Supported uploads include:

* Profile Pictures
* Listing Images
* Advertisement Images

Upload Rules

* Validate MIME type
* Validate file extension
* Validate file size
* Generate unique filenames
* Store uploads outside application code

Listing images:

Minimum

1

Maximum

7

---

# 13. Pagination

Large datasets use pagination.

Examples:

* Listings
* Messages
* Notifications
* Reports

Standard Parameters

```text
?page=1
&per_page=20
```

Default page size:

20

---

# 14. Filtering

Marketplace supports filtering by:

* Category
* Condition
* Price
* Listing Status
* Negotiable
* Featured

Administration supports:

* User Status
* Report Status

Filtering uses query parameters.

---

# 15. Sorting

Supported sorting:

* Newest
* Oldest
* Price Low to High
* Price High to Low
* Most Viewed
* Most Favorited

Default sorting:

Newest Listings

---

# 16. Search

Marketplace search supports:

* Listing Title
* Description
* Category

Search should be case-insensitive.

---

# 17. HTTP Status Codes

| Code | Meaning               |
| ---- | --------------------- |
| 200  | Success               |
| 201  | Resource Created      |
| 204  | No Content            |
| 400  | Bad Request           |
| 401  | Unauthorized          |
| 403  | Forbidden             |
| 404  | Resource Not Found    |
| 409  | Conflict              |
| 422  | Validation Error      |
| 500  | Internal Server Error |

Consistent status codes improve API reliability.

---

# 18. Error Handling

Errors must:

* Be logged internally
* Never expose sensitive information
* Return user-friendly messages
* Follow a consistent response format

Unexpected exceptions should return generic error messages.

---

# 19. Security Standards

Every API request follows the platform security policies.

Security measures include:

* Password Hashing
* Email Verification
* CSRF Protection
* SQL Injection Prevention
* XSS Prevention
* Input Validation
* Authorization Checks
* File Validation
* Account Lockout Protection
* Secure Session Management

---

# 20. Performance Guidelines

The API should support:

* Database Indexing
* Pagination
* Efficient Queries
* Optimized Image Loading
* Lazy Loading (where appropriate)

Repeated database queries should be minimized.

---

# 21. Logging

The application should log important events including:

* Login
* Logout
* Registration
* Listing Creation
* Offer Submission
* Offer Acceptance
* Meet-up Scheduling
* Report Submission
* Administrative Actions

Logs support monitoring, debugging, and auditing.

---

# 22. Hybrid Request Strategy

Student Bazaar is designed primarily as a server-rendered Flask application.

General guidelines include:

* Browser navigation returns HTML pages.
* Dynamic page updates return JSON.
* Business logic is shared through the Service Layer.
* Frontend code should not duplicate server-side business logic.

This hybrid architecture provides a modern user experience while maintaining a clean and maintainable Flask application.

---

# 23. Future API Enhancements

Future versions may introduce:

* Public REST API
* Mobile API
* Webhooks
* AI Recommendation API
* Payment API
* Push Notification API

The architecture supports future expansion without major restructuring.

---

# 24. Conclusion

The Student Bazaar API follows RESTful principles and Flask best practices while adopting a hybrid server-rendered architecture. By combining feature-based modules, standardized request handling, secure authentication, and consistent response formats, the API provides a scalable, maintainable, and future-ready foundation for the Student Bazaar platform.
