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.
Installation
Install PyBackend from PyPI using pip. No extra setup required.
pip install pybackend
# Verify the installation
python -c "import pybackend; print(pybackend.__version__)"
Quick start
A complete API in six lines. Create a file, run it, and your server is live.
app = App()
@app.get("/")
def home():
return {"message": "Hello, PyBackend"}
app.run(port=8080)
# → Server running at http://localhost:8080
Routing
Register handler functions using HTTP method decorators. Each decorator binds a URL path to a function.
@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.
@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}
Middleware
Middleware runs before every route. Use for logging, auth, CORS, etc.
@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()
Request object
The req object gives you everything about the incoming request.
| Property | Type | Description |
|---|---|---|
| req.method | str | GET, POST, PUT, PATCH, DELETE |
| req.path | str | URL path, e.g. /users/42 |
| req.headers | dict | HTTP headers |
| req.query | dict | parsed query string |
| req.params | dict | dynamic route parameters |
| req.body | dict | parsed JSON body |
| req.state | dict | mutable state, shared with middleware |
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.
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"])
| Method | Use 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.
| Route | Shows |
|---|---|
| /admin | Live request count, uptime, status |
| /admin/logs | Streaming request log |
| /admin/routes | All registered routes |
| /admin/docs | Auto‑generated API docs |
port=8080,
admin=True,
debug=False
)