Skip to content

Writing Plugins

This guide covers how to write custom Plenum plugins — from the basic contract to TypeScript setup and bundling.

Working examples:

A plugin module exports two functions:

Called once at gateway startup. Receives the options object from the upstream config. Use it for setup (database connections, configuration validation, etc.). Return value is ignored.

exports.init = function init(options) {
// options comes from x-plenum-upstream.options
return {};
};

Called for each incoming request. Must return a response object.

exports.handle = function handle(input) {
return {
status: 200,
headers: { "content-type": "application/json" },
body: { message: "Hello", path: input.request.path },
};
};

The handle() function receives:

FieldTypeDescription
request.methodstringHTTP method
request.pathstringFull request path
request.routestringMatched OpenAPI path template (e.g. /users/{id})
request.paramsRecord<string, unknown>Path parameters, coerced to the declared schema type
request.querystringRaw query string (for backward compatibility)
request.queryParamsRecord<string, unknown>Parsed query parameters, typed per the OpenAPI spec
request.headersRecord<string, string>Request headers
bodyunknownParsed request body (for POST/PUT/PATCH)
configunknownValue from x-plenum-backend with ${{...}} tokens resolved
operationobjectOpenAPI operation metadata (parameters, responses, schemas)
ctxRecord<string, unknown>User context from interceptors

Return a response object:

{
status: 200, // HTTP status code
headers: { "content-type": "application/json" }, // Response headers
body: { ... }, // JSON object, string, or null
}