Running the Application

Quickstart – Running the Application

Kick off your demo RESTful API in seconds by following this Quickstart guide. You’ll learn how to prepare your environment, install dependencies, and launch the Flask server (with JWT-protected endpoints) out of the box.


Index


Prerequisites

Before you begin, ensure you have:

  • Python 3.6+ installed and available on your PATH.
  • An environment tool of your choice (e.g., venv, virtualenv, conda).
  • (Optional) Postman or curl for testing endpoints.

Installation

  1. Clone the repository

    git clone https://github.com/magarrent/REST-API-PYTHON-demo.git
    cd REST-API-PYTHON-demo/PYTHON
    
  2. Create & activate a virtual environment

    python3 -m venv .venv
    source .venv/bin/activate      # Linux/macOS
    .venv\Scripts\activate         # Windows
    
  3. Install dependencies

    pip install Flask Flask-RESTful Flask-JWT Flask-SQLAlchemy
    

    No requirements.txt is provided; the core dependencies are inferred from the main entry-point.


Running the Application

Start the server with a single command:

python app.py

Behind the scenes, this executes the following snippet in app.py:

if __name__ == '__main__':
    from db import db
    db.init_app(app)
    app.run(port=5000, debug=True)
  • Port: 5000 (default)
  • Debug mode: Enabled (auto-reload & detailed error pages)

Verifying the Setup

Once running, you should see console output similar to:

 * Serving Flask app "app" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Open your browser or use curl to hit an unprotected endpoint:

curl http://localhost:5000/books

Response:

{"books": []}

Default Configuration

The application bootstraps with sensible defaults defined in app.py:

Configuration Key Default Value Purpose
SQLALCHEMY_DATABASE_URI 'sqlite:///data.db' Location of the SQLite database file
SQLALCHEMY_TRACK_MODIFICATIONS True Enable SQLAlchemy event system (may impact performance)
PROPAGATE_EXCEPTIONS True Allow exceptions to bubble up to Flask’s error handlers
secret_key 'SapanCrackle' Used to sign JWT tokens and session cookies
app.config['SQLALCHEMY_DATABASE_URI']    = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['PROPAGATE_EXCEPTIONS']       = True
app.secret_key                          = 'SapanCrackle'

Database Initialization

A built-in hook ensures your SQLite schema is created before the first request:

@app.before_first_request
def create_tables():
    db.create_all()
  • When? Runs once, immediately before handling the very first HTTP request.
  • What? Invokes SQLAlchemy’s create_all() to generate tables for all registered models (User, Book, Store).

That's it – your RESTful API is now live on http://localhost:5000/, ready for registration, authentication, and managing users, books, and stores!