A simple Python REST API that verifies user existence by checking usernames from API headers against a local SQLite database.
- ✅ User verification via API header
- ✅ Local SQLite database
- ✅ Pre-populated with sample users (including "toms")
- ✅ Returns 200 for existing users, 404 for non-existing users
- ✅ Additional endpoints to list and add users
- Python 3.7 or higher
- pip (Python package manager)
- Install required packages:
pip install -r requirements.txt- Start the server:
python app.py- The API will start on
http://127.0.0.1:5000
The database will be automatically initialized with sample users: toms, alice, bob, charlie
Endpoint: GET /api/verify or POST /api/verify
Headers Required:
X-Username: <username>orUsername: <username>
Responses:
200 OK- User exists in database404 Not Found- User does not exist400 Bad Request- Username header missing
Example using curl:
# User exists (returns 200)
curl -H "X-Username: toms" http://127.0.0.1:5000/api/verify
# User doesn't exist (returns 404)
curl -H "X-Username: john" http://127.0.0.1:5000/api/verifyExample using PowerShell:
# User exists (returns 200)
Invoke-WebRequest -Uri "http://127.0.0.1:5000/api/verify" -Headers @{"X-Username"="toms"}
# User doesn't exist (returns 404)
Invoke-WebRequest -Uri "http://127.0.0.1:5000/api/verify" -Headers @{"X-Username"="john"}Example using Python:
import requests
# User exists (returns 200)
response = requests.get('http://127.0.0.1:5000/api/verify',
headers={'X-Username': 'toms'})
print(response.status_code) # 200
print(response.json())
# User doesn't exist (returns 404)
response = requests.get('http://127.0.0.1:5000/api/verify',
headers={'X-Username': 'john'})
print(response.status_code) # 404Endpoint: GET /api/users
Response: 200 OK with list of all users
Example:
curl http://127.0.0.1:5000/api/usersEndpoint: POST /api/users
Body: JSON with username
{
"username": "newuser"
}Responses:
201 Created- User added successfully400 Bad Request- Username missing409 Conflict- User already exists
Example:
curl -X POST -H "Content-Type: application/json" -d "{\"username\":\"newuser\"}" http://127.0.0.1:5000/api/usersEndpoint: GET /
Response: API information and available endpoints
- Type: SQLite (file-based, no separate server needed)
- File:
users.db(created automatically) - Table:
userswith columns:id,username,created_at
If you need to reset or manually initialize the database:
python init_db.pyOnce the server is running, test it using any of these methods:
-
Web Browser: Visit
http://127.0.0.1:5000/to see API info -
PowerShell:
Invoke-RestMethod -Uri "http://127.0.0.1:5000/api/verify" -Headers @{"X-Username"="toms"}
-
Postman:
- Create a GET request to
http://127.0.0.1:5000/api/verify - Add header:
X-Usernamewith valuetoms
- Create a GET request to
restapi/
├── app.py # Main Flask application
├── init_db.py # Database initialization script
├── requirements.txt # Python dependencies
├── README.md # This file
└── users.db # SQLite database (created automatically)
200 OK- User found in database201 Created- User successfully added400 Bad Request- Missing required header or body parameter404 Not Found- User not found in database409 Conflict- User already exists (when adding)
Press Ctrl+C in the terminal where the server is running.