Skip to content

External services

See External Services for what an external service is. External services are usually configured in the Workbench, but the TypeScript client also exposes a programmatic API on eventStore.externalServices.

Start with the TypeScript client setup. For programmatic registration, choose the HTTP recipe for an HTTP service or the PostgreSQL excerpt for a database service. These are separate recipes, not a sequential script.

The client uses the name as the service identifier. register rejects with the kernel’s validation or exception messages when the kernel does not accept the definition.

Register an HTTP service with bearer token authentication

Section titled “Register an HTTP service with bearer token authentication”

The token comes from your own configuration; this example reads it from an environment variable.

import { ChronicleClient, ChronicleOptions } from '@cratis/chronicle';
const token = process.env.CUSTOMERS_API_TOKEN!;
const client = new ChronicleClient(ChronicleOptions.development());
const eventStore = await client.getEventStore('MyStore');
await eventStore.externalServices.register('CustomersApi', builder => builder
.http('https://api.example.com')
.withBearerToken(token)
.withHeader('X-Tenant', 'acme'));
client.dispose();

For the PostgreSQL alternative, reuse the eventStore setup above while its client is still alive, before client.dispose(). Supply password from your configuration.

await eventStore.externalServices.register('CustomersDb', builder => builder
.postgreSql('db.example.com', 'customers', 'postgres', password, 5432));

eventStore.externalServices exposes:

  • register(name, configure)

The builder passed to configure exposes:

  • http(url)
  • withBasicAuth(username, password)
  • withBearerToken(token)
  • withOAuth(authority, clientId, clientSecret)
  • withHeader(key, value)
  • msSql(host, database, username, password, port?)
  • postgreSql(host, database, username, password, port?)
  • withOption(key, value)