Module spotify

ballerinax/spotify Ballerina library

2.0.0

Overview

Spotify is a music and podcast streaming service that gives listeners access to millions of tracks, albums, podcasts, and audiobooks, along with personalised playlists and recommendations.

The Spotify connector provides a Ballerina client for the Spotify Web API. It lets you search the Spotify catalogue, read and manage a user's library and playlists, follow artists and users, and inspect and control playback on a user's devices from your integrations.

Key Features

  • Browse the Spotify catalogue: albums, artists, tracks, shows, episodes, audiobooks, and chapters
  • Search for any catalogue item and fetch new releases, categories, and recommendations
  • Create and update playlists, and add, reorder, or remove their items
  • Manage a user's saved tracks, albums, shows, episodes, and audiobooks, and the artists and users they follow
  • Read the user's listening history and top items, inspect playback state, and control playback on their devices

Setup guide

To use the Spotify connector, you need a Spotify account and an app registered in the Spotify Developer Dashboard.

Step 1: Create a Spotify app

  1. Sign in to the Spotify Developer Dashboard and select Create app.
  2. Enter an app name and description, add a redirect URI such as http://127.0.0.1:8080/callback, select the Web API option, accept the terms of service, and save the app.
  3. Open the app's Settings page and note the Client ID and Client secret.

Step 2: Obtain a refresh token

  1. Open the authorization URL in a browser, replacing the placeholders. Request the scopes your integration needs, for example user-read-private playlist-modify-private user-top-read user-library-read.

    https://accounts.spotify.com/authorize?client_id=<client-id>&response_type=code&redirect_uri=<redirect-uri>&scope=<space-separated-scopes>
    
  2. Approve the request. Spotify redirects to your redirect URI with a code query parameter.

  3. Exchange the code for tokens.

    Copy
    curl -X POST "https://accounts.spotify.com/api/token" \
      -H "Authorization: Basic <base64 of client-id:client-secret>" \
      -H "Content-Type: application/x-www-form-urlencoded" \
      -d "grant_type=authorization_code&code=<code>&redirect_uri=<redirect-uri>"
  4. Copy the refresh_token from the response. The connector uses it to obtain new access tokens automatically.

Note: Refresh tokens for apps created in the Spotify Developer Dashboard expire six months after the user authorizes the app. Refreshing access tokens does not extend this period. Once the refresh token expires, token requests fail with an invalid_grant error, and the user must go through the authorization flow above again to obtain a new refresh token for the connector.

Note: Endpoints that read or change user data need a token granted by that user through this flow. Alternatively, a short-lived access token can be passed with the token field of the connector's bearer-token configuration.

Quickstart

To use the Spotify connector in your Ballerina application, modify the .bal file as follows.

Step 1: Import the connector

Copy
import ballerinax/spotify;

Step 2: Create a new connector instance

Create a Config.toml file with your credentials.

Copy
clientId = "<client-id>"
clientSecret = "<client-secret>"
refreshToken = "<refresh-token>"

Then create a client from them.

Copy
configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;

spotify:Client spotify = check new ({
    auth: {clientId, clientSecret, refreshToken}
});

Step 3: Invoke the connector operation

Fetch a track from the Spotify catalogue.

Copy
public function main() returns error? {
    spotify:TrackObject _ = check spotify->getTrack("2Foc5Q5nqNiosCNqttzHof");
}

Step 4: Run the Ballerina application

Copy
bal run

Examples

The Spotify connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering the following use cases:

Build from source

Setting up prerequisites

  1. Download and install Java SE Development Kit (JDK) version 21 from either of the following sources:

    Note: After installation, remember to set the JAVA_HOME environment variable to the directory where the JDK is installed.

  2. Download and install Ballerina Swan Lake.

  3. Download and install Docker.

    Note: Ensure that the Docker daemon is running before executing any tests.

  4. Export a GitHub Personal Access Token with read package permissions as follows:

    Copy
    export packageUser=<Username>
    export packagePAT=<Personal Access Token>

Build options

Run the following commands to build from source.

  1. To build the package:

    Copy
    ./gradlew clean build
  2. To run the tests:

    Copy
    ./gradlew clean test
  3. To build without the tests:

    Copy
    ./gradlew clean build -x test
  4. To run tests against different environments:

    Copy
    ./gradlew clean test -Pgroups=<Comma separated groups/test cases>
  5. To debug the package with a remote debugger:

    Copy
    ./gradlew clean build -Pdebug=<port>
  6. To debug with the Ballerina language:

    Copy
    ./gradlew clean build -PbalJavaDebug=<port>
  7. Publish the generated artifacts to the local Ballerina Central repository:

    Copy
    ./gradlew clean build -PpublishToLocalCentral=true
  8. Publish the generated artifacts to the Ballerina Central repository:

    Copy
    ./gradlew clean build -PpublishToCentral=true

Contribute to Ballerina

As an open-source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All the contributors are encouraged to read the Ballerina Code of Conduct.

Import

import ballerinax/spotify;Copy

Other versions

See more...

Metadata

Released date: about 22 hours ago

Version: 2.0.0

License: Apache-2.0


Security

Direct vulnerabilities: No known direct vulnerabilities

Indirect vulnerabilities: 

Checked on 2026-11-23 via OSV.dev


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 0

Current verison: 1


Weekly downloads


Source repository


Keywords

Type/Connector

Vendor/Spotify

Area/Productivity & Collaboration


Contributors