Skip to content

Abhi-d-gr8/User-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI User Directory (GET & POST /info)

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 zsh is fine).

Quick setup (recommended)

  1. Open a terminal and change into the project directory:
cd /Users/abhimanyugupta/Desktop/Api
  1. 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-validator

Run the app (development mode with auto-reload)

uvicorn main:app --reload --port 8000

Run the app (production mode)

uvicorn main:app --host 0.0.0.0 --port 8000

Run 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.log

Stop 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" | jq

Look up a user by ID:

curl -s "http://localhost:8000/users/1" | jq

Add a new user:

curl -s -X POST "http://localhost:8000/info" \
  -H "Content-Type: application/json" \
  -d '{"name":"New User","email":"new@example.com"}' | jq

Interactive API docs:

Notes

  • The POST endpoint requires a JSON body with both name and email
    • 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:8000

Docker 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-api

Troubleshooting

  • If python3 is 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 endpoints
  • requirements.txt — Python dependencies
  • server.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 Compose or a systemd/launchd service file for long-running deployments.

License

MIT (modify as needed)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages