Skip to main content

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:
import { soundCloudApi } from '@/server/services/soundcloud';

// Access methods via organized structure
const tracks = await soundCloudApi.tracks.getFromUser('username');
const userInfo = await soundCloudApi.users.getInfo('username');
Structure: src/server/services/soundcloud/index.ts:9
export const soundCloudApi = {
  tracks: {
    getFromUser: getUserTracks,
  },
  users: {
    getInfo: getUserInfo,
  },
};

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
async function getSoundCloudToken(): Promise<string>

Returns

access_token
string
required
Valid SoundCloud access token for API requests

Authentication Flow

  1. Cache Check: Verifies if valid token exists in cache
  2. Token Request: If cache is empty/expired, requests new token
  3. Authorization: Uses custom headers with client credentials
  4. Caching: Stores token with 60-second safety buffer before expiration

Request Configuration

client_id
string
required
SoundCloud application client ID
client_secret
string
required
SoundCloud application client secret
Authorization
string
required
Custom authorization header from configuration
grant_type
string
required
OAuth2 grant type (URL-encoded form data)

Response Type: SoundCloudToken

interface SoundCloudToken {
  access_token: string;    // OAuth2 access token
  expires_in: number;      // Token lifetime in seconds
  token_type: string;      // "Bearer"
  scope: string;           // Granted permission scopes
}

Error Handling

try {
  const token = await getSoundCloudToken();
} catch (error) {
  // Throws: "Fallo en la autenticación de SoundCloud"
}
Requires SOUNDCLOUD_CLIENT_ID, SOUNDCLOUD_CLIENT_SECRET, and SOUNDCLOUD_AUTHORIZATION environment variables.

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
async function getUserInfo(
  user: string | number
): Promise<SoundCloudUser | null>

Parameters

user
string | number
required
SoundCloud username (string) or numeric user ID

Returns

user
SoundCloudUser | null
Complete user object, or null if user not found or error occurs

Response Type: SoundCloudUser

interface SoundCloudUser {
  id: number;                  // Unique user ID
  username: string;            // Display username
  permalink: string;           // URL-safe username
  uri: string;                 // API URI
  avatar_url: string;          // Profile picture URL
  city: string | null;         // User location city
  country: string | null;      // User location country
  followers_count: number;     // Number of followers
  followings_count: number;    // Number of users followed
  track_count: number;         // Total tracks uploaded
  [key: string]: any;          // Additional API fields
}

API Endpoint

GET https://api.soundcloud.com/resolve?url=https://soundcloud.com/{username}

Request Headers

accept
string
required
application/json
Authorization
string
required
Bearer {access_token}

Example Usage

import { getUserInfo } from '@/server/services/soundcloud/users';

// By username
const user = await getUserInfo('jowymusic');
if (user) {
  console.log(`${user.username} (ID: ${user.id})`);
  console.log(`Followers: ${user.followers_count}`);
  console.log(`Tracks: ${user.track_count}`);
}

// By user ID
const userById = await getUserInfo(12345678);

Error Handling

Returns null 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
async function getUserTracks(
  user: string | number,
  limit = 10,
  offset = 0
): Promise<SoundCloudTrack[] | null>

Parameters

user
string | number
required
SoundCloud username (string) or numeric user ID
limit
number
default:10
Maximum number of tracks to return (pagination)
offset
number
default:0
Number of tracks to skip (pagination)

Returns

tracks
SoundCloudTrack[] | null
Array of track objects, or null if user not found or error occurs

Response Type: SoundCloudTrack

interface SoundCloudTrack {
  kind: "track";               // Resource type identifier
  id: number;                  // Unique track ID
  urn: string;                 // Uniform Resource Name
  created_at: string;          // ISO 8601 timestamp
  duration: number;            // Track duration in milliseconds
  title: string;               // Track title
  description: string | null;  // Track description
  genre: string;               // Genre classification
  tag_list: string;            // Space-separated tags
  permalink_url: string;       // Public track URL
  artwork_url: string | null;  // Cover art image URL
  stream_url: string;          // Streaming endpoint
  waveform_url: string;        // Waveform visualization URL
  user: SoundCloudUser;        // Track creator information
  playback_count: number;      // Total plays
  download_count: number;      // Total downloads
  favoritings_count: number;   // Total favorites/likes
  comment_count: number;       // Total comments
  streamable: boolean;         // Streaming availability
  downloadable: boolean;       // Download availability
  [key: string]: any;          // Additional API fields
}

API Endpoint

GET https://api.soundcloud.com/users/{user_id}/tracks?limit={limit}&offset={offset}

Request Headers

accept
string
required
application/json
Authorization
string
required
Bearer {access_token}

Example Usage

import { getUserTracks } from '@/server/services/soundcloud/tracks';

// Get first 10 tracks
const tracks = await getUserTracks('jowymusic', 10, 0);

if (tracks) {
  tracks.forEach(track => {
    console.log(`${track.title} by ${track.user.username}`);
    console.log(`Plays: ${track.playback_count}`);
    console.log(`Duration: ${track.duration}ms`);
    console.log(`URL: ${track.permalink_url}`);
  });
}

// Pagination: Get next 10 tracks
const moreTracks = await getUserTracks('jowymusic', 10, 10);

// Get tracks by user ID
const tracksByID = await getUserTracks(12345678, 20);

Implementation Details

  1. User Resolution: If username provided, calls getUserInfo() to resolve to user ID
  2. Cache Check: Verifies cache using key pattern user_tracks_{userId}_limit{limit}_offset{offset}
  3. Token Retrieval: Obtains valid access token via getSoundCloudToken()
  4. API Request: Fetches tracks from SoundCloud API
  5. Cache Storage: Stores results for 1 hour

Error Handling

Returns null on errors:
  • User not found (invalid username/ID)
  • Authentication failures
  • Network errors
  • Invalid pagination parameters
const tracks = await getUserTracks('nonexistent_user');
if (!tracks) {
  console.error('Failed to retrieve tracks or user not found');
}

Caching Strategy

Token Caching

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

User Info Caching

cache_key
string
"user_{username}"
duration
number
3600 seconds (1 hour)

User Tracks Caching

cache_key
string
"user_tracks_{userId}_limit{limit}_offset{offset}"
duration
number
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_CLIENT_ID
string
required
SoundCloud application client ID from SoundCloud Developer Portal
SOUNDCLOUD_CLIENT_SECRET
string
required
SoundCloud application client secret
SOUNDCLOUD_AUTHORIZATION
string
required
Custom authorization header value for OAuth2 flow
SOUNDCLOUD_URL_GET_TOKEN
string
required
SoundCloud OAuth2 token endpoint URL

Error Messages

ErrorCauseSolution
”Fallo en la autenticación de SoundCloud”Token request failedVerify client credentials and authorization header
”Se requiere un nombre de usuario”Empty username parameterProvide valid username or user ID
”No se pudo encontrar al usuario User doesn’t existVerify username spelling and existence
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/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

Build docs developers (and LLMs) love