DEV Community

Cover image for From Code to Cloud: Building and Deploying a Smart Number Classification API with FastAPI
Fave😌✨
Fave😌✨

Posted on

From Code to Cloud: Building and Deploying a Smart Number Classification API with FastAPI

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" } 
Enter fullscreen mode Exit fullscreen mode

Error Response (400 Bad Request)

{ "number": "abc", "error": true, "message": "Invalid input. Please enter an integer." } 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Deployment on Google Cloud Compute Engine (GCE)

  1. Create a VM Instance on GCE.

Screenshot 2025-02-05 231115

  1. Install dependencies:
 sudo apt update && sudo apt install python3-pip pip install fastapi uvicorn 
Enter fullscreen mode Exit fullscreen mode
  1. Run the API in the background:
 nohup uvicorn main:app --host 0.0.0.0 --port 8000 & 
Enter fullscreen mode Exit fullscreen mode
  1. Allow external traffic on port 8000:
 gcloud compute firewall-rules create allow-fastapi \ --allow tcp:8000 \ --target-tags=http-server \ --description="Allow FastAPI traffic" 
Enter fullscreen mode Exit fullscreen mode

or do it manually in the VPC Firewall settings

Screenshot 2025-02-05 235933

  1. Verify API is accessible:
 curl http://35.209.49.217:8000/ 
Enter fullscreen mode Exit fullscreen mode

Project Structure

Number-classification-api/ │── main.py # FastAPI application │── requirements.txt # Project dependencies │── README.md # Documentation (this file) └── .gitignore # Git ignore file 
Enter fullscreen mode Exit fullscreen mode

Screenshots

Python file(main.py)

Screenshot 2025-02-05 234325

Example API Response

Screenshot 2025-02-05 233409

API Running Successfully

Screenshot 2025-02-06 001319

Pushed to GitHub

Screenshot 2025-02-05 234548

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" 
Enter fullscreen mode Exit fullscreen mode

Author

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)