Docs/SDK · @ewyn/client/Type-safe templates

Type-safe templates

Stop pasting version UUIDs. A generated template config lets you send by name — with compile-time checks on template names, versions, and required variables.

v1 · beta

Generate the config (CLI)

The SDK ships a CLI that downloads your workspace's template config and writes a TypeScript file. First, drop an ewyn.config.ts in your project root:

typescript
import type { EwynFetchConfig } from '@ewyn/client';

const config: EwynFetchConfig = {
  apiKey: process.env.EWYN_API_KEY!,
  configurationPath: './src/ewynTemplates.ts', // optional, defaults to ./ewynTemplates.ts
};

export default config;

Then run:

bash
npx @ewyn/client fetch-config
Regenerate after template changes.

Re-run fetch-config after creating or publishing templates. The config includes all non-deprecated versions per template, so regenerating never forces you onto the latest version — pin an older one with version.

Send by name

typescript
import { Ewyn } from '@ewyn/client';
import { ewynTemplates } from './ewynTemplates';

const client = new Ewyn({
  apiKey: process.env.EWYN_API_KEY!,
  templates: ewynTemplates,
});

// Type-safe: template names autocomplete, missing required
// variables are compile-time errors. Defaults to the latest version.
await client.send({
  to: 'user@example.com',
  subject: 'Welcome!',
  template: 'welcome',
  variables: { firstName: 'John', lastName: 'Doe' },
});

// Pin a specific major version (keep sending v1 after v2 ships)
await client.send({
  to: 'user@example.com',
  subject: 'Welcome!',
  template: 'welcome',
  version: 1,
  variables: { firstName: 'John', lastName: 'Doe' },
});

Other ways to get the config

  • Dashboard — API Keys page → Template Configuration → Copy JSON, then paste it into your code with as const
  • API — fetch it programmatically from GET /api/v1/templates/config
typescript
const config = {
  welcome: {
    name: 'Welcome Email',
    latest: 1,
    versions: {
      '1': {
        id: 'version-uuid-1',
        vars: {
          firstName: { required: true },
          lastName: { required: true },
          plan: { required: false },
        },
      },
    },
  },
} as const; // ← the 'as const' is what makes it type-safe

const client = new Ewyn({ apiKey: process.env.EWYN_API_KEY!, templates: config });
Don't forget `as const`.

Without it TypeScript widens the config to plain strings and you lose autocomplete and required-variable checking. The generated ewynTemplates.ts already includes it.

Was this page helpful?