Skip to main content

A simple and fast ASGI microframework for Python with WebSocket support.

Project description

AltAPI

A simple and fast ASGI microframework for Python with WebSocket support.

Documentation GitHub License

Changelog

v1.2.0

  • Added workers parameter to app.run() for multi-process support (still unstable sometimes)
  • Added access_log parameter to enable/disable request logging
  • GC optimizations now apply to all workers automatically

Installation

pip install altapi

Requirements:

  • Python >= 3.10

Quick Start

Minimal Example

from altapi import AltAPI
from altapi.http import JSONResponse

app = AltAPI()


@app.get("/")
async def home(request):
    return JSONResponse({"message": "Hello, World!"})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

Run:

python app.py

Or via uvicorn:

uvicorn app:app --reload

Features

  • ✅ ASGI compliant
  • ✅ JSON, HTML, and text responses
  • ✅ Typed path parameters ({id:int}, {name:str}, {value:float})
  • ✅ Sync and async handlers
  • ✅ Full WebSocket support
  • ✅ Built-in server (app.run())
  • ✅ Jinja2 templates
  • ✅ Response caching
  • ✅ Static file mounting

Usage Examples

Path Parameters

from altapi.http import JSONResponse


@app.get("/users/{id:int}")
async def get_user(request):
    user_id = request.path_params["id"]  # automatically int
    return JSONResponse({"id": user_id, "name": f"User {user_id}"})


@app.get("/items/{name:str}")
async def get_item(request):
    name = request.path_params["name"]  # str
    return JSONResponse({"name": name})

Handling POST Requests

@app.post("/api/echo")
async def echo(request):
    data = await request.json()
    return JSONResponse({"echo": data})

WebSocket

from altapi.websocket import WebSocket


@app.websocket("/ws/echo")
async def websocket_echo(ws: WebSocket):
    await ws.accept()
    while True:
        text = await ws.receive_text()
        await ws.send_text(f"Echo: {text}")

Caching

from altapi.caching import InMemoryCache, cache

app = AltAPI(
    cache_backend=InMemoryCache(max_size=1000),
    cache_timeout=300
)


@app.get("/api/data")
@cache(expires=3600)  # cache for 1 hour
async def get_data(request):
    return JSONResponse({"data": "cached"})

Jinja2 Templates

from altapi.templating import Jinja2Templates

app = AltAPI(templates_directory="templates")
templates = Jinja2Templates(directory="templates")


@app.get("/")
async def home(request):
    return templates.TemplateResponse(
        "index.html",
        {"request": request, "title": "Home Page"}
    )

Static Files

# Automatically serves files at /static
app = AltAPI(static_directory="static")

Response Types

from altapi.http import (
    JSONResponse,      # JSON response
    HTMLResponse,      # HTML response
    PlainTextResponse, # Plain text response
    StreamingResponse, # Streaming response
    FileResponse,      # File download
    RedirectResponse,  # Redirect
)

Documentation

Full documentation is available in DOCS.md.

License

AGPLv3, see LICENSE.txt for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

altapi-1.2.1.tar.gz (120.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

altapi-1.2.1-cp312-cp312-win_amd64.whl (159.4 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file altapi-1.2.1.tar.gz.

File metadata

  • Download URL: altapi-1.2.1.tar.gz
  • Upload date:
  • Size: 120.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for altapi-1.2.1.tar.gz
Algorithm Hash digest
SHA256 19300d145c6694760a087a2a555379f879584b6e926c219da5c33d1b15ff935f
MD5 9d83cf522c48db99c3a4ddadb0faa989
BLAKE2b-256 9f9f346e3951567c4191ddd8861d17748129d6c4f2d93023720040d15bd857cd

See more details on using hashes here.

File details

Details for the file altapi-1.2.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: altapi-1.2.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 159.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for altapi-1.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f18569e6e96595dc9b706e195067990336bcd41c15e8777c0c6a22f6260f17f9
MD5 3e95a9ccdc370fd49fffddf663dadd47
BLAKE2b-256 c3fee20ac0d28439305bee99d68312ebf663623d2599a7828a0aadf654658a0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page