Spotify Activity Ex

Examples

You can use these examples in your project.

Nodejs

const SpotifyActivity = require('spotify-activity');
 
const spotify = new SpotifyActivity('CLIENT_ID', 'CLIENT_SECRET', 'REFRESH_TOKEN');
 
spotify.fetchCurrentActivity()
    .then(activity => {
        console.log(`Now playing: ${activity.item.name} - ${activity.item.artists[0].name}`);
        console.log(`Album: ${activity.item.album.name}`);
        console.log(`Album Image: ${activity.item.album.images[0].url}`);
    })
    .catch(err => console.error(err));

React

import React, { useEffect, useState } from 'react';
import SpotifyActivity from 'spotify-activity';
 
const SpotifyComponent = () => {
    const [activity, setActivity] = useState(null);
 
    useEffect(() => {
        const spotify = new SpotifyActivity('CLIENT_ID', 'CLIENT_SECRET', 'REFRESH_TOKEN');
        spotify.fetchCurrentActivity().then(setActivity);
    }, []);
 
    if (!activity) return <div>Loading...</div>;
 
    return (
        <div>
            <h3>Now playing:</h3>
            <p>{activity.item.name} - {activity.item.artists[0].name}</p>
            <img src={activity.item.album.images[0].url} alt={activity.item.album.name} />
        </div>
    );
};
 
export default SpotifyComponent;

Nextjs

import { useEffect, useState } from 'react';
import SpotifyActivity from 'spotify-activity';
 
export default function Home() {
    const [activity, setActivity] = useState(null);
 
    useEffect(() => {
        const spotify = new SpotifyActivity('CLIENT_ID', 'CLIENT_SECRET', 'REFRESH_TOKEN');
        spotify.fetchCurrentActivity().then(setActivity);
    }, []);
 
    return (
        <div>
            <h1>Vibe Spotify Activity</h1>
            {activity ? (
                <div>
                    <p>{activity.item.name} - {activity.item.artists[0].name}</p>
                    <img src={activity.item.album.images[0].url} alt={activity.item.album.name} />
                </div>
            ) : (
                <p>Loading...</p>
            )}
        </div>
    );
}

Php

<?php
$clientId = 'CLIENT_ID';
$clientSecret = 'CLIENT_SECRET';
$refreshToken = 'REFRESH_TOKEN';
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player/currently-playing');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . getAccessToken($clientId, $clientSecret, $refreshToken)
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
$response = curl_exec($ch);
curl_close($ch);
 
$activity = json_decode($response, true);
 
echo "Now playing: " . $activity['item']['name'] . " - " . $activity['item']['artists'][0]['name'] . "\n";
echo "Album: " . $activity['item']['album']['name'] . "\n";
echo "Album Image: " . $activity['item']['album']['images'][0]['url'] . "\n";
 
function getAccessToken($clientId, $clientSecret, $refreshToken) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/api/token');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'grant_type' => 'refresh_token',
        'refresh_token' => $refreshToken,
        'client_id' => $clientId,
        'client_secret' => $clientSecret
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $response = curl_exec($ch);
    curl_close($ch);
 
    $data = json_decode($response, true);
    return $data['access_token'];
}
?>

Curl

ACCESS_TOKEN=$(curl -s -X POST \
    -d "grant_type=refresh_token" \
    -d "refresh_token=REFRESH_TOKEN" \
    -d "client_id=CLIENT_ID" \
    -d "client_secret=CLIENT_SECRET" \
    https://accounts.spotify.com/api/token | jq -r '.access_token')
 
curl -s -X GET \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    https://api.spotify.com/v1/me/player/currently-playing | jq

Python

import requests
 
CLIENT_ID = 'CLIENT_ID'
CLIENT_SECRET = 'CLIENT_SECRET'
REFRESH_TOKEN = 'REFRESH_TOKEN'
 
def get_access_token(client_id, client_secret, refresh_token):
    url = 'https://accounts.spotify.com/api/token'
    payload = {
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token,
        'client_id': client_id,
        'client_secret': client_secret
    }
    response = requests.post(url, data=payload)
    return response.json()['access_token']
 
access_token = get_access_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
 
headers = {
    'Authorization': f'Bearer {access_token}'
}
 
response = requests.get('https://api.spotify.com/v1/me/player/currently-playing', headers=headers)
activity = response.json()
 
print(f"Şu anda çalıyor: {activity['item']['name']} - {activity['item']['artists'][0]['name']}")
print(f"Albüm: {activity['item']['album']['name']}")
print(f"Albüm Resmi: {activity['item']['album']['images'][0]['url']}")