Serving with an API
Twenty lines to production.
An A P I is like a waiter in a restaurant. The customer doesn't walk into the kitchen. They give the waiter an order, and the waiter brings back the food. Your website or mobile app sends a request with a house, and your A P I sends back a price. The kitchen is your model.

- An API is a waiter
- Orders in, food out
- The kitchen is your model
A model behind an API
Fast A P I is the modern way to do this. We load the model one time, when the server starts. The House class defines what a request must contain, with a type for every field. This is called validation, and it protects your model from bad data. Then one function handles the predict route. It turns the request into a one row table, asks the model, and returns the price as J S O N. That's a real web service, in about twenty lines.
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
app = FastAPI(title="HomeWorth API")
model = joblib.load("homeworth.joblib")
class House(BaseModel):
MedInc: float
HouseAge: float
AveRooms: float
AveBedrms: float
Population: float
AveOccup: float
Latitude: float
Longitude: float
@app.post("/predict")
def predict(house: House):
row = pd.DataFrame([house.model_dump()])
return {"price": round(float(model.predict(row)[0]) * 100000)}Testing the API
Let's call it. We use Fast A P I's test client, which behaves exactly like a real caller. We send a house as J S O N. Run it. We get status two hundred, which means success, and a price. Now look at the last line. We send bad data, with text instead of a number. The server responds with an error code of four twenty two, and our model is never even touched. That's validation doing its job.
import json
from fastapi.testclient import TestClient
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import HistGradientBoostingRegressor
import joblib
data = fetch_california_housing(as_frame=True)
joblib.dump(HistGradientBoostingRegressor(random_state=0).fit(data.data, data.target),
"homeworth.joblib")
from api import app
client = TestClient(app)
house = data.data.iloc[0].to_dict()
reply = client.post("/predict", json=house)
print("Status:", reply.status_code)
print("Reply: ", json.dumps(reply.json()))
bad = client.post("/predict", json={"MedInc": "lots"})
print("Bad input status:", bad.status_code)Status: 200
Reply: {"price": 415527}
Bad input status: 422Also uses api.py
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
app = FastAPI(title="HomeWorth API")
model = joblib.load("homeworth.joblib")
class House(BaseModel):
MedInc: float
HouseAge: float
AveRooms: float
AveBedrms: float
Population: float
AveOccup: float
Latitude: float
Longitude: float
@app.post("/predict")
def predict(house: House):
row = pd.DataFrame([house.model_dump()])
return {"price": round(float(model.predict(row)[0]) * 100000)}Running it for real
To run this for real, you use a server called uvicorn. Point it at the file and the app, and your model is live on your machine. Fast A P I even gives you an interactive documentation page for free, at slash docs, where anyone can try it. Next, you would wrap all of this in a Docker container, and deploy it to the cloud. We cover that fully in our Docker, FastAPI, and M L Ops courses.
uvicorn api:app --port 8000 INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Summary
Twenty lines of code, and your model can serve the whole world. Your developers are happy. But the sales team can't use an A P I, they want something to click on. Next, you'll build a real interactive app, using nothing but Python.
Key takeaways- An API wraps your model
- Validation blocks bad data
- uvicorn runs it for real