WendyOS Docs
TutorialsPython

Add an API endpoint

Extend your Python app with a request model, a calculation, and HTTP checks.

Extend the app from your first Python deployment with a POST /quote endpoint. It accepts a quantity and unit price, validates them, and returns a total. You will check both a valid request and invalid input.

Prerequisites

Use the hello-world project from the previous tutorial, the same target, and Docker with Buildx running. You do not need a local Python installation. The HTTP checks below use curl in a macOS or Linux terminal; in Windows PowerShell, use curl.exe and the equivalent JSON quoting, or use the browser's /docs API interface.

1. Add the endpoint

Open app.py. The template already imports BaseModel. Add Field to that import:

from pydantic import BaseModel, Field

Below the existing routes, add:

class QuoteRequest(BaseModel):
    quantity: int = Field(gt=0)
    unit_price: float = Field(ge=0)


@app.post("/quote")
async def quote(request: QuoteRequest):
    return {"total": round(request.quantity * request.unit_price, 2)}

quantity must be positive and unit_price cannot be negative. FastAPI uses the model to validate the JSON body before calling the function. This example calculates a result for each request; it does not store orders.

2. Deploy the change

From the project directory, use the WENDY_TARGET you set in the previous tutorial. In a new terminal, set it again to your device hostname or IP from wendy discover, vm:<name> from wendy vm list, or 127.0.0.1:50051 for an unenrolled local-only Linux agent. Watch mode requires an explicit target or a saved default; it cannot ask you to choose one.

wendy run --device "$WENDY_TARGET" --watch

If the watch loop is already running, save app.py and wait for deployment to finish. If you stopped the app after the previous tutorial, this command starts it again.

3. Check a valid request

In another terminal, set APP_URL to the exact base URL printed by Wendy, including its port. For example, if it printed http://127.0.0.1:3001:

APP_URL=http://127.0.0.1:3001
curl -i "$APP_URL/quote" \
  -H 'Content-Type: application/json' \
  -d '{"quantity":3,"unit_price":12.5}'

Expect HTTP status 200 and:

{"total":37.5}

You can also open /docs at the same base URL and use Try it out under POST /quote to submit the body in your browser.

4. Check invalid input

Send a zero quantity:

curl -i "$APP_URL/quote" \
  -H 'Content-Type: application/json' \
  -d '{"quantity":0,"unit_price":12.5}'

Expect HTTP status 422 and an error identifying quantity in the request body. If you get 404, confirm that the updated app deployed and that you used /quote. If the connection fails, check the printed address and deployment troubleshooting.

Finish or continue

Stop the watch loop with Ctrl+C. To stop the app on your target, run wendy --device "$WENDY_TARGET" device apps stop hello-world. Stop an unused VM with wendy vm stop <name>.

You now have a deployed endpoint with input validation. For a separate project that adds a browser frontend and persistent storage, follow the Python web app tutorial.

On this page