defineHook
Creates a type-safe hook helper that ensures the payload type is consistent between hook creation and resumption.
This is a lightweight wrapper around createHook() and resumeHook() to avoid type mismatches. It also supports optional runtime validation and transformation of payloads using any Standard Schema v1 compliant validator like Zod or Valibot.
We recommend using defineHook() over createHook() in production codebases for better type safety and optional runtime validation.
Schema validation checks the payload's shape, not who sent it. Authorize callers before calling resume(). See Hook and webhook security.
API signature
Parameters
| Name | Type | Description |
|---|---|---|
__0 | { schema?: StandardSchemaV1<TInput, TOutput>; } | Schema used to validate and transform the input payload before resuming |
Returns
defineHook() returns a TypedHook<TInput, TOutput>:
| Name | Type | Description |
|---|---|---|
create | (options?: any) => Hook<TOutput> | Creates the hook. Call inside a "use workflow" function. |
resume | (token: string, payload: TInput) => Promise<HookEntity> | Resumes the hook from runtime code. Resolves to the resumed hook; throws HookNotFoundError if the token does not match an active hook. |
create() is called inside a "use workflow" function to create the hook; resume() is called from runtime code (an API route or server action). When a schema is provided, resume() accepts the raw input type (TInput) and the workflow receives the validated and possibly transformed output type (TOutput); without a schema, TOutput defaults to TInput. resume() throws HookNotFoundError if the token does not match an active hook. It does not return null.
Examples
Basic type-safe hook definition
By defining the hook once with a specific payload type, you can reuse it in multiple workflows and API routes with automatic type safety.
Resuming with type safety
Hooks can be resumed using the same defined hook and a token. By using the same hook, you can ensure that the payload matches the defined type when resuming a hook. resume() resolves to the resumed hook and throws HookNotFoundError if the token does not match an active hook.
Validate and transform with schema
You can provide runtime validation and transformation of hook payloads using the schema option. This option accepts any validator that conforms to the Standard Schema v1 specification.
Standard Schema is a standardized specification for schema validation libraries. Most popular validation libraries support it, including Zod, Valibot, ArkType, and Effect Schema. You can also write custom validators.
Using Zod with defineHook
Here's an example using Zod to validate and transform hook payloads:
When resuming the hook from an API route, the schema validates and transforms the incoming payload before the workflow resumes:
Using other Standard Schema libraries
The same pattern works with any Standard Schema v1 compliant library. Here's an example with Valibot:
Customizing tokens
Tokens are used to identify a specific hook and for resuming a hook. You can customize the token to be more specific to a use case.
create() accepts the same options as createHook(). If a newer run should replace one that still holds the token, pass experimental_force: true to take the token over instead of getting HookConflictError.
Related functions
createHook(): Create a hook in a workflow.resumeHook(): Resume a hook with a payload.