Overview
The SoundCloud service provides server-side integration with the SoundCloud API using OAuth2 client credentials flow. It manages authentication tokens with automatic caching, retrieves user information, and fetches user tracks with pagination support. Source Location:src/server/services/soundcloud/
This service is designed to run exclusively on the server. Never expose authentication credentials or tokens to the client.
Service API Object
The service exports a unified API object for organized access to all methods:src/server/services/soundcloud/index.ts:9
Authentication
getSoundCloudToken()
Retrieves a valid SoundCloud access token using OAuth2 client credentials flow. Implements automatic caching with expiration management. Location:src/server/services/soundcloud/auth.ts:21
Returns
Valid SoundCloud access token for API requests
Authentication Flow
- Cache Check: Verifies if valid token exists in cache
- Token Request: If cache is empty/expired, requests new token
- Authorization: Uses custom headers with client credentials
- Caching: Stores token with 60-second safety buffer before expiration
Request Configuration
SoundCloud application client ID
SoundCloud application client secret
Custom authorization header from configuration
OAuth2 grant type (URL-encoded form data)
Response Type: SoundCloudToken
Error Handling
User Methods
getUserInfo()
Retrieves complete user information by username or user ID. Results are cached for 1 hour. Location:src/server/services/soundcloud/users.ts:14
Parameters
SoundCloud username (string) or numeric user ID
Returns
Complete user object, or
null if user not found or error occursResponse Type: SoundCloudUser
API Endpoint
Request Headers
application/jsonBearer {access_token}Example Usage
Error Handling
Returnsnull on errors:
- User not found
- Authentication failures
- Network errors
- Invalid API responses
Track Methods
getUserTracks()
Retrieves tracks uploaded by a specific user with pagination support. Results are cached for 1 hour per unique query. Location:src/server/services/soundcloud/tracks.ts:17
Parameters
SoundCloud username (string) or numeric user ID
Maximum number of tracks to return (pagination)
Number of tracks to skip (pagination)
Returns
Array of track objects, or
null if user not found or error occursResponse Type: SoundCloudTrack
API Endpoint
Request Headers
application/jsonBearer {access_token}Example Usage
Implementation Details
- User Resolution: If username provided, calls
getUserInfo()to resolve to user ID - Cache Check: Verifies cache using key pattern
user_tracks_{userId}_limit{limit}_offset{offset} - Token Retrieval: Obtains valid access token via
getSoundCloudToken() - API Request: Fetches tracks from SoundCloud API
- Cache Storage: Stores results for 1 hour
Error Handling
Returnsnull on errors:
- User not found (invalid username/ID)
- Authentication failures
- Network errors
- Invalid pagination parameters
Caching Strategy
Token Caching
"soundcloud_token"expires_in - 60 seconds (60-second safety buffer)User Info Caching
"user_{username}"3600 seconds (1 hour)
User Tracks Caching
"user_tracks_{userId}_limit{limit}_offset{offset}"3600 seconds (1 hour)
Caching is implemented using
getFromCache and setInCache utilities from @/server/services/cache. Each unique pagination query is cached separately.Environment Variables
SoundCloud application client ID from SoundCloud Developer Portal
SoundCloud application client secret
Custom authorization header value for OAuth2 flow
SoundCloud OAuth2 token endpoint URL
Error Messages
| Error | Cause | Solution |
|---|---|---|
| ”Fallo en la autenticación de SoundCloud” | Token request failed | Verify client credentials and authorization header |
| ”Se requiere un nombre de usuario” | Empty username parameter | Provide valid username or user ID |
| ”No se pudo encontrar al usuario “ | User doesn’t exist | Verify username spelling and existence |
Method returns null | API request failed | Check logs for specific error details |
Dependencies
@/lib/BaseFetcher- HTTP request wrapper@/server/constants- Environment variable configuration@/server/services/cache- Token and response caching@/types/soundcloud/token.d- Type definitions for token response@/types/soundcloud/track.d- Type definitions for track objects@/types/soundcloud/user.d- Type definitions for user objects