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.

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();

This excerpt uses the eventStore from the example above, before the client is disposed; password comes from your configuration.

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

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.

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)