Docs Admin Login GitHub

Build APIs fast with
PyBackend

A lightweight Python web framework for clarity and speed. Define routes with decorators, handle requests with plain functions, and ship production-ready APIs in minutes.

$ pip install pybackend
0
Required deps
Python 3.8+
Minimum version
5 methods
HTTP verbs
MIT
License

Installation

Install PyBackend from PyPI using pip. No extra setup required.

terminalshell
# Install the latest stable release
pip install pybackend

# Verify the installation
python -c "import pybackend; print(pybackend.__version__)"
✓ Zero required dependencies. PyBackend runs on the standard library. Optional template rendering requires jinja2.

Quick start

A complete API in six lines. Create a file, run it, and your server is live.

app.pypython
from pybackend import App

app = App()

@app.get("/")
def home():
    return {"message": "Hello, PyBackend"}

app.run(port=8080)
terminalshell
python app.py
# → Server running at http://localhost:8080
💡 Auto-reload. Pass debug=True to app.run() to enable hot-reloading during development.

Routing

Register handler functions using HTTP method decorators. Each decorator binds a URL path to a function.

GET POST PUT PATCH DELETE
routes.pypython
# list all users
@app.get("/users")
def list_users():
    return {"users": []}

# create a user
@app.post("/users")
def create_user(req):
    return {"created": req.body}

# PUT, PATCH, DELETE work the same way

Dynamic routes

Wrap a segment in curly braces to capture it as a parameter. The param is passed to your handler.

dynamic.pypython
# /users/42
@app.get("/users/{id}")
def get_user(id):
    return {"id": id}

# nested: /users/42/posts/7
@app.get("/users/{id}/posts/{post_id}")
def get_post(id, post_id):
    return {"user_id": id, "post_id": post_id}
📌 Params are strings. Cast to int or other types as needed.

Middleware

Middleware runs before every route. Use for logging, auth, CORS, etc.

middleware.pypython
# logging
@app.middleware
def logger(req, res, next):
    print(req.method, req.path)
    return next()

# auth – short‑circuit on failure
@app.middleware
def auth(req, res, next):
    token = req.headers.get("Authorization")
    if not token:
        return res.json(status=401, data={"error": "Unauthorized"})
    req.state["user"] = "alice" # dummy
    return next()
⚠ Order matters. Middleware runs in registration order. Returning early stops the chain.

Request object

The req object gives you everything about the incoming request.

PropertyTypeDescription
req.methodstrGET, POST, PUT, PATCH, DELETE
req.pathstrURL path, e.g. /users/42
req.headersdictHTTP headers
req.querydictparsed query string
req.paramsdictdynamic route parameters
req.bodydictparsed JSON body
req.statedictmutable state, shared with middleware
example – reading from reqpython
@app.post("/users/{id}/posts")
def create_post(req):
    user_id = req.params["id"]
    title = req.body.get("title")
    page = req.query.get("page", 1)
    token = req.headers.get("Authorization")
    return { "user_id": user_id, "title": title, "page": page }

Response helpers

The res object provides helpers. Return values are auto-serialized as JSON.

response examplespython
# JSON response
res.json(status=200, data={"ok": True})

# HTML string
res.send_html(200, "<h1>Hello</h1>")

# template render (Jinja2)
res.render("templates/index.html", title="Dashboard", user=req.state["user"])
MethodUse when
res.json()API endpoints returning structured data
res.send_html()returning raw HTML
res.render()full-page rendering with Jinja2

Admin pages

Built‑in admin UI at /admin with no extra setup. Browse logs, inspect routes, and read docs.

/admin /admin/logs /admin/routes /admin/docs
RouteShows
/adminLive request count, uptime, status
/admin/logsStreaming request log
/admin/routesAll registered routes
/admin/docsAuto‑generated API docs
⚠ Admin is disabled in production by default. Pass admin=True to app.run() to enable. Never expose to the public internet without auth.
enable admin in productionpython
app.run(
    port=8080,
    admin=True,
    debug=False
)