Skip to main content
This guide walks you through creating a custom plugin from scratch. By the end you will have a working guardrail that can be enabled in your gateway configuration and used in guardrail checks.
Before you start, fork and clone the gateway repository and confirm that npm install completes successfully.

Overview

Every plugin consists of two required files:
  • manifest.json — declares the plugin’s identity, credentials, and available functions.
  • <functionId>.ts — implements each function declared in the manifest.
An optional *.test.ts file is strongly recommended.

Step-by-step guide

1

Create the plugin directory

Create a directory under plugins/ using your plugin’s identifier as the folder name. The name must be lowercase and contain no spaces.
Your plugin directory will look like this when complete:
2

Create manifest.json

The manifest describes your plugin and every function it exposes. Create plugins/my-guardrail/manifest.json:
plugins/my-guardrail/manifest.json
The id at the top level must match the directory name and must be unique across all plugins. The id on each function entry is used as both the TypeScript file name and the key in the generated plugins/index.ts.
3

Implement the handler

Create plugins/my-guardrail/blockPhrases.ts. The file name must match the function id in the manifest.
plugins/my-guardrail/blockPhrases.ts
Key points about the handler:
  • Always initialize error, verdict, and data before the try block.
  • Use getText(context, eventType) from ../utils to extract the relevant text — it works across all request types.
  • Wrap logic in a try/catch and populate error if something goes wrong; never throw from a handler.
  • Return exactly { error, verdict, data }. Transformer plugins also return { transformedData, transformed }.
4

Register the plugin in conf.json

Open conf.json at the repository root and add your plugin ID to plugins_enabled:
conf.json
If your plugin requires credentials (API keys, secrets), add them under the credentials key:
conf.json
Credentials are injected into parameters.credentials at runtime and are never logged.
5

Build the plugins

Run the build command from the repository root:
This command reads conf.json, imports each enabled plugin’s manifest.json, and regenerates plugins/index.ts with the correct import and export statements for your new handler.
If you skip the build step, your plugin handler will not be included in the gateway’s plugin registry and will not execute.
6

Write tests

Create plugins/my-guardrail/blockPhrases.test.ts:
plugins/my-guardrail/blockPhrases.test.ts
Run the plugin tests:
7

Start the gateway

Once tests pass, start the gateway normally:
Your plugin is now active. Reference it in a guardrail configuration by its plugin ID and function ID: my-guardrail.blockPhrases.

Reference: PluginHandler type

Your handler must satisfy the PluginHandler signature from plugins/types.ts:
The optional options argument gives handlers access to environment variables (options.env) and a simple key-value cache (getFromCacheByKey / putInCacheWithValue), which is useful for caching remote API responses such as JWKS keys.

Real-world example: contains.ts

The built-in contains guardrail in plugins/default/contains.ts is a good reference for the complete handler pattern:
plugins/default/contains.ts

Transformer plugins

If your plugin needs to modify the request or response rather than just evaluating it, set "type": "transformer" in the manifest and return transformedData from the handler. Use the setCurrentContentPart utility from plugins/utils.ts to write modified text back into the payload in a way that correctly handles all request types:

Contributing your plugin

To share your plugin with the community:
  1. Follow the structure above and ensure all tests pass with npm run test:plugins.
  2. Create a GitHub issue with the title format [Feature] Your Plugin Name describing the plugin’s purpose.
  3. Open a pull request with the title [New Plugin] Your Plugin Name referencing your issue.
See the contribution guide for full details.