HNG DevOps - Stage 1

HNG DevOps - Stage 1

Number Classification API

Introduction

As part of my HNG internship, I built a Number Classification API that takes an integer and returns its mathematical properties along with a fun fact. This project tested my API development, deployment, and troubleshooting skills.

In this post, I'll walk you through the entire process, from setting up the Flask API to deploying it on Render.


1️⃣ Setting Up the Project

Step 1: Install Python and Flask

First, ensure you have Python installed. If not, download it from python.org.

Then, create a project directory and set up a virtual environment:

mkdir number-classification-api && cd number-classification-api
python -m venv venv
source venv/bin/activate  # On Windows, use venv\Scripts\activate

Now, install Flask:

pip install flask requests

2️⃣ Developing the API

Step 2: Create the Flask Application

Inside your project directory, create a file called app.py and add the following code:

import os
import requests
from flask import Flask, request, jsonify

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

def is_armstrong(n):
    digits = [int(d) for d in str(n)]
    return sum(d ** len(digits) for d in digits) == n

def classify_number(n):
    properties = []
    if is_armstrong(n):
        properties.append("armstrong")
    properties.append("even" if n % 2 == 0 else "odd")
    return properties

def get_fun_fact(n):
    response = requests.get(f"http://numbersapi.com/{n}/math")
    return response.text if response.status_code == 200 else "No fact available."

app = Flask(__name__)

@app.route("/api/classify-number")
def classify():
    number = request.args.get("number")
    if not number or not number.isdigit():
        return jsonify({"number": number, "error": True}), 400

    number = int(number)
    return jsonify({
        "number": number,
        "is_prime": is_prime(number),
        "is_perfect": False,  # Placeholder for perfection check
        "properties": classify_number(number),
        "digit_sum": sum(int(d) for d in str(number)),
        "fun_fact": get_fun_fact(number)
    })

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host="0.0.0.0", port=port)

3️⃣ Testing the API Locally

Step 3: Run the Flask Server

Start the server by running:

python app.py

Visit the following URL in your browser:

http://127.0.0.1:5000/api/classify-number?number=371

Expected response:

{
    "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"
}

4️⃣ Deploying to Render

Step 4: Prepare for Deployment

Create a requirements.txt file:

pip freeze > requirements.txt

Also, create a Procfile (without any file extension) and add the following line:

web: python app.py

Step 5: Push Code to GitHub

Initialize a Git repository and push the project:

git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <your-github-repo-url>
git push -u origin main

Step 6: Deploy on Render

  1. Log in to Render and create a new Web Service.

  2. Connect your GitHub repository.

  3. Set the start command as:

python app.py
  1. Click Deploy.

Step 7: Access the Deployed API

Once deployment is successful, Render provides a public URL, e.g.,

https://your-app-name.onrender.com/api/classify-number?number=371

Now, you can access your API from anywhere! 🎉


5️⃣ Conclusion

This project helped me:

✅ Build and structure a Flask API.
✅ Implement mathematical logic in Python.
✅ Deploy an API on Render and troubleshoot issues.

Got questions? Drop them in the comments! 🚀

#DevOps #Python #Flask #APIDevelopment #CloudComputing #Render #Internship #LearningByDoing