Call Initiative¶
A handler's client already acts for its call. Outside a call, make one:
import { Initiative, loadPrivateKey } from "initiative-plugin-sdk/client";
const initiative = new Initiative({
baseUrl: "https://initiative.example.com/api/v1",
publicId: "acme.tracker",
key: loadPrivateKey(pem, "plugin-1"),
});
for (const { installation, active } of await initiative.installations()) {
if (!active) continue; // paused: keep what you hold for it
const client = initiative.asInstallation(installation);
const { items } = await client.api.listProjects({ query: { initiative_id: 12 } });
}
An Initiative made by hand reads client.entitlement() only when it is given
something to check statements with: entitlements: new EntitlementStatements({
listingUid: plugin.uid }), which reads billing's key from
INITIATIVE_ENTITLEMENT_ISSUER_KEY_PEM. The server's own client has one.
asInstallation(installation, { initiative, scopes })acts as the community, optionally narrowed to one initiative the plug-in is placed in and fewer scopes.asMember(installation, member, { purpose, initiative })acts for one member, within what they consented to (client.requestConsent(…));ConsentRequiredErrorsays they have not.client.apihas a typed method for every route Initiative's plug-in API describes, named after the route's operation id in camel case. A call takes its arguments by where they go, and answers the route's typed response:
const task = await client.api.updateTask({ path: { task_id: 7 }, body: { title: "Ship it" } });
await client.api.archiveEntity({ path: { entity_type: "file", entity_id: 3 } });
No call is sent unless the token holds the scope it needs: the route's own,
the one its argument picks (archiveEntity on a file needs
files:write), the scope of the tool a tool plug-in's kind names, or, where
Initiative checks each item, at least one of them, a tool plug-in's scope
among them. MissingScopeError names
the scope instead. Writing implies reading.
A tool plug-in's tool (Declaring a tool) is served by
one set of routes, /plugin-tools/{kind}, for every tool plug-in. A kind is
a table of the tool: the tool's key, ., and its plug-in's uid in lower
case, so Counters' groups are counter_group.tsjvtcm0s7gv2x and the counters
in them counter.tsjvtcm0s7gv2x. The tool's rows and its items' share each
route:
| Method | Route | Does | Needs |
|---|---|---|---|
readPluginTool |
GET /plugin-tools/{kind} |
The tool as it declares it, by its kind or an item's: its names, the fields of its rows and of each item with their kinds and options, which item sits under which, and its actions with the fields they ask for. | <tool kind>:read |
listPluginToolRows |
GET /plugin-tools/{kind}/ |
A page of the tool's rows in an initiative (initiative_id), or of an item's under the row it sits in (parent_id). |
<tool kind>:read |
createPluginToolRow |
POST /plugin-tools/{kind}/ |
A new row there, its fields by key and its people by reference. A field left out starts at its default. |
<tool kind>:write |
readPluginToolRow |
GET /plugin-tools/{kind}/{row_id} |
One row. | <tool kind>:read |
updatePluginToolRow |
PATCH /plugin-tools/{kind}/{row_id} |
Changes what is sent. | <tool kind>:write |
duplicatePluginToolRow |
POST /plugin-tools/{kind}/{row_id}/duplicate |
A copy of a row with the items under it. | <tool kind>:write |
stepPluginToolRow |
POST /plugin-tools/{kind}/{row_id}/step |
Moves one counter field of a row, named by its key: by by, signed, or by the field's own step, up or down, held within its bounds. Two steps landing together each count. Answers the row. |
<tool kind>:write |
runPluginToolAction |
POST /plugin-tools/{kind}/{row_id}/actions/{action_id} |
Runs one of the tool's actions, with its params as fields and people. |
<tool kind>:write |
The scope is the tool's, for its items too: counter_group.tsjvtcm0s7gv2x:read
lists counters. Each value is held to its field's declaration, as a
person's edit is. Trash, archive, comments, tags, links and sharing are the
routes every tool shares. A change signal on /plugin-tools/{kind}/{row_id}/ws
is a person's, for Initiative's own pages: it takes a person's session, not
a plug-in's token, and is not part of the plug-in API.
const kind = "counter.tsjvtcm0s7gv2x";
const { items } = await client.api.listPluginToolRows({ path: { kind }, query: { parent_id: 9 } });
await client.api.updatePluginToolRow({ path: { kind, row_id: items[0].id }, body: { fields: { count: 3 } } });
await client.api.stepPluginToolRow({ path: { kind, row_id: items[0].id }, body: { field: "count", down: true } });
A tool plug-in's rows send webhook events like any tool's, named by kind:
<kind>.created, <kind>.updated and <kind>.deleted, for the tool's
rows and each item's (counter.tsjvtcm0s7gv2x.updated). A subscription
(createSubscription) needs the tool's read scope for each,
<tool kind>:read, for its items' events too. Archiving or unarchiving a
row arrives as updated, with archived_at in its changed; moving one to
the trash arrives as deleted, and restoring it as created. An event
names what changed, never a value: read the row back for its fields.
client.api is generated from the plug-in API contract this SDK publishes
(The plug-in API contract), so it changes as
the contract does: a minor release adds routes and fields, and only a major
release removes or changes one.
PluginApiSchemas["TaskRead"] names a schema's type.
- client.request(method, path, { scope, body }) calls a community route (the
path after /c/{community}) by hand, with the same scope check.
- client.callPlugin(publicId, endpointId, params) calls another plug-in's public
endpoint through Initiative. It needs plugins:<publicId> among the plug-in's
scopes, granted by the community. A plug-in that uses whichever plug-ins a
community lets it sets callsPlugins: true (the manifest's
service.calls_plugins) instead of naming each: a community that installs
another plug-in is asked there whether this one may use it, which grants the
scope.
- client.metadata, the plug-in's metadata, always on the installation's
token: get(itemKind, ids) answers each item's values by id;
set(itemKind, id, values) writes some of one item's values, a null
removing its key, and answers them as they now stand; find(key, value)
answers the items holding value under key, as { type, id }.
metadata.install.get() and metadata.install.set(values) do the same for
the install's own values.
- The installation itself, on any installation token: client.config(),
client.connections(), client.connectionToken(ref) for a usable vendor
token, client.reportConfigStatus({ state, detail }), and
client.emitEvent({ eventType, payload, initiativeId }).
- What the community holds of the plug-in: client.entitlement() reads the
statement the entitlement hook delivers, verified the same way for the
client's installation, or null when there is none.
The plug-in authenticates with its key (private_key_jwt, RFC 7523), in an
assertion addressed to Initiative's public token endpoint (publicBaseUrl).
Tokens are opaque; the client caches each until shortly before it expires,
shares one request between concurrent callers, and sends a call answered 401
once more on a fresh token. Every request gives up after 10 seconds
(timeoutMs).
A refusal stays a refusal however often it is tried; Initiative not answering may pass:
| Error | When | |
|---|---|---|
InitiativeAuthError |
the token endpoint refused (invalid_grant, invalid_target, invalid_client, …) |
The plug-in was uninstalled, or removed from that initiative: stop, don't retry. ConsentRequiredError is the member's consent. |
InitiativeApiError |
the API answered with an error status, with Initiative's detail |
retryable is true for 408, 429 and 5xx. |
InitiativeUnavailableError |
the request failed or timed out, or the token endpoint answered 408, 429 or 5xx, or an error naming no OAuth error | Try again later. status is null when there was no answer. |