Welcome to uapi!#

uapi is an elegant, fast, and high-level framework for writing network services in Python 3.10 and later.

Using uapi enables you to:

Installation#

uapi requires an underlying web framework to run. If you are unsure which to pick, we recommend Starlette for a good balance of features and speed.

$ pip install uapi starlette uvicorn
$ pip install uapi flask gunicorn
$ pip install uapi quart uvicorn
$ pip install uapi django gunicorn
$ pip install uapi aiohttp

Your First Handler#

Let’s write a very simple Hello World HTTP handler and expose it on the root path.

Before we start writing our handlers, we need something to register them with. In uapi, that something is an instance of an App.

from uapi.starlette import App

app = App()

@app.get("/")
async def hello() -> str:
    return "hello world"
from uapi.flask import App

app = App()

@app.get("/")
def hello() -> str:
    return "hello world"
from uapi.quart import App

app = App()

@app.get("/")
async def hello() -> str:
    return "hello world"
from uapi.django import App

app = App()

@app.get("/")
def hello() -> str:
    return "hello world"
from uapi.aiohttp import App

app = App()

@app.get("/")
async def hello() -> str:
    return "hello world"

Note

uapi uses type hints in certain places to minimize boilerplate code. This doesn’t mean you’re required to type-check your code using a tool like Mypy, however. We’re not the Python police; you do you.

Mypy’s pretty great, though.

Let’s start serving the file.

Change the code to the following, and run it:

from asyncio import run
from uapi.starlette import App

app = App()

@app.get("/")
async def hello() -> str:
    return "hello world"

run(app.run())

Change the code to the following, and run it:

from uapi.flask import App

app = App()

@app.get("/")
def hello() -> str:
    return "hello world"

app.run(__name__)

Change the code to the following, and run it:

from asyncio import run
from uapi.quart import App

app = App()

@app.get("/")
async def hello() -> str:
    return "hello world"

run(app.run(__name__))
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line

from uapi.django import App

app = App()


@app.get("/")
def hello() -> str:
    return "hello world"


settings.configure(ALLOWED_HOSTS="*", ROOT_URLCONF=__name__)

urlpatterns = app.to_urlpatterns()

if __name__ == "__main__":
    execute_from_command_line()
else:  # new
    application = WSGIHandler()

Then run the file using python <filename> runserver.

Note

This example uses code from the µDjango project.

Change the code to the following, and run it:

from asyncio import run
from uapi.aiohttp import App

app = App()

@app.get("/")
async def hello() -> str:
    return "hello world"

run(app.run())

Your app is now running in development mode on localhost, port 8000.

$ curl 127.0.0.1:8000
hello world⏎