This document provides comprehensive information about the APIs and services integrated into Echo Music.
- Overview
- YouTube Music API
- Spotify Web API
- AI Services
- Third-Party Services
- Authentication
- Rate Limiting
- Error Handling
- API Security
Echo Music integrates with multiple APIs and services to provide a comprehensive music streaming experience:
- YouTube Music: Primary music streaming source
- Spotify: Secondary music source with enhanced features
- AI Services: For song recommendations and lyrics
- Third-Party Services: For lyrics, translations, and other features
Echo Music uses YouTube Music's internal API through reverse engineering and scraping techniques. This provides access to YouTube Music's vast catalog without official API access.
// Search for songs, artists, albums, playlists
GET /youtubei/v1/searchParameters:
query: Search query stringtype: Content type (song, artist, album, playlist)limit: Number of results to return
Response:
{
"contents": {
"sectionListRenderer": {
"contents": [
{
"musicShelfRenderer": {
"contents": [
{
"musicResponsiveListItemRenderer": {
"flexColumns": [
{
"musicResponsiveListItemFlexColumnRenderer": {
"text": {
"runs": [
{
"text": "Song Title"
}
]
}
}
}
]
}
}
]
}
}
]
}
}
}// Get detailed information about a song
GET /youtubei/v1/playerParameters:
videoId: YouTube video IDplaybackContext: Playback context information
// Get playlist contents
GET /youtubei/v1/browseParameters:
browseId: Playlist IDparams: Additional parameters
YouTube Music requires authentication for full access:
class YouTubeAuthManager {
suspend fun authenticate(email: String, password: String): AuthResult
suspend fun refreshToken(): AuthResult
suspend fun logout()
}- Search: 100 requests per minute
- Streaming: No specific limits
- Playlist Operations: 50 requests per minute
Echo Music integrates with Spotify's official Web API for enhanced features like Canvas, lyrics, and high-quality audio.
// Search for tracks, artists, albums
GET https://api.spotify.com/v1/searchParameters:
q: Search querytype: Content type (track, artist, album)limit: Number of results (1-50)offset: Pagination offset
Response:
{
"tracks": {
"items": [
{
"id": "track_id",
"name": "Track Name",
"artists": [
{
"id": "artist_id",
"name": "Artist Name"
}
],
"album": {
"id": "album_id",
"name": "Album Name",
"images": [
{
"url": "image_url",
"height": 640,
"width": 640
}
]
},
"preview_url": "preview_url",
"external_urls": {
"spotify": "spotify_url"
}
}
]
}
}// Get detailed track information
GET https://api.spotify.com/v1/tracks/{id}// Get audio analysis features
GET https://api.spotify.com/v1/audio-features/{id}// Get Spotify Canvas for track
GET https://spclient.wg.spotify.com/canvas/v1/canvases/{track_id}Spotify uses OAuth 2.0 with PKCE:
class SpotifyAuthManager {
suspend fun authenticate(): AuthResult
suspend fun refreshToken(): AuthResult
suspend fun getAccessToken(): String?
}- General API: 10,000 requests per hour
- Search: 1,000 requests per hour
- Audio Features: 100 requests per hour
Echo Music integrates with various AI services for enhanced user experience:
- OpenAI GPT: Song recommendations and descriptions
- Google Gemini: Alternative AI provider
- OpenRouter: Multi-provider AI access
class OpenAIService {
suspend fun getSongRecommendations(
playlist: List<Song>,
mood: String
): List<SongRecommendation>
suspend fun generatePlaylistDescription(
songs: List<Song>
): String
}API Endpoint:
POST https://api.openai.com/v1/chat/completions
Request:
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a music recommendation AI."
},
{
"role": "user",
"content": "Recommend songs similar to: [song list]"
}
],
"max_tokens": 500,
"temperature": 0.7
}class GeminiService {
suspend fun analyzeMusicMood(songs: List<Song>): MoodAnalysis
suspend fun generateLyrics(songInfo: SongInfo): String
}class LRCLibService {
suspend fun getLyrics(
trackName: String,
artistName: String
): LyricsResult
}API Endpoint:
GET https://lrclib.net/api/get
Parameters:
track_name: Song titleartist_name: Artist namealbum_name: Album name (optional)duration: Song duration (optional)
class SponsorBlockService {
suspend fun getSegments(videoId: String): List<SponsorSegment>
}API Endpoint:
GET https://sponsor.ajay.app/api/skipSegments
Parameters:
videoID: YouTube video IDcategories: Array of segment categories
class TranslationService {
suspend fun translateLyrics(
lyrics: String,
targetLanguage: String
): String
}data class YouTubeAuthResult(
val accessToken: String,
val refreshToken: String,
val expiresIn: Long,
val tokenType: String
)
class YouTubeAuthManager {
suspend fun authenticate(
email: String,
password: String
): YouTubeAuthResult
suspend fun refreshToken(): YouTubeAuthResult
fun isAuthenticated(): Boolean
suspend fun logout()
}data class SpotifyAuthResult(
val accessToken: String,
val refreshToken: String,
val expiresIn: Long,
val scope: String
)
class SpotifyAuthManager {
suspend fun authenticate(): SpotifyAuthResult
suspend fun refreshToken(): SpotifyAuthResult
fun isAuthenticated(): Boolean
suspend fun logout()
}class RateLimiter(
private val maxRequests: Int,
private val timeWindow: Duration
) {
private val requests = mutableListOf<Instant>()
suspend fun acquire(): Boolean {
val now = Instant.now()
val windowStart = now.minus(timeWindow)
// Remove old requests
requests.removeAll { it.isBefore(windowStart) }
return if (requests.size < maxRequests) {
requests.add(now)
true
} else {
false
}
}
}| Service | Limit | Window |
|---|---|---|
| YouTube Music | 100 requests | 1 minute |
| Spotify | 10,000 requests | 1 hour |
| OpenAI | 3,500 requests | 1 minute |
| LRCLIB | 1,000 requests | 1 hour |
| SponsorBlock | 10,000 requests | 1 hour |
sealed class ApiError : Exception() {
object NetworkError : ApiError()
object AuthenticationError : ApiError()
object RateLimitError : ApiError()
object ServerError : ApiError()
data class ClientError(val code: Int, val message: String) : ApiError()
}class ApiErrorHandler {
suspend fun <T> handleApiCall(
apiCall: suspend () -> T
): Result<T> {
return try {
Result.success(apiCall())
} catch (e: HttpException) {
when (e.code()) {
401 -> Result.failure(ApiError.AuthenticationError)
429 -> Result.failure(ApiError.RateLimitError)
500..599 -> Result.failure(ApiError.ServerError)
else -> Result.failure(ApiError.ClientError(e.code(), e.message()))
}
} catch (e: IOException) {
Result.failure(ApiError.NetworkError)
}
}
}class RetryManager {
suspend fun <T> retryWithBackoff(
maxRetries: Int = 3,
initialDelay: Duration = Duration.ofSeconds(1),
apiCall: suspend () -> T
): T {
var delay = initialDelay
repeat(maxRetries) { attempt ->
try {
return apiCall()
} catch (e: Exception) {
if (attempt == maxRetries - 1) throw e
delay(delay.toMillis())
delay = delay.multipliedBy(2) // Exponential backoff
}
}
throw IllegalStateException("Should not reach here")
}
}class ApiKeyManager {
private val encryptedPrefs: EncryptedSharedPreferences
fun storeApiKey(service: String, key: String) {
encryptedPrefs.edit()
.putString("api_key_$service", key)
.apply()
}
fun getApiKey(service: String): String? {
return encryptedPrefs.getString("api_key_$service", null)
}
}class RequestSigner {
fun signRequest(
url: String,
method: String,
headers: Map<String, String>,
body: String?
): Map<String, String> {
// Implement request signing logic
return headers + mapOf("Authorization" to generateSignature())
}
}class CertificatePinner {
fun createOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.certificatePinner(
CertificatePinner.Builder()
.add("api.spotify.com", "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
.add("api.openai.com", "sha256/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB=")
.build()
)
.build()
}
}class ApiMetricsCollector {
fun recordApiCall(
service: String,
endpoint: String,
duration: Duration,
success: Boolean
) {
// Record metrics for monitoring
}
fun recordRateLimit(service: String, endpoint: String) {
// Record rate limit events
}
}class ApiHealthChecker {
suspend fun checkServiceHealth(service: String): HealthStatus {
return try {
when (service) {
"youtube" -> checkYouTubeHealth()
"spotify" -> checkSpotifyHealth()
"openai" -> checkOpenAIHealth()
else -> HealthStatus.Unknown
}
} catch (e: Exception) {
HealthStatus.Unhealthy(e.message ?: "Unknown error")
}
}
}This API documentation provides a comprehensive overview of all the APIs and services integrated into Echo Music. For implementation details, refer to the source code in the respective modules.