Android – Create a New Playlist in Your Spotify Account utilizing Spotify Net API

Spotify is among the world’s hottest music streaming platforms, and it has an unlimited assortment of songs, albums, playlists, and podcasts. With the Spotify Net API, builders can create superb purposes that work together with the Spotify service. Utilizing the Net API’s endpoints, one can get detailed metadata about artists, albums, and tracks instantly from the Spotify Knowledge Catalogue. It additionally offers entry to the at the moment logged-in person’s information, such because the playlists and tracks saved within the Your Music Library of the person.
On this article, we’ll create an Android app utilizing which we are able to create a brand new playlist within the logged-in person’s Spotify Account. Fundamentals of Java and Android App Growth data are stipulations for this text. We will even use the Volley library to make the networking calls to the Spotify Net API and the GSON library to parse the returned JSON information.
Step By Step Implementation
Step 1: Create a New Undertaking in Android Studio
Please seek advice from How you can Create/Begin a New Undertaking in Android Studio to discover ways to create a brand new mission. Identify the mission “My Spotify App” (or any identify of your alternative) and choose Java because the programming language. A brand new mission will probably be created with a Principal Exercise and its XML file as follows:

Step 2: Create an app within the Spotify Dashboard
- Head over to the Spotify Dashboard and log in utilizing your Spotify account.
- Click on on “Create an app.”
- Write the app identify (My Spotify App), give it an outline, settle for the T&C, and click on on create button.
As soon as the app is created, the Dashboard will seem like this:

Step 3: Finalize mission settings within the dashboard
We are going to add a Redirect URI, our package deal identify, and the SHA-1 key of our Android Studio. The Redirect URI is used to redirect again to your utility from an online browser (on this case, the net web page for authentication). Due to this fact we create a novel URI for our app.
The package deal identify of an Android app is a novel identifier that distinguishes it from different apps on the gadget and the Google Play retailer. Equally, the SHA-1 secret is used to uniquely establish your Android Studio put in in your PC. Confer with this text to discover ways to get the SHA-1 key in Android Studio.
These particulars are wanted to limit our API key to solely work with our app and stop unauthorized use by others. Comply with the next steps so as to add these particulars:
- Within the mission dashboard, click on on “Edit Settings.”
- Beneath “Redirect URIs”, add your package deal identify as a redirect URI: <package-name>://callback, which on this case is:
com.instance.myspotifyapp://callback
- Additionally, add your package deal identify beneath the “Android Packages” part, alongside along with your Android Studio’s distinctive SHA-1 key.
Click on on Save, and you’ll be finished with the mission settings within the Spotify Dashboard. Your app will at the moment be in growth mode and can solely help a most of 25 customers. To make your app accessible to greater than 25 customers, you have to to Submit a quota extension request to the Spotify workforce.
Step 4: Add the mandatory dependencies and permissions
We might want to authenticate the person to make calls to the Spotify Net API. We are going to use the Spotify Android auth library, which is chargeable for authenticating the person and fetching the authorization code/entry token that may subsequently be used to play music or in requests to the API.
We are going to use the Volley library to make HTTP calls to the API and the GSON library to parse the outcomes and use them in our app. We’ll examine the Maven Central repository to get the newest model of the libraries. The hyperlinks for every library are talked about under. Select the newest model and Gradle from the respective dropdowns, copy the dependency and, paste it into your module-level gradle file, then sync your mission.
The Maven Central repository will look as follows:

Since Spotify Auth library model 2.0.0, we should additionally present the scheme and host of the redirect URI our app makes use of for authorizing in our module-level construct.gradle file. On this case, we have to paste the next in our defaultConfig scope inside android:
manifestPlaceholders = [redirectSchemeName: "com.example.myspotifyapp", redirectHostName: "callback"]
Sync your mission. The Gradle file will seem like this:

We should additionally add web permission in our app’s manifest file. Paste the next snippet in your AndroidManifest.xml file above the applying tag:
<uses-permission android:identify="android.permission.INTERNET" />
Step 5: Add the mandatory string values within the strings.xml file
Including the strings like keys and many others. that are utilized in many locations throughout the android app within the strings.xml file, is taken into account a greatest apply. We’ll retailer our Shopper ID, Redirect URI, and the shared preferences key within the string.xml file. You will get your Shopper ID and redirect URI from the Spotify Developer Dashboard. We’ll use “Spotify” as our key to entry shared preferences. The code for the strings.xml file will look as follows:
XML
|
Step 6: Add the code for authentication
Create a brand new Exercise, identify it LoginActivity in Android Studio, and choose it because the launcher exercise. We are going to write the code for authentication on this exercise and launch the MainActivity as soon as authentication is profitable. As soon as authentication is finished, we obtain a token, which is later used for authorization in making requests to the API. We’ll retailer the token in android’s persistent storage Shared Preferences for later use. We additionally have to outline the scope to entry the particular info we’ll be accessing from our person’s account. For instance, the user-read-email scope is used to learn the linked e-mail handle of the person. You may learn extra about scopes right here. The code for LoginActivity.java is as proven under:
Java
|
The XML file of LoginActivity, MainActivity, and its Java file would be the identical because the default for now. Now you can run the app and examine if auth is operating efficiently. You must see a toast saying “Auth profitable”, and the MainActivity ought to pop up proper after.
Step 7: Create helper lessons to get logged-in person’s particulars
Now that the authentication is finished, we’ll attempt to get some person particulars and examine them within the MainActivity. We have to get the person particulars in order that we are able to pay money for the person’s Spotify ID, which will probably be wanted to create a playlist in that person’s account and way more stuff. You may learn extra concerning the endpoint for getting person particulars right here. We are going to first have to create a mannequin Person class to serialize the response from the endpoint. We are going to create variables for identify, e-mail, and person ID as a result of that’s the solely info we at the moment want.
Java
|
We are going to now create a UserInfo class to fetch the response from the API’s endpoint. We are going to make the decision to the API utilizing the Volley library and parse it into the Person class utilizing GSON.
Java
|
Step 8: Show the main points within the Principal Exercise
Add a TextView on which we’ll show the person’s particulars. The XML code for the Principal Exercise class is given under:
XML
|
Now we’ll write the code within the MainActivity.java file to show the person’s information.
Java
|
Now you can run the app. It is possible for you to to see your identify and e-mail on the Principal Exercise. A pattern screenshot is hooked up under:

Step 9: Add code to create a brand new playlist in your Spotify account
We are going to now programmatically create a playlist in our Spotify account. The small print of the API for creating a brand new playlist may be discovered right here. First, we’ll create a button within the Principal Exercise for creating a brand new playlist. The XML code is as follows:
XML
|
Now we’ll make the request to the endpoint on the button click on in our Principal Exercise. The Java code is as follows:
Java
|
Now you can run the app, and after clicking the button, you’ll discover a playlist has been created in your Spotify account. A pattern app and Spotify screenshots have been hooked up under.

