A minimal FastAPI application that manages user information, implemented in main.py.
Overview
Endpoints:
- GET /info?email=user@example.com — lookup user by email
- POST /info — add new user with name and email
- GET /users/{user_id} — lookup user by ID
Models:
// POST /info request body
{
"name": "string (min length 1)",
"email": "valid-email@example.com"
}
// Response format
{
"name": "string",
"email": "email@example.com",
"phone": "string"
}Prerequisites
- Python 3.8+ installed and available as
python3. - Optional: a POSIX shell (macOS
zshis fine).
Quick setup (recommended)
- Open a terminal and change into the project directory:
cd /Users/abhimanyugupta/Desktop/Api- Create and activate a virtual environment, then install dependencies:
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install fastapi uvicorn[standard] pydantic email-validatorRun the app (development mode with auto-reload)
uvicorn main:app --reload --port 8000Run the app (production mode)
uvicorn main:app --host 0.0.0.0 --port 8000Run in background (with logs)
nohup uvicorn main:app --host 0.0.0.0 --port 8000 > server.log 2>&1 & echo $!Check logs:
tail -n 200 server.logStop a background server
Find the PID and stop it (replace <PID> with the numeric PID):
pgrep -af "python3 app.py" # shows running processes
kill <PID>
# if it doesn't stop:
kill -9 <PID>Testing the endpoints
Look up a user by email:
curl -s "http://localhost:8000/info?email=alice@example.com" | jqLook up a user by ID:
curl -s "http://localhost:8000/users/1" | jqAdd a new user:
curl -s -X POST "http://localhost:8000/info" \
-H "Content-Type: application/json" \
-d '{"name":"New User","email":"new@example.com"}' | jqInteractive API docs:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Notes
- The POST endpoint requires a JSON body with both
nameandemail- Name must be non-empty after trimming whitespace
- Email must be valid format
- Duplicate emails are rejected with 400 status
- The server runs on port 8000 by default
- Data is stored in memory (resets when server restarts)
- FastAPI provides automatic OpenAPI documentation
Optional: Production deployment
Using Gunicorn (Unix systems):
pip install gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000Docker setup:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir fastapi uvicorn[standard] pydantic email-validator
COPY main.py ./
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Build and run:
docker build -t user-directory-api .
docker run -p 8000:8000 user-directory-apiTroubleshooting
- If
python3is not found, install Python 3.8 or newer - If imports fail, verify you installed all dependencies:
pip install fastapi uvicorn[standard] pydantic email-validator
- If port 8000 is in use, change it:
uvicorn main:app --port 8001
- For "email not found" errors, check the exact email case (lookups are case-insensitive)
Project files
main.py— FastAPI app with user directory endpointsrequirements.txt— Python dependenciesserver.log— Optional log file when running with nohup
Future improvements
- Add data persistence (SQLite/PostgreSQL)
- Add PUT/DELETE endpoints
- Add authentication
- Add more user fields
- Add search by name
If you'd like, I can also:
- Add automated unit tests (pytest) and a small test runner.
- Add a sample
Docker Composeor asystemd/launchd service file for long-running deployments.
License
MIT (modify as needed)