Get started

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:

KeyTypeRules
idstringRequired. Lower case letters, digits and dashes, starting with a letter (2-63 characters). Unique on the server.
namestringRequired. Display name, up to 120 characters.
versionstringRequired. MAJOR.MINOR.PATCH.
descriptionstringOptional, up to 2000 characters. Shown in the Admin Studio.
capabilitiesstring[]Required, at least one. See below.
entitiesstring[]Optional. Entity keys the plugin applies to; omit for all entities.
mainstringOptional, default index.js. A CommonJS file (.js or .cjs) inside the plugin folder.

Capabilities

CapabilityAllows
records:modifyRegistering before-hooks that change record data before it is saved
records:observeRegistering 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:

ts
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

HookRunsCapability
record.beforeCreateBefore a record is created, inside the transactionrecords:modify
record.beforeUpdateBefore a record is updated, inside the transactionrecords:modify
record.afterCreateAfter the create is committedrecords:observe
record.afterUpdateAfter the update is committedrecords:observe
record.afterDeleteAfter the (soft) delete is committedrecords: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

ts
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

ts
interface AfterHookPayload {
  tenantId: string;
  entity: string;
  recordId: string;
  data: Readonly<Record<string, unknown>>;
}

type AfterHookHandler = (payload: AfterHookPayload) => void | Promise<void>;

Errors and limits

SituationResult
A before-hook throwsThe change is rejected: 422 PLUGIN_REJECTED with the error message
A before-hook exceeds the time limitThe change is rejected: 503 PLUGIN_TIMEOUT
A returned value is invalid for the fieldThe usual 422 validation error
An after-hook throws or times outA warning is logged; the committed change stays

The time limit is PLUGIN_HOOK_TIMEOUT_MS (default 2000 ms, maximum 60000 ms).

Server settings

VariableDefaultPurpose
PLUGINS_DIRplugins/ in the projectFolder scanned for plugins at startup
PLUGIN_HOOK_TIMEOUT_MS2000Time 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.

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