ytmusic-api is an unofficial JavaScript/TypeScript library designed to scrape and interact with YouTube Music data. It provides a robust API for accessing information such as songs, videos, artists, albums, playlists, lyrics, and search suggestions directly from YouTube Music. The library ships with comprehensive TypeScript type definitions, aiming for 95% data type consistency despite the inherent variability of scraped data. Currently at version 5.3.1, the project demonstrates an active release cadence with frequent updates addressing bug fixes, stability improvements, and new features. Its primary differentiator is its strong TypeScript support and a refactored codebase built for understandability and testability, distinguishing it from older, less-typed alternatives.
npm install ytmusic-apiVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing the YTMusic API and performing a song search, then logging the results.
Always pin to a specific patch version and thoroughly test after updates to avoid unexpected regressions. Monitor the package's GitHub repository for critical updates and issues.
Review error handling in your application, especially around data processing from API responses. Ensure your code gracefully handles potential errors or `null`/`undefined` values for inconsistent data fields.
Implement defensive programming practices, such as optional chaining (`?.`) and nullish coalescing (`??`), when accessing properties of API response objects. Always validate critical data fields at runtime.
Ensure you are using `ytmusic-api@5.0.2` or later if your project relies on CommonJS modules. For new projects, prefer using ES Modules if possible.
Ensure `await ytmusic.initialize()` is called before making any API requests, typically within an `async` function.
Change your import statement to `import YTMusic from 'ytmusic-api'`. If you must use CommonJS, ensure your project is configured as `"type": "commonjs"` or remove the `type` field from `package.json`.
Use optional chaining (`song.artist?.name`) and provide fallback values (e.g., `song.artist?.name || 'Unknown Artist'`) to safely access potentially missing properties.