SwiftAPI

Build Fast, Modern APIs with Python

An educational, lightweight Python web framework inspired by FastAPI. Perfect for learning and building efficient asynchronous web services.

Build: Passing
PyPI: v0.1.0
License: MIT
A screenshot of Python code for a web application.

Try the demo locally

pip install swiftapi\npython examples/hello_world.py

Getting Started

Getting a SwiftAPI application up and running is quick and easy. Follow these simple steps.

1. Installation

Install SwiftAPI using pip. It has minimal dependencies to keep your projects lightweight.

pip install swiftapi

2. Create your first endpoint

Create a file (e.g., main.py) and add the following code:

from swiftapi.app import SwiftAPI

app = SwiftAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello World"}

3. Run the server

Use an ASGI server like Uvicorn to run your application.

uvicorn your_file:app --reload

Now, open your browser and navigate to http://127.0.0.1:8000. You should see the JSON response: {"message": "Hello World"}.

Async HTTP Routing

SwiftAPI uses simple decorators to define API routes. It fully supports async def for high-performance, non-blocking I/O.

Path and Query Parameters

Define path parameters with curly braces and add type-hinted function arguments for query parameters. SwiftAPI handles parsing and type conversion for you.

@app.get("/users/{user_id}")
async def read_user(user_id: int, q: str | None = None):
    # path parameter 'user_id' is automatically converted to int
    # query parameter 'q' is optional
    return {"user_id": user_id, "q": q}

A request to /users/123?q=searchterm would result in the JSON response: {"user_id": 123, "q": "searchterm"}.

A diagram illustrating API request routing.

Lightweight Validation

An icon representing successful data validation.

Ensure data integrity with a simple, Pydantic-like validation system using Python's built-in dataclasses.

Request Body Validation

Define your expected data structure using a dataclass. SwiftAPI will automatically validate incoming request bodies, parse the data, and inject it into your path function.

from dataclasses import dataclass
from swiftapi.app import SwiftAPI

app = SwiftAPI()

@dataclass
class Item:
    name: str
    price: float
    is_offer: bool | None = None

@app.post("/items")
async def create_item(item: Item):
    # SwiftAPI automatically validates the request body
    # against the 'Item' dataclass.
    return item

If a request is sent with an invalid body (e.g., missing 'name' or incorrect type for 'price'), SwiftAPI will automatically return a 422 Unprocessable Entity error with a descriptive JSON body.

Automatic API Docs

One of SwiftAPI's most powerful features is its ability to automatically generate interactive API documentation from your code.

A preview of the Swagger UI documentation page.

Swagger UI

Navigate to /docs to explore your API through a rich, interactive Swagger UI. Test endpoints, see schemas, and understand your API's capabilities instantly.

A preview of the ReDoc documentation page.

ReDoc

For a clean, single-column documentation view, visit /redoc. ReDoc provides a beautifully rendered, readable alternative for your API documentation.

This is all generated automatically from your type hints and route definitions, requiring zero extra configuration.

Contributing to SwiftAPI

SwiftAPI is an open-source project, and contributions are welcome! Whether it's reporting a bug, proposing a new feature, or submitting code, your help is appreciated.