Getting Started Sample Project - flarchitect
Description: Flarchitect ships with a tiny demo that shows how it turns a SQLAlchemy model into a REST API. The sample lives in demo/quickstart/load.py and defines a single Author model. Running the script star...

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# Getting Started Sample Project

Flarchitect ships with a tiny demo that shows how it turns a SQLAlchemy model into a REST API. The sample lives in `demo/quickstart/load.py` and defines a single `Author` model. Running the script starts a local server and exposes the model at `/api/author`, returning an empty list until you add data.

     1import datetime
     2
     3from flask import Flask
     4from flask_sqlalchemy import SQLAlchemy
     5from sqlalchemy import Date, Integer, String, Text
     6from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
     7
     8from flarchitect import Architect
     9
    10
    11# Create a base model that all models will inherit from. This is a requirement for the auto api creator to work.
    12# Don't, however, add to any of your models when using flask-sqlalchemy, instead, inherit from `db.model` as you
    13# would normally.
    14class BaseModel(DeclarativeBase):
    15    def get_session(*args):
    16        return db.session
    17
    18
    19# Create a new flask app
    20app = Flask(__name__)
    21
    22# Create a new instance of the SQLAlchemy object and pass in the base model you have created.
    23db = SQLAlchemy(model_class=BaseModel)
    24
    25# Set the database uri to an in memory database for this example.
    26app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
    27
    28# Set the required fields for flarchitect to work.
    29app.config["API_TITLE"] = "My API"
    30app.config["API_VERSION"] = "1.0"
    31app.config["API_BASE_MODEL"] = db.Model
    32
    33
    34# Create a new model that inherits from db.Model
    35class Author(db.Model):
    36    __tablename__ = "author"
    37
    38    class Meta:
    39        # all models should have class Meta object and the following fields which defines how the model schema's are
    40        # references in redocly api docs.
    41        tag_group = "People/Companies"
    42        tag = "Author"
    43
    44    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    45    first_name: Mapped[str] = mapped_column(String)
    46    last_name: Mapped[str] = mapped_column(String)
    47    biography: Mapped[str] = mapped_column(Text)
    48    date_of_birth: Mapped[datetime] = mapped_column(Date)
    49    nationality: Mapped[str] = mapped_column(String)
    50    website: Mapped[str | None] = mapped_column(String)
    51
    52
    53with app.app_context():
    54    # initialize the database with the app context
    55    db.init_app(app)
    56    # create the database tables
    57    db.create_all()
    58    # initialize the Architect object with the app context
    59    Architect(app)
    60
    61# Run the app
    62if __name__ == "__main__":
    63    app.run(debug=True)
    64
    65# To access the API documentation, navigate to http://localhost:5000/docs

## Run the demo

    python demo/quickstart/load.py
    curl http://localhost:5000/api/author

The curl command answers with a JSON payload that includes some metadata and a `value` list of authors. Because the demo starts with no records, that list is empty:

    {
        "total_count": 0,
        "value": []
    }

Pop open `http://localhost:5000/docs` in your browser to explore the automatically generated API docs.