Introduction
In the fast-evolving world of machine learning and artificial intelligence, developing models is only half the battle. The real power of machine learning comes when these models can be accessed and utilised by applications, users, and systems in real-time. That is where APIs come in. And among the tools available for creating efficient, scalable APIs, FastAPI stands out as a modern, high-performance web framework that makes the process both intuitive and powerful.
If you are enrolled in a Data Science Course in mumbai, chances are you have encountered FastAPI or will soon. This article will walk you through the process of creating APIs for your machine learning models using FastAPI, from setup to deployment.
Why FastAPI?
FastAPI is a Python-based web framework for building APIs. It is built on top of Starlette for the web parts and Pydantic for the data handling, making it:
- Fast – As the name suggests, it is optimised for speed.
- Modern – Supports asynchronous code and is built with Python-type hints.
- Easy to use – Designed to be developer-friendly and requires less boilerplate.
- Automatic documentation – Integrates OpenAPI and Swagger UI out of the box.
These features make it an ideal choice for serving machine learning models where speed, and ease of use are critical.
Step-by-Step: Creating a Machine Learning API with FastAPI
Let us walk through creating a simple API that wraps around a trained machine-learning model.
-
Prepare Your ML Model
Before you even touch FastAPI, ensure your machine-learning model is trained and saved. For example, suppose you have a classification model trained using scikit-learn:
# train_model.py
import pickle
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
iris = load_iris()
X, y = iris.data, iris.target
model = RandomForestClassifier()
model.fit(X, y)
with open(“iris_model.pkl”, “wb”) as f:
pickle.dump(model, f)
-
Install FastAPI and Uvicorn
Apart from FastAPI, ensure you have an ASGI server, say, Uvicorn, to run your API:
pip install fastapi uvicorn
-
Create Your FastAPI App
Now, let us build the FastAPI app that loads the model and serves predictions.
# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import numpy as np
import pickle
# Load model
with open(“iris_model.pkl”, “rb”) as f:
model = pickle.load(f)
app = FastAPI(title=”Iris Classifier API”)
# Define input schema
class IrisInput(BaseModel):
sepal_length: float
sepal_width: float
petal_length: float
petal_width: float
@app.post(“/predict”)
def predict(data: IrisInput):
features = np.array([[data.sepal_length, data.sepal_width, data.petal_length, data.petal_width]])
prediction = model.predict(features)
return {“prediction”: int(prediction[0])}
This basic structure is common in many projects taught as part of assignments in data courses. You define an input schema using BaseModel, load your model, and create an endpoint that receives JSON data and returns predictions.
-
Run the API
To start the FastAPI server:
uvicorn app:app –reload
Navigate to http://127.0.0.1:8000/docs to see the automatically generated Swagger UI documentation. This interactive interface lets you test your endpoint without needing external tools.
-
Test Your API
You can use a tool like Postman, curl, or simply the Swagger UI to test:
POST /predict
{
“sepal_length”: 5.1,
“sepal_width”: 3.5,
“petal_length”: 1.4,
“petal_width”: 0.2
}
A JSON response is generated that contains the predicted class.
-
Going Asynchronous
One of FastAPI’s most powerful features is async support, which is essential for non-blocking calls in production systems. Although machine learning predictions are often CPU-bound, using async def functions ensures your API is ready for integration with other async services like databases or message queues.
@app.post(“/predict”)
async def predict(data: IrisInput):
features = np.array([[data.sepal_length, data.sepal_width, data.petal_length, data.petal_width]])
prediction = model.predict(features)
return {“prediction”: int(prediction[0])}
-
Error Handling and Validation
With FastAPI, validation is automatically handled based on type annotations in the Pydantic models. If a user sends invalid data, the API will return a descriptive error message without any extra code.
This feature allows data scientists and engineers to build robust applications without extensive experience in backend development and with minimal coding efforts.
-
Deployment Tips
When you are ready to move your model into production, consider the following:
- Dockerise your app for easy deployment.
- Use Gunicorn with Uvicorn workers for production-grade performance.
- Host on services like Heroku, AWS, GCP, or Render.
- Implement logging and monitoring tools to track performance and usage.
-
Security Considerations
APIs that expose machine learning models can be vulnerable to misuse if not secured properly. Compromised machine-learning models can generate biases in inferences. Securing machine learning modes is crucial in view of the powerful adversary impacts security breaches can engender. Following are some best practices to ensure security of machine learning models:
- Use API keys or OAuth for authentication.
- Limit the rate of requests (rate limiting).
- Validate input thoroughly to avoid injection attacks.
- Monitor logs for unusual behaviour.
-
Extending the API
After deploying this basic model, you can choose to extend your API by adding:
- support for batch predictions.
- endpoints for model explanations using tools like SHAP or LIME.
- a /health check endpoint to integrate with monitoring tools.
- a /retrain endpoint that can accept new data and retrain your model.
These extensions are often explored in advanced modules data courses, giving students a full-stack understanding of deploying ML systems.
Final Thoughts
Creating APIs for machine learning models is a vital skill for any data scientist looking to make their models useful beyond Jupyter notebooks. FastAPI provides a fast, modern, and scalable solution that integrates seamlessly with Python and machine learning tools. FastAPI offers high performance, automatic interactive documentation, and seamless integration with Python type hints. It simplifies API development, ensures data validation, and supports asynchronous programming. Given its excellent developer experience, it is ideal for building modern, scalable web applications, and microservices quickly and efficiently.
Whether you are building prototypes, entering a hackathon, or working on your capstone project in a Data Scientist Course, FastAPI can help you turn your models into powerful, user-facing applications with minimal hassle.
With clear syntax, automatic docs, and great performance, FastAPI is a go-to framework for modern ML deployment—and one that every data scientist should have in their toolkit.
Business Name: ExcelR- Data Science, Data Analytics, Business Analyst Course Training Mumbai
Address: Unit no. 302, 03rd Floor, Ashok Premises, Old Nagardas Rd, Nicolas Wadi Rd, Mogra Village, Gundavali Gaothan, Andheri E, Mumbai, Maharashtra 400069, Phone: 09108238354, Email: enquiry@excelr.com.

