Skip to content

Define the plug-in

// src/plugin.ts
import { definePlugin, defineEndpoint } from "initiative-plugin-sdk/manifest";

export const openTickets = defineEndpoint({
  direction: "read",
  label: { en: "Open tickets" },
  params: { project: { type: "string", label: { en: "Project" } } },
  returns: {
    titles: { type: "string", list: true },
    total: "int",
  },
  handler: async ({ params, installation, client }) => {
    const tickets = await lookUp(installation, params.project);
    return { result: { titles: tickets.map((ticket) => ticket.title), total: tickets.length } };
  },
});

export default definePlugin({
  publicId: "acme.tracker",
  uid: "K7M2QX8N4TVB9C", // npx initiative-plugin uid, once
  name: "Acme Tracker",
  scopes: ["projects:read", "comments:write"],
  endpoints: { "open-tickets": openTickets },
  schedules: {
    sweep: { every: "15m", run: async ({ since, installation, client }) => { /* … */ } },
  },
  widgets: {
    "open-count": {
      meta: { name: { en: "Open tickets" } },
      endpoints: ["open-tickets"],
      module: "src/widgets/open-count.ts",
      sample_data: { "open-tickets": { total: 12 } },
    },
  },
});
  • Types come from the definition. An endpoint's params type what its handler is handed, and its returns type what the handler answers with: a return of the wrong type, or one the endpoint does not declare, does not compile. A widget or a summary naming an endpoint that is not a declared read does not compile either.
  • Endpoints are named by their key. The manifest id is plugin.<publicId>.<key>, and everywhere the definition refers to an endpoint (a widget, a sample, a parameter's options_from, a bundled dashboard, a connection's summary, an action) it uses the key.
  • Everything else is the contract's own shape, from manifest.contract.json: vendor, connections (keyed by id), webhooks, pages (keyed by id), dashboards, fields (keyed by metadata key), parts and actions (keyed by id), settings (keyed by setting key) and help. The types are generated from the contract, so every term the contract declares has a type here.
  • Features follow from what is filled in. A plug-in with widgets declares widgets, and so on.

What a handler is handed

params What the caller sent, by the declared names. Initiative sends strings; an automation may send numbers and booleans as they are.
installation The community, by the reference this plug-in knows it by. It stays the same across uninstall and reinstall, until the community erases the plug-in's data, so it is what your own rows key on. Member references hold the same way.
community_status Where the community stands: active, read_only, suspended, on_hold or deleted. Anything but active means hold back work done on its behalf.
actor { kind: "installation" }, or { kind: "member", member } when another plug-in called as one of the community's members.
caller The plug-in that made the call through Initiative, or null for Initiative's own.
initiative The initiative the call is confined to, or null.
connections Connection id → the handle Initiative gives a vendor token for.
viewer On a call one of the plug-in's actions made: the reader who ran it, by this installation's reference for them. Otherwise null.
subject On a call one of the plug-in's actions made: the item it was run on, { type, id }. Otherwise null.
client Initiative, already acting as actor and narrowed to initiative.
context What you gave createPlugin (Serve).

A handler answers { result }, and actor too when the call ran on another credential than the call's own (a read that always uses the community's installation, say). To refuse, throw new EndpointError(status, code, detail); anything else thrown is answered 500 and logged.

A write is reachable through another plug-in, as one of the actors it declares, and only when it is public; or through one of the plug-in's own actions, on an item kind the action is offered on, as the installation. The SDK refuses anything else before the handler runs.