Skip to content

Webhooks

See Webhooks for what a webhook is. Use eventStore.webhooks to register webhook observers for event streams.

Start with the TypeScript client setup. Choose one registration recipe for a webhook: make an explicit call, or declare an imported class for discovery.

Chronicle sends events to the target URL from the kernel, so the URL must be reachable from the kernel, not only from your application. Use withBearerToken, withBasicAuth, or withOAuth rather than an unauthenticated endpoint, and keep the secrets in configuration.

import 'reflect-metadata';
import { ChronicleClient, ChronicleOptions, eventType } from '@cratis/chronicle';
import { field } from '@cratis/fundamentals';
@eventType()
class EmployeeHired {
@field(String) firstName: string;
@field(String) lastName: string;
constructor(firstName: string, lastName: string) {
this.firstName = firstName;
this.lastName = lastName;
}
}
const client = new ChronicleClient(ChronicleOptions.development());
const eventStore = await client.getEventStore('MyStore');
await eventStore.webhooks.register(
'employee-audit',
'https://example.internal/webhooks/employee-audit',
builder => {
builder
.withEventType(EmployeeHired)
.withHeader('x-source', 'chronicle')
.withBearerToken('token-value');
}
);
client.dispose();

For a target that requires OAuth scopes, replace the bearer-token call in the registration above with withOAuth(authority, clientId, clientSecret, scope?). Read the authority and credentials from your configuration:

builder.withOAuth(
process.env.OAUTH_AUTHORITY!,
process.env.OAUTH_CLIENT_ID!,
process.env.OAUTH_CLIENT_SECRET!,
'employees.read employees.write'
).withHeader('x-source', 'chronicle');

The fourth argument is a space-delimited string forwarded unchanged. Omit it to keep the existing three-argument call; an omitted or empty scope sends no scope value. The builder remains chainable.

Define discoverable webhooks with decorators

Section titled “Define discoverable webhooks with decorators”

For discovery instead of an explicit registration call, declare a class with @webhook(...):

  • @webhook(targetUrl)
  • @webhook(id, targetUrl)
  • @webhook(id, targetUrl, eventSequenceId)

Import the modules that declare the webhook classes before creating the event store. On store creation, getEventStore(...) discovers and registers the classes supplied by the client’s artifact provider with the store’s other artifacts. Looking up a cached store does not rerun that creation path.

import 'reflect-metadata';
import { IWebhook, IWebhookDefinitionBuilder, webhook } from '@cratis/chronicle';
@webhook('employee-webhook', 'https://example.internal/webhooks/employees')
class EmployeeWebhook implements IWebhook {
define(builder: IWebhookDefinitionBuilder): void {
builder
.notReplayable()
.withHeader('x-client', 'chronicle-ts');
}
}

eventStore.webhooks exposes:

  • discover()
  • registerDiscovered()
  • register(webhookId, targetUrl, configure)
  • getWebhooks()
  • remove(webhookId)