Skip to content

Instantly share code, notes, and snippets.

@ekafyi
Last active July 31, 2020 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekafyi/36066424a79e155787d048b51cd3144b to your computer and use it in GitHub Desktop.
Save ekafyi/36066424a79e155787d048b51cd3144b to your computer and use it in GitHub Desktop.
my attempt to adapt glitch.com/~spotify-web-api-node into Netlify functions
// truncated version of my attempt to adapt https://glitch.com/~spotify-web-api-node into Netlify functions
const SpotifyWebApi = require("spotify-web-api-node");
const TEST_ARTIST_ID = "1lYT0A0LV5DUfxr6doRP3d";
let accessToken;
const spotifyApi = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
});
exports.handler = async (event) => {
if (!accessToken) {
const result = await spotifyApi.clientCredentialsGrant().then((data) => data);
accessToken = await result.body["access_token"];
}
const spotifyAuthApi = new SpotifyWebApi({ accessToken });
try {
data = await spotifyAuthApi.getArtist(TEST_ARTIST_ID);
return {
statusCode: 200,
body: JSON.stringify(data.body),
};
} catch (err) {
return {
statusCode: 500, // temporary error code
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment