Notes for Lesson on 4/30
A simple FastAPI project demonstrating basic API endpoints for reading, iterating, and managing names and numbers.
This project uses FastAPI to expose a few simple REST endpoints:
- Read a greeting message
- Read and increment a counter stored in
number.txt - Manage an in‑memory list of names
- Set and get a separate numeric value
- Python 3.10+
- pip
- Clone the repo or download the files.
- (Optional) Create a virtual environment:
python -m venv .venv source .venv/bin/activate - Install dependencies:
pip install -r requirements.txt
Start the server with Uvicorn:
uvicorn main:app --reloadBy default, it listens on http://127.0.0.1:8000.
| Method | Path | Description |
|---|---|---|
| GET | / |
Returns a greeting JSON |
| GET | /iterate |
Reads, returns, then increments number.txt |
| GET | /names |
Returns JSON list of added names |
| POST | /addname/{name} |
Appends name to list, returns name |
| PUT | /set_right_num/{s_num} |
Sets internal right_num, returns it |
| GET | /right_num |
Returns the current right_num |
All endpoints are defined in main.py.
You can test endpoints via curl, Python requests, or the included Jupyter notebook.
# Greeting
curl http://127.0.0.1:8000/
# Iterate number
curl http://127.0.0.1:8000/iterate
# Add a name
curl -X POST http://127.0.0.1:8000/addname/elias
# Set right_num
curl -X PUT http://127.0.0.1:8000/set_right_num/6
# Get right_num
curl http://127.0.0.1:8000/right_numimport requests
# Add a name
resp = requests.post("http://127.0.0.1:8000/addname/alice")
print(resp.json())
# Get names
resp = requests.get("http://127.0.0.1:8000/names")
print(resp.json())Open and run request.ipynb for live examples.
main.py– FastAPI applicationrequirements.txt– Python dependenciesnumber.txt– Counter storagerequest.ipynb– Example usage notebook- .gitignore – Ignored files list
- APIs in Python.pdf – Lesson material PDF
LICENSE– MIT LicenseCODE_OF_CONDUCT.md– Contributor Covenant
Please open issues or pull requests via GitHub. Use the templates in .github/ISSUE_TEMPLATE/.
This project is licensed under the MIT License. See LICENSE for details.