Det9999 commited on
Commit
0dec4f8
·
verified ·
1 Parent(s): 972d774

Upload pages/api/spotify/data.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. pages/api/spotify/data.js +36 -0
pages/api/spotify/data.js ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import axios from 'axios';
2
+
3
+ export default async function handler(req, res) {
4
+ const { accessToken, type } = req.query;
5
+
6
+ if (!accessToken) {
7
+ return res.status(401).json({ error: 'Missing access token' });
8
+ }
9
+
10
+ try {
11
+ let url;
12
+ if (type === 'profile') {
13
+ url = 'https://api.spotify.com/v1/me';
14
+ } else if (type === 'tracks') {
15
+ url = 'https://api.spotify.com/v1/me/top/tracks?limit=10&time_range=medium_term';
16
+ } else if (type === 'artists') {
17
+ url = 'https://api.spotify.com/v1/me/top/artists?limit=10&time_range=medium_term';
18
+ } else {
19
+ return res.status(400).json({ error: 'Invalid type' });
20
+ }
21
+
22
+ const response = await axios.get(url, {
23
+ headers: {
24
+ Authorization: `Bearer ${accessToken}`,
25
+ },
26
+ });
27
+
28
+ res.status(200).json(response.data);
29
+ } catch (error) {
30
+ console.error('Spotify API error:', error.response?.data || error.message);
31
+ res.status(error.response?.status || 500).json({
32
+ error: 'Failed to fetch Spotify data',
33
+ details: error.response?.data
34
+ });
35
+ }
36
+ }