Building an API for a Model (Flask / FastAPI)
Part of the Data Science Roadmap
In short
A website or app can't import your notebook. Wrap the model in a small web API and anything that can send an HTTP request, from a phone app to a spreadsheet script, can get predictions.
Wrapping a trained model in a lightweight web API so other applications can send it data and get predictions back — FastAPI is the modern, faster-growing choice with built-in data validation.

Think of it like this
The takeaway counter
At a takeaway window, you hand over an order slip and get a parcel back. You never enter the kitchen or meet the cook. The counter checks your slip is valid and passes it inside. An API is that counter, and your model is the kitchen.
Watch it happen
One prediction request
POST /predict
1/5The app sends JSON with four flower measurements.
The big ideas
What you need to know
FastAPI
Type hints become input validation and automatic docs at /docs. A popular modern choice for ML APIs.
Flask
Older, minimal and very common. You handle validation yourself or add a library.
Load once
Load the model when the server starts, not inside the request function, or every call gets slow.
Validate inputs
Real users send missing fields, text in number fields and extreme values. Reject them clearly.
See it in code
Walk through the code
1import joblib2from fastapi import FastAPI3from pydantic import BaseModel4 5app = FastAPI()6model = joblib.load("iris_model.joblib") # load once at startup7 8class Flower(BaseModel):9 sepal_length: float10 sepal_width: float11 petal_length: float12 petal_width: float13@app.post("/predict")14def predict(f: Flower):15 pred = model.predict([[f.sepal_length, f.sepal_width, f.petal_length, f.petal_width]])16 return {"class_id": int(pred[0])}1/4Create the app and load the saved model a single time.
Save as main.py, install fastapi and uvicorn, then run uvicorn main:app --reload and open /docs to try it.
Save yourself the pain
Common mistakes
Oops
Loading the model file inside the predict function.
Do this instead
Load it once at startup. Reading from disk on every request wastes time.
Oops
Returning NumPy types directly in the response.
Do this instead
Convert to int, float or lists first. Some NumPy types can't be turned into JSON.
Your mission
Serve your saved model
Quick check
Why load the model at server startup instead of inside the request handler?
Go deeper
- 1Build a simple FastAPI endpoint that loads a model and returns a prediction.
- 2Test your API locally with a sample request using a tool like curl or Postman.
- 3Learn to validate incoming request data before passing it to the model.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.