Plugin SDK reference
The complete contract between ByteContent and a plugin. Types are exported from @bytecontent/contracts today and will move to the dedicated @bytecontent/plugin-sdk package.
Manifest
Every plugin folder contains a plugin.json:
| Key | Type | Rules |
|---|---|---|
id | string | Required. Lower case letters, digits and dashes, starting with a letter (2-63 characters). Unique on the server. |
name | string | Required. Display name, up to 120 characters. |
version | string | Required. MAJOR.MINOR.PATCH. |
description | string | Optional, up to 2000 characters. Shown in the Admin Studio. |
capabilities | string[] | Required, at least one. See below. |
entities | string[] | Optional. Entity keys the plugin applies to; omit for all entities. |
main | string | Optional, default index.js. A CommonJS file (.js or .cjs) inside the plugin folder. |
Capabilities
| Capability | Allows |
|---|---|
records:modify | Registering before-hooks that change record data before it is saved |
records:observe | Registering after-hooks that see records after they are saved or deleted |
Registering a hook without the matching capability throws while the plugin loads, and the plugin is not loaded.
Module
The module exports a register(api) function, called once when the API starts:
interface PluginApi {
on(event: 'record.beforeCreate' | 'record.beforeUpdate', handler: BeforeHookHandler): void;
on(event: 'record.afterCreate' | 'record.afterUpdate' | 'record.afterDelete', handler: AfterHookHandler): void;
/** Writes to the API log, prefixed with the plugin id. */
log(message: string): void;
}
interface PluginModule {
register(api: PluginApi): void;
}Hooks
| Hook | Runs | Capability |
|---|---|---|
record.beforeCreate | Before a record is created, inside the transaction | records:modify |
record.beforeUpdate | Before a record is updated, inside the transaction | records:modify |
record.afterCreate | After the create is committed | records:observe |
record.afterUpdate | After the update is committed | records:observe |
record.afterDelete | After the (soft) delete is committed | records:observe |
Hooks only run for workspaces where the plugin is enabled, and only for the entities listed in the manifest. When several plugins are enabled they run one after another (in plugin folder order); each before-hook sees the changes of the previous ones.
Before-hook payload
interface BeforeHookPayload {
tenantId: string;
entity: string; // entity key, e.g. "article"
fields: readonly string[]; // field keys of the entity
data: Readonly<Record<string, unknown>>; // record data after the requested changes
previous: Readonly<Record<string, unknown>> | null; // stored data (updates only)
}
type BeforeHookHandler = (payload: BeforeHookPayload) =>
Record<string, unknown> | void | Promise<Record<string, unknown> | void>;Return an object with the fields to change. The result is merged into the record and validated against the entity's metadata exactly like user input, so a plugin cannot store invalid values or bypass field rules. Payload objects are frozen copies.
After-hook payload
interface AfterHookPayload {
tenantId: string;
entity: string;
recordId: string;
data: Readonly<Record<string, unknown>>;
}
type AfterHookHandler = (payload: AfterHookPayload) => void | Promise<void>;Errors and limits
| Situation | Result |
|---|---|
| A before-hook throws | The change is rejected: 422 PLUGIN_REJECTED with the error message |
| A before-hook exceeds the time limit | The change is rejected: 503 PLUGIN_TIMEOUT |
| A returned value is invalid for the field | The usual 422 validation error |
| An after-hook throws or times out | A warning is logged; the committed change stays |
The time limit is PLUGIN_HOOK_TIMEOUT_MS (default 2000 ms, maximum 60000 ms).
Server settings
| Variable | Default | Purpose |
|---|---|---|
PLUGINS_DIR | plugins/ in the project | Folder scanned for plugins at startup |
PLUGIN_HOOK_TIMEOUT_MS | 2000 | Time limit per hook call |
Security model
- Only server administrators can install plugins, by placing them in the plugins folder. Workspaces can enable or disable installed plugins, nothing more.
- Plugins run in the API process. Install only plugins you trust - review the code, or use plugins reviewed for the marketplace.
- Enabling and disabling a plugin is recorded in the audit log.
Ready to write one? Follow Build a plugin.