SoundCloud Scraper

FREEMIUM
By DataFanatic | Updated 17 days ago | Music
Popularity

9.7 / 10

Latency

896ms

Service Level

100%

Health Check

100%

Back to All Tutorials (3)

How to Use Endpoints with `offsetToken`

Overview

Before using any endpoints that have an optional field offsetToken, please read and run the code examples below. Taking the endpoint /v1/user/tracks as an example, the code examples demonstrate how to get all tracks of a user. They may help you better understand how to work with pagination and avoid pitfalls.

If there is no code example in your programming languages, variable names and comments still help.

Node.js Code Example (with axios)

// A third-party HTTP client library.
let axios = require('axios')

// Libraries help you manipulate files.
let fs = require('node:fs')
let path = require('node:path')

// Replace this key with yours.
let apiKey = '<YOUR-API-KEY>'

let paramUser = 'https://soundcloud.com/edsheeran'
let resultName = 'tracks.json'

main()

async function main() {

  // Scrapes tracks.
  let tracks = await getUserAllTracks(paramUser, 50)

  // Saves the tracks to a JSON file.
  await fs.promises.writeFile(path.join(__dirname, resultName), JSON.stringify(tracks, null, 2))

}

/**
 * Scrapes all of a user's tracks.
 * @param {string} user User URL or ID.
 * @param {number} [limit] The max number of tracks per page.
 * @param {number} [maxPages] The max number of pages that will be scraped. Avoids unexpected infinite loop.
 */
async function getUserAllTracks(user, limit = 50, maxPages = 20) {

  // Sets query parameters for the first page.
  let params = { user: user, limit: limit }

  let tracks = []

  for (let i = 0; i < maxPages; ++i) {

    console.log(`Fetching page ${i}`)
    console.log(`Params: ${JSON.stringify(params)}`)

    let res
    try {
      res = await axios.request({
        method: 'GET',
        url: 'https://soundcloud-scraper.p.rapidapi.com/v1/user/tracks',
        params: params,
        headers: {
          'X-RapidAPI-Key': apiKey,
          'X-RapidAPI-Host': 'soundcloud-scraper.p.rapidapi.com',
        },
      })
    } catch (err) {

      // For the HTTP library `axios`, an error will be thrown
      // if the response status falls out of the range of 2xx
      // or the request was made but no response was received.
      // Learn more: https://axios-http.com/docs/handling_errors.
      console.log(err)

      break
    }

    // Note that the number of items on a page may be less than `limit`,
    // even if the page is not the last page.
    // (SoundCloud's backend seems to simply discard some of the items fetched from the database.)
    let page = res.data.tracks
    console.log(`Item count: ${page.items.length}\n`)

    // Appends items of the current page.
    tracks.push(...page.items)

    // `nextOffsetToken` is the only field that determines whether there are more items.
    if (page.nextOffsetToken === null) { // No more tracks.
      break
    }

    // Resets the query parameters for the next page.
    // Field `user` is not needed anymore.
    params = { offsetToken: page.nextOffsetToken, limit: limit }

  }

  return tracks

}