SoundCloud Rust Client
A Rust client for interacting with the SoundCloud API. This library provides an async interface for searching, retrieving, and downloading content from SoundCloud. It automatically discovers a valid client_id from SoundCloud, so you don't need to provide one.
Add the crate to your project:
Quickstart
use ;
async
Advanced: Using ClientBuilder for Custom Retry Configuration
use ;
async
More Examples
Search, get, and download a track
use ;
async
Search, fetch, and download a playlist
use ;
async
Get user information, followers, tracks, and playlists
use ;
async
Identifier
The library uses an Identifier enum to handle different types of SoundCloud resource identifiers:
use Identifier;
// Use numeric ID
let track_id = Id;
// Use URN (useful for some API endpoints)
let track_urn = Urn;
This provides better type safety and flexibility when working with SoundCloud resources.
API Overview
Core Client Methods
Creating a Client
Client::new() -> Result<Self, Error>: Initialize the client with default retry configuration by discovering aclient_id.Client::with_retry_config(retry_config: RetryConfig) -> Result<Self, Error>: Initialize the client with custom retry configuration.
ClientBuilder (Recommended for Custom Configuration)
ClientBuilder::new() -> Self: Create a new builder with default retry configuration.with_max_retries(max_retries: u32) -> Self: Set the maximum number of retry attempts (default: 1).with_retry_on_401(retry_on_401: bool) -> Self: Enable or disable retrying on 401 Unauthorized responses (default: true).build() -> Result<Client, Error>: Build the client with the configured settings.
Client Management
refresh_client_id(&self) -> Result<(), Error>: Refresh the client ID by re-discovering it from SoundCloud. Useful if you encounter 401 errors.get_client_id_value(&self) -> String: Get the current client ID value.health_check(&self) -> bool: Health check endpoint that calls/meon the API. Returnstrueif the API responds successfully (2xx),falseotherwise.
Low-Level API Methods
get<Q: Serialize, R: DeserializeOwned>(&self, path: &str, query: Option<&Q>) -> Result<R, Error>: Perform a GET request against the SoundCloud API.get_json<R: DeserializeOwned, Q: Serialize>(base_url: &str, path: Option<&str>, query: Option<&Q>, client_id: &str) -> Result<(R, u16), Error>: Static helper to GET JSON from any base URL. Returns both the response body and HTTP status code.
Search
get_search_results(query: Option<&SearchResultsQuery>) -> Result<SearchResultsResponse, Error>search_all(query: Option<&SearchAllQuery>) -> Result<SearchAllResponse, Error>
Tracks
search_tracks(query: Option<&TracksQuery>) -> Result<Tracks, Error>get_track(identifier: &Identifier) -> Result<Track, Error>get_track_related(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Tracks, Error>download_track(identifier: &Identifier, stream_type: Option<&StreamType>, destination: Option<&str>, filename: Option<&str>) -> Result<(), Error>get_stream_url(identifier: &Identifier, stream_type: Option<&StreamType>) -> Result<String, Error>get_track_waveform(identifier: &Identifier) -> Result<Waveform, Error>
Playlists
search_playlists(query: Option<&PlaylistsQuery>) -> Result<Playlists, Error>get_playlist(identifier: &Identifier) -> Result<Playlist, Error>get_playlist_reposters(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Users, Error>download_playlist(identifier: &Identifier, destination: Option<&str>, playlist_name: Option<&str>) -> Result<(), Error>
Albums
search_albums(query: Option<&AlbumQuery>) -> Result<Playlists, Error>
Users
search_users(query: Option<&UsersQuery>) -> Result<Users, Error>get_user(identifier: &Identifier) -> Result<User, Error>get_user_followers(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Users, Error>get_user_followings(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Users, Error>get_user_playlists(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Playlists, Error>get_user_tracks(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Tracks, Error>get_user_reposts(identifier: &Identifier, pagination: Option<&Paging>) -> Result<Reposts, Error>
Retry Configuration
The client supports automatic retry on 401 Unauthorized errors, which can occur when SoundCloud rotates their client IDs. By default, the client will retry once with a refreshed client ID. You can customize this behavior:
use ClientBuilder;
let client = new
.with_max_retries // Retry up to 3 times
.with_retry_on_401 // Enable retry on 401 (default: true)
.build
.await?;
RetryConfig defaults:
max_retries: 1retry_on_401: true
When a 401 error occurs, the client will automatically refresh the client ID and retry the request up to max_retries times.
Notes on Downloads and FFmpeg
- HLS downloads use
ffmpeg-sidecar. On first HLS download, the crate will automatically download an FFmpeg binary for your platform. No manual installation is required. - Progressive downloads are saved directly without FFmpeg.
- If your environment blocks downloads or requires proxies, the automatic FFmpeg download may fail; in that case, configure your network accordingly before using HLS.
Error Handling
The library uses a custom Error type that implements std::error::Error + Send + Sync for async compatibility. All API methods return Result<T, Error>.
use ;
match client.search_tracks.await
License
MIT