Skip to content

Building an ML API with Flask/FastAPI

Why APIs are common for ML deployment

An API lets other services call your model:

  • web apps
  • mobile apps
  • internal tools

The minimal contract

Inputs:

  • JSON payload with features

Outputs:

  • prediction (and optionally probability)

false


  flowchart LR
  C[Client] -->|HTTP POST JSON| A[API]
  A --> M[Model pipeline]
  M --> A
  A -->|JSON response| C

false

FastAPI is popular because:

  • automatic docs
  • type hints
  • validation
app.py (FastAPI)
# Requires: fastapi, uvicorn, joblib
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
 
app = FastAPI()
model = joblib.load("model.joblib")
 
class Features(BaseModel):
    age: int
    income: float
    city: str
    plan: str
 
@app.post("/predict")
def predict(features: Features):
    X = [features.model_dump()]
    pred = model.predict(X)[0]
    return {"prediction": int(pred)}
app.py (FastAPI)
# Requires: fastapi, uvicorn, joblib
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
 
app = FastAPI()
model = joblib.load("model.joblib")
 
class Features(BaseModel):
    age: int
    income: float
    city: str
    plan: str
 
@app.post("/predict")
def predict(features: Features):
    X = [features.model_dump()]
    pred = model.predict(X)[0]
    return {"prediction": int(pred)}

Flask example (minimal)

app.py (Flask)
# Requires: flask, joblib
from flask import Flask, request, jsonify
import joblib
 
app = Flask(__name__)
model = joblib.load("model.joblib")
 
@app.post("/predict")
def predict():
    payload = request.get_json(force=True)
    pred = model.predict([payload])[0]
    return jsonify({"prediction": int(pred)})
app.py (Flask)
# Requires: flask, joblib
from flask import Flask, request, jsonify
import joblib
 
app = Flask(__name__)
model = joblib.load("model.joblib")
 
@app.post("/predict")
def predict():
    payload = request.get_json(force=True)
    pred = model.predict([payload])[0]
    return jsonify({"prediction": int(pred)})

Practical tips

  • validate inputs (types, ranges)
  • log requests (careful with PII)
  • version your model artifact

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did