Get started

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.

How plugins run

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:

text
plugins/
  reading-time/
    plugin.json
    index.js

3. Write the manifest

json
{
  "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

js
'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 data after the requested changes, the entity's field keys, and (for updates) the previous data.
  • It returns only the fields it wants to change. Returning nothing changes nothing.
  • Throwing an error rejects the change; the API answers 422 PLUGIN_REJECTED with your message.

5. Load and enable it

  1. Restart the API. The log shows Loaded plugin [email protected], or the reason it failed to load.
  2. In the Admin Studio, open Templates & plugins and enable Reading time for the workspace. The change is recorded in the audit log.
  3. Create or edit an article: readingTime is 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.

js
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 with 503 PLUGIN_TIMEOUT.
  • Check fields exist with fields.includes(...); workspaces can change their schema.
  • Ask for the fewest capabilities you need, and limit entities where 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.

ByteContent by ByteRings is open source under the MIT License. Edit on GitHub