Build a plugin
Plugins add behaviour to ByteContent: fill fields automatically, enforce rules, or notify other systems when content changes. In this guide you build a plugin that calculates the reading time of articles.
Plugins are trusted code installed on the server by an administrator and enabled per workspace. They receive record data only - no database access - and every change they return is validated like user input. See the Plugin SDK reference for the full contract.
1. Prepare the content model
Install the CMS template, then add an Integer field with the key readingTime to the article entity in the Admin Studio.
2. Create the plugin folder
Plugins live in the server's plugins directory (plugins/ in the project, or the folder set in PLUGINS_DIR). Each plugin is a folder with a manifest and a module:
plugins/
reading-time/
plugin.json
index.js3. Write the manifest
{
"id": "reading-time",
"name": "Reading time",
"version": "1.0.0",
"description": "Calculates the reading time of articles from their body.",
"capabilities": ["records:modify"],
"entities": ["article"],
"main": "index.js"
}capabilities declares what the plugin may do. A before-hook needs records:modify; registering a hook without its capability fails when the plugin loads. entities limits the plugin to articles.
4. Write the module
'use strict';
const WORDS_PER_MINUTE = 220;
/** @param {import('@bytecontent/contracts').PluginApi} api */
function register(api) {
const readingTime = ({ fields, data }) => {
if (!fields.includes('readingTime') || typeof data.body !== 'string') return;
const words = data.body.trim().split(/\s+/).filter(Boolean).length;
return { readingTime: Math.max(1, Math.round(words / WORDS_PER_MINUTE)) };
};
api.on('record.beforeCreate', readingTime);
api.on('record.beforeUpdate', readingTime);
}
module.exports = { register };- The handler receives the record
dataafter the requested changes, the entity's field keys, and (for updates) thepreviousdata. - It returns only the fields it wants to change. Returning nothing changes nothing.
- Throwing an error rejects the change; the API answers
422 PLUGIN_REJECTEDwith your message.
5. Load and enable it
- Restart the API. The log shows
Loaded plugin [email protected], or the reason it failed to load. - In the Admin Studio, open Templates & plugins and enable Reading time for the workspace. The change is recorded in the audit log.
- Create or edit an article:
readingTimeis filled in automatically.
React to changes with after-hooks
After-hooks run once a change is committed. They are suited to notifications and integrations, need the records:observe capability, and cannot change data. Failures are logged and never undo the change.
api.on('record.afterUpdate', async ({ entity, recordId, data }) => {
if (data.status === 'published') {
await fetch(process.env.REBUILD_HOOK_URL, { method: 'POST' });
api.log(`${entity} ${recordId} published, rebuild triggered`);
}
});Good practice
- Be fast. Hooks have a time limit (
PLUGIN_HOOK_TIMEOUT_MS, 2 seconds by default). A slow before-hook fails the request with503 PLUGIN_TIMEOUT. - Check fields exist with
fields.includes(...); workspaces can change their schema. - Ask for the fewest capabilities you need, and limit
entitieswhere you can. - Keep secrets in environment variables, never in the plugin folder.
Share it
Publish the plugin on GitHub or npm, or submit it to the ByteContent marketplace as a free or paid plugin.