Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Notify principals in their inbox

Use this guide to tell principals in your product about events that concern them, such as a request assigned to them. Your server sets up who is notified of what; each principal reads and marks their own inbox. Notifications explains topics, recipients, preferences, and the inbox.

A recipient is your own ID for a principal, such as u_ana.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Create a topic

A topic is what notifications are about, and what a principal turns on or off in their preferences. Every topic write sets defaults.inbox, defaults.push, and required. This one reaches the inbox by default and lets a principal turn either channel on or off.

const { topic } = await api('PUT', '/v1/events/topics/assignments', key, {
  name: 'Assignments',
  description: 'When a request is assigned to you',
  defaults: { inbox: true, push: false },
  required: false,
});
console.log(topic.defaults); // { inbox: true, push: false }

2. Subscribe the topic to events

A subscription with a notification target turns each matching event into a notification. fromPaths names the recipient by a path in the event’s data, and the template’s {{…}} take the event’s values:

const { subscription: notify } = await api(
  'POST',
  '/v1/events/subscriptions',
  key,
  {
    name: 'Assignments to the assignee',
    filter: { types: ['request.assigned'] },
    target: {
      kind: 'notification',
      topic: 'assignments',
      recipients: { fromPaths: ['assignee.id'] },
      template: {
        title: '{{data.summary}}',
        url: '/requests/{{subject}}',
      },
    },
  },
);
console.log(notify.target.kind); // notification

3. Publish an event

Assign request r_12 to Ana. The notification is made when the event is accepted:

await api(
  'POST',
  '/v1/events/events',
  key,
  {
    specversion: '1.0',
    id: crypto.randomUUID(),
    source: '/support/backend',
    type: 'request.assigned',
    subject: 'r_12',
    actor: 'user:u_ben',
    data: {
      summary: 'Ben assigned “Login loop on Safari” to you',
      assignee: { id: 'u_ana' },
    },
  },
  { 'content-type': 'application/cloudevents+json' },
);

4. Read the inbox as the principal

Each principal reads their own inbox with a user token. Your server mints one for a principal of your own sign-in, and its externalId is their recipient ID. Give the token to their browser:

const { token: anaToken } = await api('POST', '/v1/user-tokens', key, {
  externalId: 'u_ana',
});
const inbox = await api('GET', '/v1/events/inbox?unread=true', anaToken);
const [notification] = inbox.notifications;
console.log(notification.title, notification.url);
// Ben assigned “Login loop on Safari” to you /requests/r_12

A principal signed in through IdentityHound sends their session token instead, and is the recipient with their principal ID.

5. Mark it read

Mark the notification read when the principal opens it:

const { notification: read } = await api(
  'POST',
  `/v1/events/inbox/${notification.id}/read`,
  anaToken,
);
console.log(Boolean(read.readAt)); // true

Check it worked

Count Ana’s inbox:

const count = await api('GET', '/v1/events/inbox/count', anaToken);
console.log(count); // { unread: 0, unseen: 1 }

unseen counts what is new in the inbox: Mark seen clears it when the inbox opens. Stream your notifications sends new ones to the browser as they arrive.

To reach Ana’s phone too, follow Send a push to a phone. Push explains push addresses, lock-screen text, and each phone’s badge, which keeps its own seen position.