A lightweight Flask REST API for performing basic mathematical operations — addition, subtraction, multiplication, division, and square.
Built with simplicity and learning in mind, this project helps you understand how to create and test RESTful APIs using Flask.
- ➕ Add two numbers
- ➖ Subtract two numbers
- ✖️ Multiply two numbers
- ➗ Divide two numbers (with zero-check)
- 🔲 Square a single number
- ✅ Includes basic error handling for invalid or missing inputs
- Python 3.x
- Flask (for building REST APIs)
- JSON (for input/output data format)
git clone https://github.com/your-username/flask-math-api.git
cd flask-math-api
pip install flask
python app.py
Once the server starts, the API will be available at:
Method | Endpoint | Description | Required Fields |
---|---|---|---|
POST | /add |
Add two numbers | num1 , num2 |
POST | /subtract |
Subtract two numbers | num1 , num2 |
POST | /multiply |
Multiply two numbers | num1 , num2 |
POST | /divide |
Divide two numbers | num1 , num2 |
POST | /square |
Find the square of a number | num |
GET | / |
Welcome route showing available endpoints | — |
Request Body (JSON):
{
"num1": 10,
"num2": 5
}
Response:
{
"operation": "addition",
"result": 15.0
}
➗ POST /divide (Example with Error) Request Body (JSON):
{
"num1": 10,
"num2": 0
}
Response:
{
"error": "Division by zero is not allowed."
}
You can easily test this API using Postman:
-
Open Postman and click on New → HTTP Request.
-
Set the method to POST.
-
Enter the desired endpoint, for example: http://127.0.0.1:5000/add
-
Go to the Body tab → select raw → choose JSON from the dropdown.
-
Enter your JSON data, for example:
{
"num1": 8,
"num2": 4
}
Click Send — you'll receive a JSON response like:
{
"operation": "addition",
"result": 12.0
}
Repeat the same for /subtract, /multiply, /divide, or /square by changing the endpoint and body accordingly.
-
Missing or invalid fields
-
Non-numeric input
-
Division by zero
-
Invalid JSON format
-
Example Error Response:
{
"error": "Invalid input. Please provide valid JSON with 'num1' and 'num2'."
}