Skip to content

Hooks

hooks: {
  after_connect: async ({ connection, actor, access_token, params, client }) => {
    const login = await vendorLogin(access_token);
    return { values: { login }, account_label: `@${login}` }; // or { refuse: true, message: { en: "…" } }
  },
  revoke: async ({ connection, access_token, refresh_token, values }) => { /* end the grant */ },
  webhook: async ({ connection, headers, body, client }) => {
    await client.emitEvent({ eventType: "plugin.acme.tracker.ticket-opened", payload: { number: 7 } });
  },
},
  • after_connect runs once a connection's flow has exchanged its code, when the flow sets after_connect. When the account the person signed in with reaches several (an installation per organization, a site per company), answer { choices: [{ values, account_label }, …] }: Initiative connects a lone choice at once, and otherwise lists the labels for the person to pick one, storing its values as a single answer's.
  • It also runs when an admin saves a static connection without a flow that sets after_connect: true beside its fields, before anything is stored: access_token is null and values holds what they typed, secrets included. Check them at the vendor and answer the connection's managed values and an account label, or { refuse: true } to store nothing. A refusal's message, by language, says what to fix, such as the permissions the vendor did not grant; Initiative shows it to the admin where they saved. This is where a plug-in makes the vendor's webhooks for the connection, with a secret of 32 random bytes kept in a managed secret field.
  • A flow with an install_url signs the person in first, and after_connect runs with no params.installation_id: answer the installations they control as choices, and none sends them to the install page. Back from it, after_connect runs again with the installation_id the page returned.
  • revoke runs when a connection whose flow says revoke: "hook" ends. A flow can instead have Initiative end the grant itself: rfc7009 posts to revoke_url, and github_grant sends GitHub's "Delete an app authorization" (DELETE with the client's credentials and the access token) to it.
  • revoke also runs when a connection with after_connect: true beside its fields is cleared or replaced, or a value it checked is not kept, with the values it held, so you can remove what you made at the vendor.
  • webhook runs once per community for each vendor delivery Initiative received, checked and routed; headers are the vendor's x- headers, lowercased, and body is exactly what the vendor sent. Initiative checks a delivery under webhooks.verify.secret: one vendor value, {vendor.<key>}, or a managed secret field of the routed connection, {<key>}. With a connection's secret, it routes the delivery first and checks it under the secret of each connection its route value names. A delivery whose body a community already has is not handed on again.
  • Each schedule's run is called once per community each interval, with since: when it last succeeded there, or null the first time.

A hook that throws is answered 500: a connection is then not recorded, and a delivery or a schedule is tried again.

After connecting

A static connection may have a setup to go through once it is made, and a read that says how it stands:

connections: {
  store: {
    scope: "static",
    label: { en: "Store" },
    fields: [{ key: "domain", type: "string", label: { en: "Store address" } }],
    after_connect: true,
    setup: { label: { en: "Sync the store" } },
    summary: "sync-state",
  },
},
hooks: {
  setup: async ({ connection, values, answers }) => {
    if (answers) {
      if (!(await startSync(values, answers))) return { refuse: true, message: { en: "Choose where the stock goes." } };
      return {};
    }
    if (await synced(values)) return { steps: [] };
    return {
      steps: [
        { title: { en: "What syncing does" }, text: { en: "The store's stock follows yours." } },
        {
          title: { en: "Locations" },
          questions: [
            {
              key: "location-1",
              label: { en: "Main Street" },
              options: [
                { value: "new", label: { en: "A new location: Main Street" } },
                { value: "none", label: { en: "Nothing" } },
              ],
              value: "new",
            },
          ],
        },
      ],
      submit: { en: "Sync now" },
    };
  },
},
  • setup runs, as revoke does, with the connection and the values it holds, secrets included. Without answers, answer what to ask now: steps, each a title, some text and questions, and submit, what the last step's button says; or { steps: [] } when there is nothing to set up. A question is a choice among its options, and its value is the option suggested, one of them.
  • Initiative asks it once the connection is made (saved and checked, or a flow completed), and when it answers steps, opens them as a wizard in its own dialog over the plug-in's settings. While the connection is connected, Initiative asks again when it draws the connection, and while there are steps it shows a button with setup.label on it that opens the wizard. Only people who may manage the connection see any of it.
  • Submitting sends answers, each question's key and the value of the option chosen. Act on them and answer {}, which closes the wizard, or { refuse: true, message }, which the wizard shows.
  • The SDK answers 500 in place of an answer Initiative would not take, and logs why: more than 8 steps, 64 questions in all or 50 options a question, a key asked twice or an option offered twice, a suggestion that is not one of its options, a title, label or submit over 120 characters, a step's text over 1000, a key or value over 64, or with answers, anything but {} or a refusal.
  • summary names a read by its key, held to the rules a community summary is: a read, with returns, and no required parameter. Wherever Initiative shows the connection while it is connected, it reads the endpoint as the installation and draws each return under its label beneath the connection. It reads it again when the setup is submitted.

validateManifest refuses a setup on a connection that is not static or in a declarative plug-in, which has no hook to ask, and a summary the community summary's rules refuse.

The community's own hooks

Eight hooks are about the community's install rather than a connection: the six below, and backup and restore (Backups). Each is optional; a plug-in that defines none is answered 404, and Initiative stops sending that hook.

hooks: {
  installed: async ({ installation }) => { /* a first install, or a return */ },
  uninstalled: async ({ installation }) => { /* keep what you hold: it may come back */ },
  erase: async ({ installation }) => { /* delete everything about this community */ },
  community_status: async ({ installation, status }) => { /* pause unless active */ },
  plugin_status: async ({ installation, status }) => { /* your call */ },
  entitlement: async ({ installation, version, values }) => { /* keep the newest */ },
},
  • installed runs when the community installs the plug-in, and again if it returns after an uninstall. It comes back under the same installation and the same member references, so whatever you kept for it still fits.
  • uninstalled runs when it removes the plug-in. Its references stand.
  • erase runs when the community asks for the plug-in's data to be deleted, or is itself purged. Delete everything you hold for it. Once the hook answers, Initiative forgets the references, and a later install starts afresh under new ones. A hook that throws is asked again.
  • community_status runs whenever the community's standing changes. The same value rides every call as community_status, so a missed hook is caught up on the next call.
  • plugin_status runs whenever the plug-in's own state in the community changes: active, read_only, on_hold or deleted, as the deployment's staff or its billing service set it. What to keep, pause, archive or erase in each state is the plug-in's decision, and it may ignore any of them: Initiative manages no plug-in's data. A suspended plug-in is sent nothing at all, so suspended never arrives. Unlike community_status, it does not ride every call.
  • entitlement carries billing's statement of what this community holds of the plug-in: values, whose keys are agreed between the plug-in and billing, and a version that rises with each statement. Statements can repeat or arrive out of order, so keep the highest version you have seen and ignore a lower one. A deployment with no billing never sends one. The SDK verifies each statement before your hook sees it: see Entitlement statements.