How It Works
defineHook() creates a typed hook that can be awaited in a workflow. When the tool is called, it creates a hook instance using the tool call ID as the token.The user submits their decision through an API endpoint, which resumes the hook with the approval data.
Creating an Approval Tool
Add a tool that allows the agent to deliberately pause execution until a human approves or rejects an action:Define the Hook
Create a typed hook with a Zod schema for validation:workflows/hooks/booking-approval.ts
Implement the Tool
Create a tool that creates a hook instance using the tool call ID as the token:workflows/chat/steps/tools.ts
The
defineHook().create() function must be called from within a workflow context, not from within a step. This is why executeBookingApproval does not have "use step" - it runs in the workflow context where hooks are available.Create the API Route
Create an API endpoint that the UI will call to submit the approval decision:app/api/hooks/approval/route.ts
Create the Approval Component
Build a component that reacts to the tool call data:components/booking-approval.tsx
Using Webhooks Directly
For simpler cases where you don’t need type-safe validation, usecreateWebhook() directly:
workflows/chat/steps/tools.ts
- External systems that need to call back into your workflow
- Payment provider callbacks
- Email-based approval links
- Slack or Teams integrations
Advanced Patterns
Timeout with Default Action
Combine hooks withsleep() to implement timeouts:
lineNumbers
Multi-Step Approval
Implement multi-level approvals:lineNumbers
Email Approval Links
Send approval links via email using webhooks:lineNumbers
Related Documentation
- Hooks & Webhooks - Complete guide to hooks and webhooks
createWebhook()API Reference - Webhook configuration optionsdefineHook()API Reference - Type-safe hook definitions- Chat Session Modeling - Multi-turn workflow patterns