---
title: "Notify principals in their inbox"
description: "Turn EventWolf events into notifications in a principal's inbox, read the inbox as that principal with a user token, and mark a notification read."
url: "https://agentpost.tools/docs/eventwolf/guides/notifications"
kind: guide
needs:
  - what: "The `api` helper"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "A `dev` secret key, `key`, for a project that adopted EventWolf"
    from: "https://agentpost.tools/docs/platform/guides/keys.md"
---

# 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](/docs/eventwolf/concepts/notifications.md) explains topics, recipients, preferences, and the inbox.

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

## Before you start

-   The `api` helper, from [Platform quickstart](/docs/platform/quickstart.md).
-   A `dev` secret key, `key`, for a project that adopted EventWolf, from [Create a project and issue a key](/docs/platform/guides/keys.md).

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.

```ts
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:

```ts
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:

```ts
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](/docs/platform/concepts/user-tokens.md). 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:

```ts
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:

```ts
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:

```ts
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](/docs/eventwolf/reference/inbox.md#mark-seen) clears it when the inbox opens. [Stream your notifications](/docs/eventwolf/reference/inbox.md#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](/docs/eventwolf/guides/push.md). [Push](/docs/eventwolf/concepts/push.md) explains push addresses, lock-screen text, and each phone’s badge, which keeps its own seen position.
