Music Streaming API
A RESTful API for a music streaming service built with FastAPI and SQLite.
Features
- User authentication (register, login, JWT tokens)
- Music management (songs, albums, artists)
- Playlist creation and management
- Music streaming
- Search functionality
- Basic recommendation system
- File uploads for audio and images
Tech Stack
- FastAPI: High-performance web framework for building APIs
- SQLite: Lightweight relational database
- SQLAlchemy: SQL toolkit and Object-Relational Mapper
- Alembic: Database migration tool
- Pydantic: Data validation and settings management
- JWT: JSON Web Tokens for authentication
- Uvicorn: ASGI server for hosting the application
Project Structure
/
├── alembic/ # Database migration scripts
├── app/ # Application source code
│ ├── api/ # API endpoints
│ │ ├── deps.py # API dependencies
│ │ └── v1/ # API version 1
│ │ ├── api.py # Main API router
│ │ └── endpoints/ # API endpoint modules
│ ├── core/ # Core modules
│ │ ├── config.py # Configuration settings
│ │ └── security.py # Security utilities
│ ├── db/ # Database
│ │ ├── init_db.py # Database initialization
│ │ └── session.py # Database session
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic models/schemas
│ ├── services/ # Business logic services
│ └── utils/ # Utility functions
├── storage/ # Storage for files
│ ├── db/ # Database files
│ └── media/ # Media files (audio, images)
├── main.py # Application entry point
├── alembic.ini # Alembic configuration
└── requirements.txt # Python dependencies
Setup and Installation
-
Clone the repository:
git clone <repository-url> cd music-streaming-api
-
Create virtual environment (optional):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables (create a
.env
file in the root directory):SECRET_KEY=your-secret-key ACCESS_TOKEN_EXPIRE_MINUTES=11520 # 8 days
-
Run database migrations:
alembic upgrade head
-
Initialize the database (creates admin user):
# Access the endpoint or run the Python script curl http://localhost:8000/init-db
-
Start the server:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
API Endpoints
The API is organized into the following groups:
Authentication
POST /api/v1/auth/register
- Register a new userPOST /api/v1/auth/login
- Login to get access tokenGET /api/v1/auth/me
- Get current user information
Users
GET /api/v1/users/
- List users (admin only)POST /api/v1/users/
- Create a new user (admin only)GET /api/v1/users/me
- Get current userPUT /api/v1/users/me
- Update current userGET /api/v1/users/{user_id}
- Get a specific userPUT /api/v1/users/{user_id}
- Update a user (admin only)DELETE /api/v1/users/{user_id}
- Delete a user (admin only)
Artists
GET /api/v1/artists/
- List artistsPOST /api/v1/artists/
- Create a new artist (admin only)GET /api/v1/artists/{artist_id}
- Get a specific artistPUT /api/v1/artists/{artist_id}
- Update an artist (admin only)DELETE /api/v1/artists/{artist_id}
- Delete an artist (admin only)
Albums
GET /api/v1/albums/
- List albumsPOST /api/v1/albums/
- Create a new album (admin only)GET /api/v1/albums/{album_id}
- Get a specific albumPUT /api/v1/albums/{album_id}
- Update an album (admin only)DELETE /api/v1/albums/{album_id}
- Delete an album (admin only)
Songs
GET /api/v1/songs/
- List songsPOST /api/v1/songs/
- Create a new song (admin only)GET /api/v1/songs/{song_id}
- Get a specific songPUT /api/v1/songs/{song_id}
- Update a song (admin only)DELETE /api/v1/songs/{song_id}
- Delete a song (admin only)
Playlists
GET /api/v1/playlists/
- List user's playlistsGET /api/v1/playlists/public
- List public playlistsPOST /api/v1/playlists/
- Create a new playlistGET /api/v1/playlists/{playlist_id}
- Get a specific playlistPUT /api/v1/playlists/{playlist_id}
- Update a playlistDELETE /api/v1/playlists/{playlist_id}
- Delete a playlistPOST /api/v1/playlists/{playlist_id}/songs
- Add a song to a playlistDELETE /api/v1/playlists/{playlist_id}/songs
- Remove a song from a playlist
Streaming
GET /api/v1/stream/song/{song_id}
- Stream a songGET /api/v1/stream/album/cover/{album_id}
- Get album cover imageGET /api/v1/stream/artist/image/{artist_id}
- Get artist image
Upload
POST /api/v1/upload/song
- Upload a song file (admin only)POST /api/v1/upload/image
- Upload an image file (admin only)
Recommendations
GET /api/v1/recommendations/similar-songs/{song_id}
- Get similar songsGET /api/v1/recommendations/for-user
- Get personalized recommendationsGET /api/v1/recommendations/popular
- Get popular songsGET /api/v1/recommendations/similar-artists/{artist_id}
- Get similar artists
Other
GET /health
- Health check endpointGET /init-db
- Initialize the database with an admin user
API Documentation
Once the application is running, you can access the API documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Environment Variables
Variable | Description | Default |
---|---|---|
SECRET_KEY | Secret key for JWT tokens | Auto-generated |
ACCESS_TOKEN_EXPIRE_MINUTES | Minutes before JWT token expires | 11520 (8 days) |
Default Admin User
When the database is initialized, a default admin user is created:
- Email: admin@example.com
- Username: admin
- Password: adminpassword
It's recommended to change the password immediately after first login in a production environment.
Development
Running Tests
# To be implemented
pytest
Database Migrations
To create a new migration after changing models:
alembic revision --autogenerate -m "Description of changes"
alembic upgrade head
License
This project is licensed under the MIT License.
Description
Languages
Python
99.3%
Mako
0.7%