Plugin APIs
Plugin APIs allow your plugin to expose functionality that other plugins can use, creating a plugin ecosystem where plugins can interact and extend each other's capabilities.
Prerequisites
You have created a Strapi plugin and understand the Admin Panel API.
What are Plugin APIs?
Plugin APIs are methods and functionality that your plugin exposes through the apis parameter in registerPlugin. Other plugins can access these APIs to extend functionality, share data, or integrate features.
app.registerPlugin({
id: 'my-plugin',
name: 'My Plugin',
apis: {
// Expose APIs here
myApi: (data) => {
/* implementation */
},
anotherApi: () => {
/* implementation */
},
},
});
Use Case: Notification Plugin
Let's build a Notification Plugin that other plugins can use to send notifications throughout the admin panel.
1. Creating the Notification Plugin
// src/plugins/notifications/admin/src/index.ts
export default {
register(app: any) {
app.registerPlugin({
id: 'notifications',
name: 'Notifications',
apis: {
// Send a notification to the admin panel
sendNotification: (
message: string,
type: 'success' | 'error' | 'warning' | 'info'
) => {
// Show notification in admin panel
console.log(`[${type.toUpperCase()}] ${message}`);
// Could integrate with toast notifications, email, etc.
},
// Schedule a notification for later
scheduleNotification: (message: string, delay: number) => {
setTimeout(() => {
console.log(`Scheduled: ${message}`);
}, delay);
},
// Get notification history
getNotificationHistory: () => {
return [
{ message: 'User created', type: 'success', timestamp: new Date() },
{ message: 'Failed to save', type: 'error', timestamp: new Date() },
];
},
// Add custom notification types
addCustomType: (typeName: string, config: any) => {
console.log(`Added custom notification type: ${typeName}`, config);
},
},
});
},
};
2. Using the Notification Plugin
Now other plugins can use the notification functionality:
// src/plugins/user-management/admin/src/index.ts
export default {
register(app: any) {
app.registerPlugin({
id: 'user-management',
name: 'User Management',
});
},
bootstrap(app: any) {
const notifications = app.getPlugin('notifications');
if (notifications && notifications.apis) {
// Send welcome notification when plugin loads
notifications.apis.sendNotification(
'User Management plugin loaded successfully',
'success'
);
// Schedule a reminder
notifications.apis.scheduleNotification(
'Remember to review user permissions weekly',
5000 // 5 seconds delay
);
}
},
};
3. Real-world Usage in Action
// When a user is created
const createUser = async (userData: any) => {
try {
const user = await strapi.entityService.create(
'plugin::users-permissions.user',
{
data: userData,
}
);
// Notify success using the notification plugin
const notifications = strapi.plugin('notifications');
if (notifications && notifications.apis) {
notifications.apis.sendNotification(
`User "${user.username}" created successfully`,
'success'
);
}
return user;
} catch (error) {
// Notify error
const notifications = strapi.plugin('notifications');
if (notifications && notifications.apis) {
notifications.apis.sendNotification(
`Failed to create user: ${error.message}`,
'error'
);
}
throw error;
}
};