Search documentation

Type to search the documentation.

Docs/Examples/Serve HTTP endpoints

Python example

Serve HTTP endpoints

Declare GET and POST endpoints for a Live App.

Before you run#

Use an installed, authenticated Yak CLI with access to the invite-only preview. Read Live Apps for the full setup and current behavior.

Source code#

Download hello_live_app.py

Canonical examplehello_live_app.py
hello_live_app.py
import asyncio
import yak

app = yak.App(name="hello-live-app")


@app.task()
@yak.web_endpoint(method="GET", path="/hello")
def hello(request: yak.Request) -> yak.Response:
    name = request.query_params.get("name", "Yak")
    return yak.Response(
        body={"message": f"hello from {name}", "path": request.path},
        status=200,
    )


@app.task()
@yak.web_endpoint(method="POST", path="/echo")
def echo(request: yak.Request) -> yak.Response:
    payload = request.json
    return yak.Response(
        body={"received": payload},
        status=200,
    )


@app.entrypoint()
async def main() -> None:
    await app.serve()


if __name__ == "__main__":
    asyncio.run(main())

Run the example#

Save the source as hello_live_app.py, then run:

Commandhello_live_app.py
hello_live_app.py ready
$ yak serve hello_live_app.py