|
| 1 | +# Temporal External Client Configuration Samples |
| 2 | + |
| 3 | +This directory contains TypeScript samples that demonstrate how to use the Temporal SDK's external client configuration feature. This feature allows you to configure a `Client` using a TOML file and/or programmatic overrides, decoupling connection settings from your application code. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +To run, first see [README.md](../README.md) for prerequisites. |
| 8 | + |
| 9 | +## Configuration File |
| 10 | + |
| 11 | +The [`config.toml`](./config.toml) file defines three profiles for different environments: |
| 12 | + |
| 13 | +- `[profile.default]`: A working configuration for local development. |
| 14 | +- `[profile.staging]`: A configuration with an intentionally **incorrect** address (`localhost:9999`) to demonstrate how it can be corrected by an override. |
| 15 | +- `[profile.prod]`: A non-runnable, illustrative-only configuration showing a realistic setup for Temporal Cloud with placeholder credentials. This profile is not used by the samples but serves as a reference. |
| 16 | + |
| 17 | +## Samples |
| 18 | + |
| 19 | +The following TypeScript scripts demonstrate different ways to load and use these configuration profiles. |
| 20 | + |
| 21 | +### [`load-from-file.ts`](./src/load-from-file.ts) |
| 22 | + |
| 23 | +This sample shows the most common use case: loading the `default` profile from the `config.toml` file. |
| 24 | + |
| 25 | +### [`load-profile.ts`](./src/load-profile.ts) |
| 26 | + |
| 27 | +This sample demonstrates loading the `staging` profile by name (which has an incorrect address) and then correcting the address programmatically. This highlights the recommended approach for overriding configuration values at runtime. |
| 28 | + |
| 29 | +## Running this sample |
| 30 | + |
| 31 | +1. `temporal server start-dev` to start [Temporal Server](https://github.com/temporalio/cli/#installation). |
| 32 | +2. `npm install` to install dependencies. |
| 33 | +3. Run any of the samples: |
| 34 | + - `npm run load-default` - Load and connect using the default profile |
| 35 | + - `npm run load-profile` - Load staging profile and override the incorrect address |
| 36 | + |
| 37 | +### Expected Output |
| 38 | + |
| 39 | +**Running `npm run load-default`:** |
| 40 | + |
| 41 | +``` |
| 42 | +--- Loading default profile from config.toml --- |
| 43 | +Loaded 'default' profile from /path/to/config.toml. |
| 44 | + Address: localhost:7233 |
| 45 | + Namespace: default |
| 46 | + gRPC Metadata: {"my-custom-header":"development-value","trace-id":"dev-trace-123"} |
| 47 | +
|
| 48 | +Attempting to connect to client... |
| 49 | +✅ Client connected successfully! |
| 50 | +``` |
| 51 | + |
| 52 | +**Running `npm run load-profile`:** |
| 53 | + |
| 54 | +``` |
| 55 | +--- Loading 'staging' profile with programmatic overrides --- |
| 56 | +The 'staging' profile in config.toml has an incorrect address (localhost:9999). |
| 57 | +We'll programmatically override it to the correct address. |
| 58 | +
|
| 59 | +Loaded 'staging' profile from /path/to/config.toml with overrides. |
| 60 | + Address: localhost:7233 (overridden from localhost:9999) |
| 61 | + Namespace: staging |
| 62 | +
|
| 63 | +Attempting to connect to client... |
| 64 | +✅ Client connected successfully! |
| 65 | +``` |
0 commit comments