Docs incorrect or hard to follow?

Tweet me

so I can make things better! 😊

Soundcloud Wrapper

Basic Usage

Outlines how the Soundcloud Wrapper is used on a simplistic level.

Install

npm i soundcloud-wrapper

Backend Usage

// index.ts
import Soundcloud from "soundcloud-wrapper"
 
// ... existing code ...
 
// define token schema
const tokenSchema = new mongoose.Schema({
  userId: { type: mongoose.Schema.Types.ObjectId },
  access_token: { type: String },
  token_type: { type: String },
  expires_in: { type: Number },
  refresh_token: { type: String },
  scope: { type: String },
})
 
// define token model
const Token = mongoose.model("Token", tokenSchema)
 
// initialize soundcloud client
const sc = new Soundcloud(
  process.env.SOUNDCLOUD_CLIENT_ID as string,
  process.env.SOUNDCLOUD_CLIENT_SECRET as string,
  process.env.SOUNDCLOUD_REDIRECT_URI as string,
  process.env.PKCE_CODE_VERIFIER as string
)
 
// route for getting and storing auth token
app.get("/soundcloud/token", async (req: Request, res: Response) => {
  try {
    // get token from soundcloud, req.query.code should be passed from your front end after the user has authorized the app
    const tokenRequest = await sc.token.getToken(req.query.code as string)
    // store token in db
    const token = new Token({
      // userId should be securely passed along with the request - the value here is a placeholder
      userId: new mongoose.Types.ObjectId(),
      ...tokenRequest,
    })
    await token.save()
    res.status(201).json(token)
  } catch (e) {
    res.status(500).json({ message: "Error getting token" })
  }
})
 
// route querying soundcloud api
app.get("/soundcloud/me", async (req: Request, res: Response) => {
  try {
    // userId should be securely passed along with the request - the value here is a placeholder
    const userId = "67a391e61cc9342bf0b895b1"
    //get token form DB
    const token = await Token.findOne({ userId })
    // query soundcloud api
    const me = await sc.me.me(token?.access_token as string)
    // return response
    res.status(200).json(me)
  } catch (e) {
    res.status(500).json({ message: "Error getting me" })
  }
})
 
// ... existing code ...

Adding To Your App

To get started make sure you have aquired all your details outlined in the Prerequisite section.

If you already have everything you need to get started check out Logged In Auth Flow for step by step instructions on using the API.

On this page