Get Spotify song preview URLs along with song details. This package helps you find preview URLs for Spotify songs, which can be useful when the official preview URLs are not available.
npm install spotify-preview-finder dotenv
There are two ways to use this package:
First, install the required dependencies:
npm install dotenv
Create a .env
file in your project root:
SPOTIFY_CLIENT_ID=your_client_id
SPOTIFY_CLIENT_SECRET=your_client_secret
Then in your code:
// IMPORTANT: This must be the first line in your main file
require('dotenv').config();
// The package automatically uses the environment variables for authentication
const spotifyPreviewFinder = require('spotify-preview-finder');
async function example() {
try {
// Get preview URLs for a song (limit is optional, default is 5)
const result = await spotifyPreviewFinder('Shape of You', 3);
if (result.success) {
result.results.forEach(song => {
console.log(`\nSong: ${song.name}`);
console.log(`Spotify URL: ${song.spotifyUrl}`);
console.log('Preview URLs:');
song.previewUrls.forEach(url => console.log(`- ${url}`));
});
} else {
console.error('Error:', result.error);
}
} catch (error) {
console.error('Error:', error.message);
}
}
example();
The package handles authentication automatically in this order:
- When you call
require('dotenv').config()
, it loads your credentials from the.env
file intoprocess.env
- When you call the function, it:
- Creates a Spotify API client using your credentials
- Gets an access token automatically (valid for 1 hour)
- Uses this token for the search request
- The token is refreshed automatically when needed
You can also set up authentication directly in your code (not recommended for production):
const spotifyPreviewFinder = require('spotify-preview-finder');
// Set environment variables programmatically
process.env.SPOTIFY_CLIENT_ID = 'your_client_id';
process.env.SPOTIFY_CLIENT_SECRET = 'your_client_secret';
async function searchSongs() {
try {
// Search for multiple songs
const songs = [
await spotifyPreviewFinder('Bohemian Rhapsody', 1),
await spotifyPreviewFinder('Hotel California', 1),
await spotifyPreviewFinder('Imagine', 1)
];
songs.forEach(result => {
if (result.success && result.results.length > 0) {
const song = result.results[0];
console.log(`\nFound: ${song.name}`);
console.log(`Preview URL: ${song.previewUrls[0]}`);
}
});
} catch (error) {
console.error('Error:', error.message);
}
}
searchSongs();
{
success: true,
results: [
{
name: "Shape of You - Ed Sheeran",
spotifyUrl: "https://open.spotify.com/track/7qiZfU4dY1lWllzX7mPBI3",
previewUrls: [
"https://p.scdn.co/mp3-preview/7339548839a263fd721d01eb3364a848cad16fa7"
]
},
// ... more results
]
}
songName
(string) - The name of the song to search forlimit
(number, optional) - Maximum number of results to return (default: 5)
Promise that resolves to an object with:
success
(boolean) - Whether the request was successfulresults
(array) - Array of song objects containing:name
(string) - Song name with artist(s)spotifyUrl
(string) - Spotify URL for the songpreviewUrls
(array) - Array of preview URLs
error
(string) - Error message if success is false
- Go to Spotify Developer Dashboard
- Log in with your Spotify account
- Click "Create an App"
- Fill in the app name and description
- Once created, you'll see your Client ID and Client Secret
- Copy these credentials to your
.env
file:SPOTIFY_CLIENT_ID=your_client_id_here SPOTIFY_CLIENT_SECRET=your_client_secret_here
- "Authentication failed" error: Make sure your .env file is in the root directory and credentials are correct
- "Cannot find module 'dotenv'": Run
npm install dotenv
- No environment variables found: Make sure
require('dotenv').config()
is at the top of your main file
MIT
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.