A simple poll application built with FastAPI, SQLite, and JWT authentication. Users can register, log in, create, retrieve, vote on, and delete polls. The project follows best practices with modular code in the api/
directory.
- User registration and login (JWT authentication)
- Create, retrieve, and delete polls
- Add options to polls (minimum of two options required)
- Vote on polls (authenticated users only)
- View poll results with vote counts
- SQLite database with SQLAlchemy ORM
- Modular code structure for maintainability
Polly-API/
├── api/
│ ├── __init__.py
│ ├── auth.py
│ ├── database.py
│ ├── models.py
│ ├── routes.py
│ └── schemas.py
├── main.py
├── requirements.txt
└── README.md
- Clone the repository
git clone <your-repo-url>
cd Polly-API
- Set up a Python virtual environment (recommended)
A virtual environment helps isolate your project dependencies.
-
On Unix/macOS:
python3 -m venv venv source venv/bin/activate
-
On Windows (cmd):
python -m venv venv venv\Scripts\activate
-
On Windows (PowerShell):
python -m venv venv .\venv\Scripts\Activate.ps1
To deactivate the virtual environment, simply run:
deactivate
- Install dependencies
pip install -r requirements.txt
- Set environment variables (optional)
Create a .env
file in the project root to override the default secret key:
SECRET_KEY=your_super_secret_key
- Run the application
uvicorn main:app --reload
The API will be available at http://127.0.0.1:8000
.
- Endpoint:
POST /register
- Body:
{
"username": "yourusername",
"password": "yourpassword"
}
- Endpoint:
POST /login
- Body (form):
username
: yourusernamepassword
: yourpassword
- Response:
{
"access_token": "...",
"token_type": "bearer"
}
- Endpoint:
GET /polls
- Query params:
skip
(default 0),limit
(default 10) - Authentication: Not required
- Endpoint:
POST /polls
- Headers:
Authorization: Bearer <access_token>
- Body:
{
"question": "Your poll question",
"options": ["Option 1", "Option 2"]
}
- Endpoint:
GET /polls/{poll_id}
- Authentication: Not required
- Endpoint:
POST /polls/{poll_id}/vote
- Headers:
Authorization: Bearer <access_token>
- Body:
{
"option_id": 1
}
- Endpoint:
GET /polls/{poll_id}/results
- Authentication: Not required
- Response:
{
"poll_id": 1,
"question": "Your poll question",
"results": [
{
"option_id": 1,
"text": "Option 1",
"vote_count": 3
},
{
"option_id": 2,
"text": "Option 2",
"vote_count": 1
}
]
}
- Endpoint:
DELETE /polls/{poll_id}
- Headers:
Authorization: Bearer <access_token>
Visit http://127.0.0.1:8000/docs for the interactive Swagger UI.
MIT License