This repository is no longer maintained by the SoundCloud team due to capacity constraints. We're instead focusing our efforts on improving the API & the developer platform. Please note, at the time of updating this, the repo is already not in sync with the latest API changes.
We recommend the community to fork this repo in order to maintain the SDK. We'd be more than happy to make a reference on our developer that the developers can use different SDKs build by the community. In case you need to reach out to us, please head over to https://github.com/soundcloud/api/issues
The official SoundCloud API wrapper. It provides simple methods to handle authorization and to execute HTTP calls.
gem install soundcloudThe following examples are for the latest gem version.
SoundCloud::VERSION# => "0.3.4"# register a client with YOUR_CLIENT_ID as client_id_client=SoundCloud.new(:client_id=>YOUR_CLIENT_ID)# get newest trackstracks=client.get('/tracks',:limit=>10)# print each linktracks.eachdo |track|
putstrack.permalink_urlend# register a new client, which will exchange the username, password for an access_token# NOTE: the SoundCloud API Docs advise not to use the user credentials flow in a web app.# In any case, never store the password of a user.client=SoundCloud.new({:client_id=>YOUR_CLIENT_ID,:client_secret=>YOUR_CLIENT_SECRET,:username=>'some@email.com',:password=>'userpass'})# print logged in usernameputsclient.get('/me').usernameclient=SoundCloud.new({:client_id=>YOUR_CLIENT_ID,:client_secret=>YOUR_CLIENT_SECRET,:redirect_uri=>YOUR_REDIRECT_URI,})redirectclient.authorize_url()# the user should be redirected to "https://soundcloud.com/connect?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REDIRECT_URI"# after granting access he will be redirected back to YOUR_REDIRECT_URI# in your respective handler you can build an exchange token from the transmitted codeclient.exchange_token(:code=>params[:code])# register a new client which will exchange an existing refresh_token for an access_tokenclient=SoundCloud.new({:client_id=>YOUR_CLIENT_ID,:client_secret=>YOUR_CLIENT_SECRET,:refresh_token=>SOME_REFRESH_TOKEN})# upload a new track with audio.mp3 as audio and image.jpg as artworktrack=client.post('/tracks',:track=>{:title=>'a new track',:asset_data=>File.new('audio.mp3')})# print new tracks linkputstrack.permalink_url# register the clientclient=SoundCloud.new(:client_id=>YOUR_CLIENT_ID)# call the resolve endpoint with a track urltrack=client.get('/resolve',:url=>"http://soundcloud.com/forss/flickermood")# print the track idputstrack.id# initializing a client with an access tokenclient=SoundCloud.new(:access_token=>SOME_ACCESS_TOKEN)# updating the users profile descriptionclient.put("/me",:user=>{:description=>"a new description"})client=SoundCloud.new(:access_token=>"A_VALID_TOKEN")# get my last playlistplaylist=client.get("/me/playlists").first# get ids of contained trackstrack_ids=playlist.tracks.map(&:id)# => [22448500, 21928809]# adding a new track 21778201track_ids << 21778201# => [22448500, 21928809, 21778201]# map array of ids to array of track objects:tracks=track_ids.map{|id| {:id=>id}}# => [{:id=>22448500}, {:id=>21928809}, {:id=>21778201}]# send update/put request to playlistplaylist=client.put(playlist.uri,:playlist=>{:tracks=>tracks})# print the list of track ids of the updated playlist:pplaylist.tracks.map(&:id)Stores the passed options and call exchange_token in case options are passed that allow an exchange of tokens.
Stores the passed options and try to exchange tokens if no access_token is present and:
refresh_token,client_idandclient_secretis present.client_id,client_secret,username, andpasswordis presentclient_id,client_secret,redirect_uri, andcodeis present
Stores the passed options except for state and display and return an
authorize url. The client_id and redirect_uri options need to present to
generate the authorize url. The state and display options can be used to
set the parameters accordingly in the authorize url.
These methods expose all available HTTP methods. They all share the signature
(path_or_uri, query={}, options={}). The query hash will be merged with the
options hash and passed to httparty. Depending on if the client is authorized
it will either add the client_id or the access_token as a query parameter. In
case an access_token is expired and a refresh_token, client_id and
client_secret is present it will try to refresh the access_token and retry
the call. The response is either a Hashie::Mash or an array of Hashie::Mashes.
The mashes expose all resource attributes as methods and the original response
through HashResponseWrapper#response.
These methods are accessors for the stored options.
A Proc passed to on_exchange_token will be called each time a token was successfully exchanged or refreshed
Returns a date based on the expires_in attribute returned from a token
exchange.
Will return true or false depending on if expires_at is in the past.
In case a request was not successful a SoundCloud::ResponseError will be
raised. The original HTTParty response is available through
SoundCloud::ResponseError#response.
For more code examples, please visit the SoundCloud API Documentation.
