Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

EventWolf quickstart

At the end, your project holds the events of one support request, r_9, and you know which of them a subscription for resolved requests would match. Run the blocks in order as one script: later blocks use values from earlier ones. The EventWolf API reference has every operation.

EventWolf answers for your project at /v1/events with your project’s secret key. If you created your project in the Platform quickstart, use the Platform API key token from that quickstart to adopt EventWolf by running await api('POST', `/v1/projects/${project.id}/services`, token, { service: 'eventwolf' }). Keep using the same project key for the calls below.

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. Register an event type

Register request.assigned with a JSON Schema for its data. From now on, an event of this type whose data doesn’t match is rejected. refPaths makes the assignee’s ID a reference, so you can find the event by the principal:

const { eventType } = await api(
  'PUT',
  '/v1/events/event-types/request.assigned',
  key,
  {
    description: 'A support request was assigned to someone.',
    schema: {
      type: 'object',
      required: ['assignee'],
      properties: {
        assignee: {
          type: 'object',
          required: ['id'],
          properties: { id: { type: 'string' } },
        },
      },
    },
    refPaths: { 'assignee.id': { type: 'user' } },
  },
);
console.log(eventType.origin, eventType.schema.version); // registered 1

A project’s starting config accepts unregistered types and lists them as observed. Event types explains schemas and versions.

2. Publish events

Publish what happened to request r_9 as CloudEvents: who created it, who it was assigned to, and how it was resolved. Each event’s source and id identify it. subject and refs say what it is about, and actor says who did it. One request takes up to 100 events as a batch:

const source = '/support/backend';
const events = [
  {
    type: 'request.created',
    actor: 'user:u_ben',
    data: { title: 'Refund for order 1042' },
  },
  {
    type: 'request.assigned',
    actor: 'user:u_ben',
    data: { assignee: { id: 'u_ana' } },
  },
  {
    type: 'request.resolved',
    actor: 'user:u_ana',
    data: { resolution: 'refunded' },
  },
].map((event) => ({
  specversion: '1.0',
  id: crypto.randomUUID(),
  source,
  subject: 'r_9',
  refs: 'request:r_9',
  ...event,
}));
const batch = { 'content-type': 'application/cloudevents-batch+json' };

const published = await api('POST', '/v1/events/events', key, events, batch);
console.log(published.results.map((result) => result.result));
// [ 'accepted', 'accepted', 'accepted' ]

Each accepted event gets a sequence, its place in your project’s order.

3. Publish the same events again

A retry after a network error sends the same events again. EventWolf knows them by their source and id, so it answers duplicate without storing them again. Here the project key can publish the stored events, so each result includes the first sequence. Duplicates explains when the sequence can be absent.

const again = await api('POST', '/v1/events/events', key, events, batch);
console.log(
  again.results.map((result) => result.result),
  again.results[1].sequence === published.results[1].sequence,
);
// [ 'duplicate', 'duplicate', 'duplicate' ] true

4. See a rejected event

An event whose data breaks its type’s schema is rejected and recorded as an ingestion failure, so a broken deploy shows up right away. Here assignee is a string, not an object:

const broken = {
  specversion: '1.0',
  id: crypto.randomUUID(),
  source,
  type: 'request.assigned',
  subject: 'r_9',
  refs: 'request:r_9',
  data: { assignee: 'u_ana' },
};
const [rejected] = (
  await api('POST', '/v1/events/events', key, [broken], batch)
).results;
console.log(rejected.result, rejected.code); // rejected invalid_data

const { failures } = await api(
  'GET',
  '/v1/events/ingest-failures?type=request.assigned',
  key,
);
console.log(failures[0].claimed.id === broken.id, failures[0].issues);
// true [ 'data.assignee must be object' ]

Fix the producer and publish the event again with the same id. The EventWolf errors explain what to do for each rejection.

5. Read the request’s history

Read the events you published about request:r_9, oldest first:

const history = await api(
  'GET',
  '/v1/events/events?ref=request:r_9&order=asc',
  key,
);
console.log(history.events.map((record) => record.event.type));
// [ 'request.created', 'request.assigned', 'request.resolved' ]

The history holds each accepted event once. History queries and the live tail use the same filter for types, sources, references, extensions, and values in data.

6. Check what a subscription would match

A subscription sends the events that match its filter to your webhook, or to principals’ inboxes and phones. Before you create one, test its filter against events you have:

const filter = { types: ['request.resolved'] };
const [created, , resolved] = history.events;

const match = await api('POST', '/v1/events/subscriptions/test', key, {
  filter,
  sequence: resolved.sequence,
});
const miss = await api('POST', '/v1/events/subscriptions/test', key, {
  filter,
  sequence: created.sequence,
});
console.log(match.matched, miss.matched, miss.steps.at(-1).detail);
// true false type "request.created" is not one of ["request.resolved"]

Each step says which part of the filter matched or didn’t.

Check it worked

Find the events about Ana. The type’s refPaths made the assignee a reference, so the assignment is among them:

const ana = await api('GET', '/v1/events/events?ref=user:u_ana', key);
console.log(ana.events.map((record) => record.event.type));
// [ 'request.resolved', 'request.assigned' ]

Ana is the resolved event’s actor, so it is about Ana too. Next, Receive webhooks creates the subscription and delivers resolved requests to your server; to notify principals instead, follow Notify principals in their inbox, and to bring in history from another system, Import event history.