Skip to content

This repository hosts the essential codes and documents for creating a sample RESTful API using Python and Flask.

License

Notifications You must be signed in to change notification settings

tvnisxq/restAPI-using-Python-Flask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

restAPI (Python + Flask)

Project Banner

A small example REST API using Python and Flask. This repository contains a minimal Flask app in src/main.py with a root endpoint and an Armstrong-number checker endpoint.

Features

  • GET / — returns a simple greeting ("Hello, World!").
  • GET /armstrong/<int:n> — returns whether n is an Armstrong number as JSON.

Requirements

  • Python 3.8+ recommended. If you use Python 3.13, be aware some older package wheels may not support it; prefer 3.11 for maximum compatibility.
  • Flask (install into the virtual environment used to run the app).

Quick start (Windows, cmd.exe)

  1. Open a cmd prompt and change to the project folder.

Features

Project Banner

  • GET / — returns a simple greeting ("Hello, World!").
  • GET /armstrong/<int:n> — returns whether n is an Armstrong number.
  • GET /palindrome/<int:n> — returns whether n is a palindrome (supports ordinary integers, including 4-digit numbers like 1221).
  • GET /sum?a=2&b=3 — returns the sum of a and b (query params).
  • POST /sum — accepts JSON { "a": <int>, "b": <int> } and returns the sum.
  • GET /avg?a=2&b=3 — returns the average of a and b (query params).

Requirements

  • Python 3.8+ recommended. If you use Python 3.13, some wheels may not be available; prefer 3.11 if you run into install problems.
  • See requirements.txt for the minimal dependencies to run and test the project.
  1. (Optional) Create and activate a virtual environment:
python -m venv .venv
.venv\Scripts\activate.bat
  1. Install dependencies:
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
  1. Run the app:
python src\main.py

By default the app runs on http://127.0.0.1:5000.

API endpoints & examples

Root

curl http://127.0.0.1:5000/
# {"message":"Hello, World!"}

Armstrong

curl http://127.0.0.1:5000/armstrong/153
# {"Number":153,"Armstrong":true,"Server IP":"127.0.0.1:5000"}

Palindrome

curl http://127.0.0.1:5000/palindrome/1221
# {"Number":1221,"Palindrome":true,"Server IP":"127.0.0.1:5000"}

Sum (GET, query params)

curl "http://127.0.0.1:5000/sum?a=2&b=3"
# {"a":2,"b":3,"result":5}

Sum (POST, JSON body)

curl -H "Content-Type: application/json" -d "{\"a\":2,\"b\":3}" http://127.0.0.1:5000/sum
# {"a":2,"b":3,"result":5}

Average (direct function)

from src.main import avg
print(avg(2, 3))  # 2.5

Testing

Tests are in the tests/ folder and use pytest.

Install test requirements and run tests:

python -m pip install -r requirements.txt
python -m pytest -q

About

This repository hosts the essential codes and documents for creating a sample RESTful API using Python and Flask.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages