Skip to main content

superfunctions-fastapi

FastAPI adapter for superfunctions.http

Location: packages/python-fastapi/
Package: superfunctions-fastapi
Import: from superfunctions_fastapi import create_router

Installation

pip install superfunctions-fastapi

Usage

Option 1: Create Router from Routes

from fastapi import FastAPI
from superfunctions.http import Route, HttpMethod, Response, NotFoundError
from superfunctions_fastapi import create_router

app = FastAPI()

# Define handlers using superfunctions abstractions
async def get_user(request, context):
    user_id = context.params["id"]
    
    # Your logic here
    user = await db.find_one(...)
    if not user:
        raise NotFoundError(f"User {user_id} not found")
    
    return Response(status=200, body=user)

async def create_user(request, context):
    data = await context.json()
    user = await db.create(...)
    return Response(status=201, body=user)

# Create router from routes
routes = [
    Route(method=HttpMethod.GET, path="/users/{id}", handler=get_user),
    Route(method=HttpMethod.POST, path="/users", handler=create_user),
]

router = create_router(routes, prefix="/api/v1", tags=["users"])
app.include_router(router)

Option 2: Convert Individual Handlers

from fastapi import FastAPI, Request
from superfunctions_fastapi import to_fastapi_handler
from superfunctions.http import Response

app = FastAPI()

async def get_user(request, context):
    return Response(status=200, body={"id": context.params["id"]})

@app.get("/users/{id}")
async def route(request: Request, id: str):
    handler = to_fastapi_handler(get_user)
    return await handler(request, id=id)

Features

  • ✅ Automatic request/response conversion
  • ✅ HTTP error handling
  • ✅ Path parameters
  • ✅ Query parameters
  • ✅ Headers
  • ✅ JSON body parsing
  • ✅ OpenAPI documentation support

Benefits

Write framework-agnostic handlers that work across:

  • FastAPI
  • Flask (with superfunctions-flask)
  • Django (with superfunctions-django)

Example with authfn

from fastapi import FastAPI, Depends
from superfunctions_fastapi import create_router
from superfunctions.http import Route, HttpMethod, Response, UnauthorizedError
from authfn import create_authfn, AuthFnConfig

app = FastAPI()
auth = create_authfn(AuthFnConfig(database=adapter))

async def get_protected_resource(request, context):
    # Authenticate using authfn
    auth_header = context.headers.get("authorization")
    if not auth_header:
        raise UnauthorizedError("Missing authorization header")
    
    session = await auth.provider.authenticate(request)
    if not session:
        raise UnauthorizedError("Invalid credentials")
    
    return Response(status=200, body={"message": "Protected resource", "user": session.keyId})

routes = [
    Route(method=HttpMethod.GET, path="/protected", handler=get_protected_resource),
]

router = create_router(routes)
app.include_router(router)

License

MIT

Metadata

Release files for superfunctions-fastapi 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for superfunctions-fastapi 0.1.0
File Size Uploaded Provenance Actions
superfunctions_fastapi-0.1.0.tar.gz 4.2 kB Without provenance Details for superfunctions_fastapi-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for superfunctions-fastapi 0.1.0
File Interpreter ABI Platform Actions
superfunctions_fastapi-0.1.0-py3-none-any.whl Python 3 none any Details for superfunctions_fastapi-0.1.0-py3-none-any.whl

Total release size: 9.1 kB

Release files / superfunctions_fastapi-0.1.0.tar.gz

Download URL superfunctions_fastapi-0.1.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
89c35b0e3f4bbbea9ffa68aa3cbf65976631f5a1740de7d4ab61d6bd37e9a3c5
BLAKE2b-256 checksum
How to use checksums
0b63aba5cabd20666f4501ffe601d01752874ebb76f37c180fef43c58a7b1f92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / superfunctions_fastapi-0.1.0-py3-none-any.whl

Download URL superfunctions_fastapi-0.1.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1bbb8692cf44d04a975a146abd1b6f369b180eb8a4480585a44f67335a7c29ef
BLAKE2b-256 checksum
How to use checksums
d1c52c1b2038c4a4d2123b82dc5dbc165ae0c1d789bae2e30c4d10ffc8d1c7f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for superfunctions-fastapi 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page