
- Set up project structure with FastAPI and SQLite - Implement user authentication with JWT - Create database models for users, events, bets, and transactions - Add API endpoints for user management - Add API endpoints for events and betting functionality - Add wallet management for deposits and withdrawals - Configure Alembic for database migrations - Add linting with Ruff - Add documentation in README
4.1 KiB
4.1 KiB
Betting Application API
A FastAPI-based backend for a betting application. This API provides functionality for user management, betting on events, wallet operations, and more.
Features
- User authentication and management
- Event creation and management
- Betting functionality
- Wallet management (deposits, withdrawals)
- Transaction history
- Admin features for event management and bet settlement
Requirements
- Python 3.9+
- SQLite database
Installation
- Clone the repository:
git clone https://github.com/yourusername/betting-application-api.git
cd betting-application-api
- Set up a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Create a
.env
file in the root directory with the following content (customize as needed):
# App configuration
SECRET_KEY=yoursecretkey
API_V1_STR=/api/v1
PROJECT_NAME=betting-application-api
ENVIRONMENT=development
# JWT Settings
ACCESS_TOKEN_EXPIRE_MINUTES=30
JWT_ALGORITHM=HS256
JWT_SECRET_KEY=your_jwt_secret_key
# Security
ALLOWED_HOSTS=["*"]
# Admin user
FIRST_SUPERUSER_EMAIL=admin@example.com
FIRST_SUPERUSER_PASSWORD=admin123
- Initialize the database and run migrations:
alembic upgrade head
Running the API
Start the API server:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
Authentication
POST /api/v1/auth/login
- Get access token (OAuth2)
Users
POST /api/v1/users
- Register a new userGET /api/v1/users/me
- Get current user informationPUT /api/v1/users/me
- Update current user informationGET /api/v1/users/{user_id}
- Get user by ID (admin or self)GET /api/v1/users
- List all users (admin only)
Events
GET /api/v1/events
- List all events (filter by status)POST /api/v1/events
- Create a new event (admin only)GET /api/v1/events/{event_id}
- Get event by IDPUT /api/v1/events/{event_id}
- Update event (admin only)DELETE /api/v1/events/{event_id}
- Delete event (admin only)POST /api/v1/events/outcomes/{outcome_id}/settle
- Settle an outcome (admin only)
Bets
GET /api/v1/bets
- List user's bets (filter by status)POST /api/v1/bets
- Place a new betGET /api/v1/bets/{bet_id}
- Get bet by IDPOST /api/v1/bets/{bet_id}/cancel
- Cancel a pending bet
Wallet
GET /api/v1/wallet/balance
- Get user's balanceGET /api/v1/wallet/transactions
- Get user's transaction historyPOST /api/v1/wallet/deposit
- Deposit fundsPOST /api/v1/wallet/withdraw
- Withdraw funds
Health Check
GET /health
- API health check
Models
User
- id: Unique identifier
- email: User's email (unique)
- full_name: User's full name
- is_active: User account status
- is_admin: Admin privileges flag
- balance: User's wallet balance
Event
- id: Unique identifier
- name: Event name
- description: Event description
- start_time: When the event starts
- end_time: When the event ends
- status: upcoming, live, finished, or cancelled
Market
- id: Unique identifier
- event_id: Associated event
- name: Market name
- is_active: Market availability status
Outcome
- id: Unique identifier
- market_id: Associated market
- name: Outcome name
- odds: Betting odds
- is_winner: Result status
- is_active: Outcome availability status
Bet
- id: Unique identifier
- user_id: User who placed the bet
- outcome_id: The selected outcome
- amount: Bet amount
- odds: Odds at time of bet
- potential_win: Potential winnings
- status: pending, won, lost, cancelled, or voided
Transaction
- id: Unique identifier
- user_id: Associated user
- amount: Transaction amount
- transaction_type: deposit, withdrawal, bet_placed, bet_won, bet_lost, or bet_refund
- status: pending, completed, failed, or cancelled
- reference: External reference (optional)
- bet_id: Associated bet (optional)
License
MIT