Number Classification API
Overview
The Number Classification API is a FastAPI-powered web service that classifies numbers based on their mathematical properties and provides a fun fact. It determines whether a number is prime, perfect, Armstrong, even, or odd and fetches an interesting fact from an external API.
Features
- Classifies numbers based on mathematical properties.
- Fetches fun facts using the Numbers API.
- Provides a structured JSON response.
- Handles errors gracefully and ensures valid integer input.
- Publicly accessible and deployed on Google Cloud Compute Engine (GCE).
API Endpoint
Classify a Number
GET /api/classify-number?number=<integer>
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
number | int | Yes | The number to classify |
Successful Response (200 OK)
{ "number": 371, "is_prime": false, "is_perfect": false, "properties": ["armstrong", "odd"], "digit_sum": 11, "fun_fact": "371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371" }
Error Response (400 Bad Request)
{ "number": "abc", "error": true, "message": "Invalid input. Please enter an integer." }
Deployment Details
- Framework: FastAPI (Python)
- Hosting Platform: Google Cloud Compute Engine (GCE)
- Port: 8000
- CORS Handling: Enabled to allow cross-origin requests
Installation & Setup
Prerequisites
- Python 3.8+
- FastAPI & Uvicorn
- Virtual Environment (optional but recommended)
Steps to Run Locally
# Clone the repository git clone https://github.com/onlyfave/Number-classification-api.git cd Number-classification-api # Create a virtual environment python3 -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate # Install dependencies pip install -r requirements.txt # Run the FastAPI server uvicorn main:app --host 0.0.0.0 --port 8000 --reload
Deployment on Google Cloud Compute Engine (GCE)
- Create a VM Instance on GCE.
- Install dependencies:
sudo apt update && sudo apt install python3-pip pip install fastapi uvicorn
- Run the API in the background:
nohup uvicorn main:app --host 0.0.0.0 --port 8000 &
- Allow external traffic on port 8000:
gcloud compute firewall-rules create allow-fastapi \ --allow tcp:8000 \ --target-tags=http-server \ --description="Allow FastAPI traffic"
or do it manually in the VPC Firewall settings
- Verify API is accessible:
curl http://35.209.49.217:8000/
Project Structure
Number-classification-api/ │── main.py # FastAPI application │── requirements.txt # Project dependencies │── README.md # Documentation (this file) └── .gitignore # Git ignore file
Screenshots
Python file(main.py)
Example API Response
API Running Successfully
Pushed to GitHub
Technologies Used
Testing the API
You can test the API using cURL, Postman, or a web browser:
curl "http://35.209.49.217:8000/api/classify-number?number=42"
Author
- Favour Onyeneke
- GitHub link: onlyfave
Conclusion
The Number Classification API is a lightweight and efficient API that provides mathematical insights and fun facts about numbers. Future improvements may include support for additional number properties and performance optimizations.🔥
Top comments (0)