Date Handling in API Clients#
JSON does not natively distinguish between plain strings and date strings, so Hey API's generated clients require explicit configuration to serialize and deserialize Date objects. Three mechanisms are available, each targeting a different point in the pipeline:
| Mechanism | When it runs | Purpose |
|---|---|---|
@hey-api/transformers | Runtime (response) | Convert ISO date strings → Date objects |
| Validator plugins (Zod / Valibot) | Runtime (request + response) | Validate and parse date formats |
Parser patch | Code-generation time | Rewrite date fields before any plugin sees them |
Response Deserialization — Transformers Plugin#
The @hey-api/transformers plugin is the primary mechanism for turning date strings in API responses into native Date objects .
Enable it in your openapi-ts config:
// openapi-ts.config.ts
{
plugins: [
{
dates: true,
name: '@hey-api/transformers',
},
],
}
With dates: true, the generator:
- Changes TypeScript types from
stringtoDatefor all date/date-time fields. - Emits a
transformers.gen.tsfile with per-operation transformer functions that callnew Date(value)at runtime .
Limitation: Only native JavaScript
Dateobjects are supported. Third-party date libraries (e.g.,dayjs,luxon) are not .
Wiring Transformers into the SDK#
Transformers are opt-in in the SDK layer. Set transformer: true on @hey-api/sdk to automatically apply responseTransformer to every generated SDK function :
{
plugins: [
'@hey-api/transformers',
{ name: '@hey-api/sdk', transformer: true },
],
}
Without this, generated transformers exist in the output but are not called automatically — you must invoke them manually.
Request Validation with Zod / Valibot#
Validator plugins handle request bodies and responses. They do not transform string → Date the way the transformers plugin does; instead they validate that the value matches the expected ISO format .
Zod exposes a dates object to tune z.iso.datetime() behavior :
dates.offset: true— allows values with timezone offsets.dates.local: true— allows values without a timezone.
Enable a validator and wire it into the SDK:
{
plugins: [
{ name: '@hey-api/sdk', validator: true },
{ name: 'zod', dates: { offset: true } },
],
}
You can also restrict validation to requests only, leaving response validation off — useful when you trust server output but not client input .
Pre-Generation Schema Patching#
When a spec contains incorrect or unwanted date representations, fix them before code generation using parser.patch.schemas. Patches run before any parsing, so all downstream plugins see the corrected schema .
Example — convert a date-time string field to a Unix timestamp (number):
// openapi-ts.config.ts
{
parser: {
patch: {
schemas: {
Foo: (schema) => {
delete schema.properties.updatedAt.format;
schema.properties.updatedAt.type = 'number';
},
},
},
},
}
Other common uses: add a missing format: 'date-time' annotation, or remove an erroneous date field entirely (delete schema.properties.internalDate).
Further Reading#
- Transformers plugin reference — full API,
bigIntoption, and generated code example - SDK plugin — Transformers section —
transformerandvalidatorwiring options - Parser configuration —
patch,transforms, andfilters - Zod plugin —
dates.offset/dates.localconfiguration - Validators overview — Valibot, Zod, and other supported validators