Published on

How to get client cridential token from Spotify using Node.Js ?

Authors

How to get client cridential token from Spotify using Node.Js ?

Spotify has a web API that we can use in our applications. In this article, I will show you how to use the Spotify rest API and make a request to some endpoints. You can check the API reference here.

In this article, I will write a demo using node.js, but you could use any other framework because the logic is simple. We will get the token from Spotify API first. And then we will store that token to access other endpoints that Spotify provides. The reason I choose node because it is simple to install and setup. And I will use this article as a reference for next project that i will create using next.js.

In addition to Node, Express will be used for routing and dotenv for environment variable management.

Basic setup for node.js

Take the following steps to create the basic structure for node.js api:

1 - Create the node.js project by typing the following command

npm init

2 - install express.js

npm install express

3 - Create a index.js file and Copy and paste the following hello world example to it:

const express = require('express')
const app = express()
const port = 3000

app.get('/list', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

We created an express.js application that has one endpoint called list and if we run this application by typing the following command:

node index.js

We should see the following output:

![[Screen Shot 2022-06-21 at 22.03.22.png]]

And if we go to the list endpoint we should see the Hello World output on the screen like this:

![[Screen Shot 2022-06-21 at 22.10.07.png]]

Now we created the basic setup for node application. Let's install dotenv package and create a .env file. .env file will be where we store client id and client secret values.

  • install the dotenv
    yarn add dotenv
    
  • Create a .env file and type your client ID and client secret values that you get from the Spotify dashboard.
SPOTIFY_CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxx
SPOTIFY_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxx

To get access token from Spotify we will send the request to the api/token endpoint with client_id and client_secret values.

app.post('/token', async (req, res) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With')

  var client_id = process.env.SPOTIFY_CLIENT_ID
  var client_secret = process.env.SPOTIFY_CLIENT_SECRET

  var authOptions = {
    url: 'https://accounts.spotify.com/api/token',
    headers: {
      Authorization: 'Basic ' + new Buffer(client_id + ':' + client_secret).toString('base64'),
    },

    form: {
      grant_type: 'client_credentials',
    },

    json: true,
  }

  request.post(authOptions, function (error, response, body) {
    if (!error && response.statusCode === 200) {
      res.json({ token: body.access_token })
    }
  })
})

If we test this endpoint we should see a token in the response.

Example response:

{
  "token": "BQDcG6MJ8DddSLyH5f-XvWjM9BB6pmnUEehZI3a7q3-jyGqfMXgwUQMG85-KcZAS2jm0ebfyDBs6BiJ78Hui_qUPhEd_NVQfnUsVeDXebzIbtnFslRQ"
}

And we can use that token to access other endpoints.

References: