Skip to main content

Overview

The Spotify service provides server-side integration with the Spotify Web API using OAuth2 client credentials flow. It handles authentication token management with automatic caching and expiration, and provides methods to retrieve artist top tracks. Source Location: src/server/services/spotify/
This service is designed to run exclusively on the server. Never expose authentication credentials to the client.

Authentication

getSpotifyToken()

Retrieves a valid Spotify access token using client credentials flow. Checks cache before requesting a new token to optimize API usage. Location: src/server/services/spotify/auth.ts:15
async function getSpotifyToken(): Promise<string>

Returns

access_token
string
required
Valid Spotify access token for API requests

Authentication Flow

  1. Cache Check: Verifies if a valid token exists in cache
  2. Token Request: If cache is empty/expired, requests new token from Spotify
  3. Authorization: Uses Basic Auth with Base64-encoded client_id:client_secret
  4. Caching: Stores token in cache with 60-second buffer before expiration

API Endpoint

POST https://accounts.spotify.com/api/token

Request Headers

Content-Type
string
required
application/x-www-form-urlencoded
Authorization
string
required
Basic {base64(client_id:client_secret)}

Request Body

grant_type
string
required
Must be client_credentials

Response Type: SpotifyToken

interface SpotifyToken {
  access_token: string;    // OAuth2 access token
  token_type: string;      // "Bearer"
  expires_in: number;      // Token lifetime in seconds
}

Error Handling

try {
  const token = await getSpotifyToken();
} catch (error) {
  // Throws: "Fallo en la autenticación de Spotify"
}
Requires SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET environment variables.

Track Methods

getArtistTopTracks()

Retrieves the most popular tracks for a configured artist. Results are cached for 1 hour to reduce API calls. Location: src/server/services/spotify/tracks.ts:14
async function getArtistTopTracks(): Promise<Track[] | null>

Parameters

No parameters required. Uses SPOTIFY_ARTIST_ID from environment configuration.

Returns

tracks
Track[] | null
Array of track objects, or null if error occurs or artist ID not configured

Response Type: Track

interface Track {
  album: Album;                    // Album information
  artists: Artist2[];              // Array of artist objects
  available_markets: string[];     // ISO country codes
  disc_number: number;             // Disc number (multi-disc albums)
  duration_ms: number;             // Track duration in milliseconds
  explicit: boolean;               // Explicit content flag
  external_ids: ExternalIds;       // ISRC, EAN, UPC identifiers
  external_urls: ExternalUrls4;    // Spotify URL
  href: string;                    // API endpoint for full track info
  id: string;                      // Spotify track ID
  is_playable: boolean;            // Playback availability
  linked_from: LinkedFrom;         // Track linking information
  restrictions: Restrictions2;     // Playback restrictions
  name: string;                    // Track name
  popularity: number;              // 0-100 popularity score
  preview_url: string;             // 30-second preview MP3 URL
  track_number: number;            // Track position on album
  type: string;                    // "track"
  uri: string;                     // Spotify URI
  is_local: boolean;               // Local file flag
}

interface Album {
  album_type: string;              // "album" | "single" | "compilation"
  total_tracks: number;            // Number of tracks
  available_markets: string[];     // ISO country codes
  external_urls: ExternalUrls;     // Spotify URL
  href: string;                    // API endpoint
  id: string;                      // Spotify album ID
  images: Image[];                 // Album artwork (various sizes)
  name: string;                    // Album name
  release_date: string;            // YYYY-MM-DD format
  release_date_precision: string;  // "year" | "month" | "day"
  restrictions: Restrictions;      // Content restrictions
  type: string;                    // "album"
  uri: string;                     // Spotify URI
  artists: Artist[];               // Album artists
}

interface Image {
  url: string;                     // Image URL
  height: number;                  // Image height in pixels
  width: number;                   // Image width in pixels
}

interface Artist {
  external_urls: ExternalUrls2;    // Spotify URL
  href: string;                    // API endpoint
  id: string;                      // Spotify artist ID
  name: string;                    // Artist name
  type: string;                    // "artist"
  uri: string;                     // Spotify URI
}

API Endpoint

GET https://api.spotify.com/v1/artists/{artist_id}/top-tracks?market=ES

Request Headers

Authorization
string
required
Bearer {access_token}

Example Usage

import { getArtistTopTracks } from '@/server/services/spotify/tracks';

const tracks = await getArtistTopTracks();
if (tracks) {
  tracks.forEach(track => {
    console.log(`${track.name} - ${track.artists[0].name}`);
    console.log(`Popularity: ${track.popularity}`);
    console.log(`Duration: ${track.duration_ms}ms`);
  });
}

Error Handling

Returns null on errors:
  • Missing SPOTIFY_ARTIST_ID environment variable
  • Authentication failures
  • Network errors
  • Invalid API responses
const tracks = await getArtistTopTracks();
if (!tracks) {
  console.error('Failed to retrieve tracks');
}

Caching Strategy

Token Caching

cache_key
string
"spotify_token"
duration
number
expires_in - 60 seconds (60-second safety buffer)

Track Caching

cache_key
string
"spotify_top_tracks_{artist_id}"
duration
number
3600 seconds (1 hour)
Caching is implemented using getFromCache and setInCache utilities from @/server/services/cache.

Environment Variables

SPOTIFY_CLIENT_ID
string
required
Spotify application client ID from Spotify Developer Dashboard
SPOTIFY_CLIENT_SECRET
string
required
Spotify application client secret
SPOTIFY_ARTIST_ID
string
required
Spotify artist ID for getArtistTopTracks() method

Error Messages

ErrorCauseSolution
”Fallo en la autenticación de Spotify”Token request failedVerify client credentials and network connectivity
”No se ha definido un SPOTIFY_ARTIST_ID”Missing artist ID configurationSet SPOTIFY_ARTIST_ID environment variable
Method returns nullAPI request failedCheck logs for specific error details

Dependencies

  • @/lib/BaseFetcher - HTTP request wrapper
  • @/server/constants - Environment variable configuration
  • @/server/services/cache - Token and response caching
  • @/types/spotify/token.d - Type definitions for token response
  • @/types/spotify/topTracks.d - Type definitions for tracks response

Build docs developers (and LLMs) love