A scalable and high‑performance FastAPI backend service that exposes REST APIs for predicting insurance premiums using a trained machine learning model. This backend is designed to integrate seamlessly with frontend applications (React, mobile apps) or other services.
- ⚡ Built with FastAPI for speed and modern API development
- 🤖 Machine Learning–powered insurance premium prediction
- 📄 Automatic API documentation with Swagger UI and ReDoc
- 🧩 Clean, modular project structure
- 🔌 Easy integration with frontend and external systems
- ☁️ Ready for cloud deployment
insurance-prediction-backend-fastapi/
├── model/ # Saved trained ML models
├── model_train/ # Model training and preprocessing scripts
├── app.py # FastAPI application entry point
├── database.py # Database configuration (optional / extensible)
├── models.py # Pydantic / ORM models
├── requirements.txt # Python dependencies
├── .gitignore
└── README.md
| Layer | Technology |
|---|---|
| Backend Framework | FastAPI |
| Language | Python 3.8+ |
| Server | Uvicorn |
| ML Library | scikit-learn |
| API Docs | Swagger UI / ReDoc |
Once the server is running, FastAPI provides interactive documentation automatically:
| Purpose | URL |
|---|---|
| Swagger UI | /docs |
| ReDoc | /redoc |
| Base URL | / |
POST /predict
{
"age": 45,
"bmi": 28.0,
"children": 2,
"smoker": "yes",
"gender": "male",
"region": "southwest"
}{
"predicted_premium": 12345.67
}
⚠️ Input fields depend on the trained model. Update request schema as per your implementation.
git clone https://github.com/harinandanmv/insurance-prediction-backend-fastapi.git
cd insurance-prediction-backend-fastapipython -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activatepip install -r requirements.txtuvicorn app:app --reloadServer will start at:
http://127.0.0.1:8000
You can test the backend using:
- Swagger UI (
/docs) - ReDoc (
/redoc) - Postman / Insomnia
- curl
curl -X POST "http://127.0.0.1:8000/predict" \
-H "Content-Type: application/json" \
-d '{"age":30,"bmi":26,"children":1,"smoker":"no","gender":"female","region":"northwest"}'The model_train/ directory contains scripts used to:
- Load and preprocess the dataset
- Train the insurance prediction model
- Save the trained model to the
model/directory
This separation allows independent experimentation and model updates without affecting the API layer.
This backend can be deployed using:
- Docker
- AWS / GCP / Azure
- Render / Railway / Fly.io
- On‑premise servers
Contributions are welcome!
- Fork the repository
- Create a new branch
- Commit your changes
- Open a Pull Request
This project is licensed under the MIT License.