This guide walks you through creating plugins for youtube-music-cli.
```bash # Clone a template cp -r templates/plugin-basic my-plugin cd my-plugin
# Edit plugin.json with your plugin info # Edit index.ts with your plugin logic ```
``bash youtube-music-cli plugins install /path/to/my-plugin ``
``bash youtube-music-cli plugins enable my-plugin ``
We provide three templates to get you started:
Location: templates/plugin-basic/
A minimal plugin that demonstrates:
Location: templates/plugin-ui/
A plugin that adds UI functionality:
Location: templates/plugin-audio/
A plugin for audio stream processing:
{
"id": "my-awesome-plugin",
"name": "My Awesome Plugin",
"version": "1.0.0",
"description": "Does awesome things",
"author": "Your Name",
"main": "index.ts",
"permissions": ["player"],
"hooks": ["track-change"]
}
Important fields:
permissions: Only request what you needhooks: Declare which events you'll listen toimport type {Plugin, PluginManifest, PluginContext} from 'youtube-music-cli';
const manifest: PluginManifest = {
// ... same as plugin.json
};
const plugin: Plugin = {
manifest,
async init(context) {
// Called once when plugin is loaded
context.logger.info('Plugin loaded!');
// Register event handlers
context.on('track-change', event => {
// Do something when track changes
context.logger.info('Now playing:', event.track?.title);
});
},
async enable(context) {
// Called when plugin is enabled
context.logger.info('Plugin enabled');
},
async disable(context) {
// Called when plugin is disabled
// Clean up resources here
},
async destroy(context) {
// Called when plugin is unloaded
// Final cleanup
},
};
export default plugin;
export {manifest};
# Install locally
youtube-music-cli plugins install /path/to/my-plugin
# List plugins to verify
youtube-music-cli plugins list
# Enable it
youtube-music-cli plugins enable my-awesome-plugin
# Run the app to test
youtube-music-cli
Only request permissions you actually need. Users are more likely to trust plugins with fewer permissions.
Wrap your code in try-catch blocks to prevent crashing the main app:
context.on('track-change', async event => {
try {
await doSomething(event.track);
} catch (error) {
context.logger.error('Failed:', error);
}
});
Always unregister shortcuts and clean up resources:
async disable(context) {
context.unregisterShortcut(['ctrl+shift+m']);
// Cancel any pending operations
}
Use context.logger instead of console.log for proper log formatting and filtering.
Use context.filesystem for persistent data:
// Save data
await context.filesystem.writeFile('cache.json', JSON.stringify(data));
// Load data
const data = JSON.parse(await context.filesystem.readFile('cache.json'));
Store user-configurable settings with context.config:
// Read setting with default
const enabled = context.config.get('notifications', true);
// Save setting
context.config.set('notifications', false);
``bash youtube-music-cli plugins install https://github.com/you/my-plugin ``
plugins/ directory ``bash youtube-music-cli plugins install my-plugin ``
Set the DEBUG environment variable:
DEBUG=* youtube-music-cli
Plugin logs are prefixed with the plugin name:
[My Plugin] Plugin loaded!
[My Plugin] Now playing: Song Title
Plugin not loading:
Permission denied:
Events not firing:
async init(context) {
context.on('track-change', (event) => {
if (event.track) {
// Show system notification
showNotification(event.track.title, event.track.artists[0]?.name);
}
});
}
async init(context) {
context.registerShortcut(['ctrl+l'], () => {
const track = context.player.getCurrentTrack();
if (track) {
// Open lyrics view
context.navigation.navigate('lyrics');
}
});
}
async init(context) {
const blocklist = ['known-ad-id-1', 'known-ad-id-2'];
context.audio.onStreamRequest(async (url, track) => {
if (blocklist.includes(track.videoId)) {
context.logger.info('Blocked ad:', track.title);
return null; // Skip this track
}
return url; // Allow playback
});
}