If you've used Flask before and loved how simple it is to build APIs, you’ll probably enjoy Quart too. Quart is like Flask, but with built-in async support, making it better for handling modern, high-speed web applications.
💡 Why Quart?
Flask is great, but it wasn’t built with async/await in mind. In today’s world where performance and speed are critical, especially for APIs having support for asynchronous code (without workarounds) is a big win.
Quart gives you:
The same API and structure as Flask
Support for async views, database calls, etc.
Works well with tools like SQLAlchemy, JWT, and more
🔁 Flask vs. Quart (Basic Example)
Flask:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from Flask!"
Quart:
from quart import Quart
app = Quart(__name__)
@app.route("/")
async def hello():
return "Hello from Quart!"
You’ll notice the main difference is async def instead of def. That allows you to use await inside the route, like calling an async database or HTTP request.
🔐 JWT Authentication Example in Quart
from quart import Quart
from quart_jwt_extended import JWTManager, jwt_required, create_access_token
app = Quart(__name__)
app.config["JWT_SECRET_KEY"] = "your-secret-key"
jwt = JWTManager(app)
@app.route("/login", methods=["POST"])
async def login():
# Dummy example
access_token = create_access_token(identity="user123")
return {"access_token": access_token}
@app.route("/protected")
@jwt_required
async def protected():
return {"message": "This is a protected route"}
📁 Example Project
I’ve created a small Quart project that shows how to:
Set up Quart with async SQLAlchemy
Use JWT for authentication
Organize routes and config
👉 Check it out on GitHub:
Quart API
A Python-based asynchronous REST API built with Quart, SQLAlchemy (async), and [PostgreSQL], using Poetry for dependency management.
🚀 Features
- Fast async API with Quart
- PostgreSQL + SQLAlchemy (async ORM)
- JWT authentication with
quart-jwt-extended
- Fully typed models
- Alembic for migrations
🛠️ Requirements
- Python 3.10+
- Poetry
- PostgreSQL database
📦 Installation
1. Clone the repository
git clone https://github.com/engrmarkk/quart-api.git
cd quart-api
2. Install dependencies
poetry install
3. Create .env
touch .env
Edit .env
and set your database credentials or JWT secret, e.g.:
DB_URI=postgresql+asyncpg://username:password@localhost:5432/quart-api # or your external db (but +asyncpg: should be present in the uri)
SECRET_KEY=supersecretkey
🗃️ Database Setup
Create the PostgreSQL database
CREATE DATABASE quart_api;
Run migrations
poetry run alembic upgrade head
🔐 JWT Auth (Optional Setup)
Ensure you're using quart-jwt-extended
.
Poetry handles this if you ran poetry install
.
▶️ Running the Server
chmod u+x run_it.sh # to give the file permission the first time
…🧠 Final Thoughts
Quart is a great choice if you're already familiar with Flask and want to step into the world of asynchronous Python. It lets you build scalable, modern APIs without changing much in your existing workflow.
So if you're starting a new project, give Quart a try!
Top comments (0)