A FastAPI app in Python
A decorated function is an endpoint; the return value becomes JSON.
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI(title="typestar")
@app.get("/healthz")
def healthz():
return {"status": "ok"}
@app.get("/langs")
def langs():
return ["python", "rust", "sql"]
client = TestClient(app)
print(client.get("/healthz").json())
print(client.get("/langs").status_code, client.get("/langs").json())
How it works
- The decorator carries the method and the path.
- A dict or a model is serialized for you.
TestClientcalls the app without a server.
Keywords and builtins used here
defhealthzlangsprintreturn
The run, in numbers
- Lines
- 19
- Characters to type
- 364
- Tokens
- 117
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 64 seconds.
Step 1 of 4 in FastAPI routes, step 6 of 19 in Web services & data access.