Overview
resumeHook() and resumeWebhook() allow you to resume paused workflows by sending data from external contexts like API routes or server actions. These functions deliver payloads to hooks created with createHook() or createWebhook().
resumeHook
Resume a workflow by sending data to a hook identified by its token.
Signature
function resumeHook<T = any>(
tokenOrHook: string | Hook,
payload: T,
encryptionKeyOverride?: CryptoKey
): Promise<Hook>
Parameters
The unique token identifying the hook, or the hook object itself
The data payload to send to the hook. Must be serializable.
Optional encryption key override for the payload
Returns
Promise resolving to the hook object containing runId, token, and metadata
Example
import { resumeHook } from 'workflow/api';
// In an API route
export async function POST(request: Request) {
const { token, data } = await request.json();
try {
const hook = await resumeHook(token, data);
return Response.json({
success: true,
runId: hook.runId
});
} catch (error) {
return new Response('Hook not found', { status: 404 });
}
}
resumeWebhook
Resume a workflow via a webhook by sending an HTTP request. This function processes the incoming request and sends the response.
Signature
function resumeWebhook(
token: string,
request: Request
): Promise<Response>
Parameters
The unique token identifying the webhook
The incoming HTTP request object
Returns
Promise resolving to the HTTP response from the workflow’s respondWith() call
Example
import { resumeWebhook } from 'workflow/api';
// In an API route that handles webhook callbacks
export async function POST(
request: Request,
{ params }: { params: { token: string } }
) {
return await resumeWebhook(params.token, request);
}
Common Use Cases
Manual Approval Flow
// Workflow with hook
export async function approvalWorkflow(orderId: string) {
'use workflow';
const hook = createHook<{ approved: boolean }>({
token: `approval-${orderId}`
});
const decision = await hook;
if (decision.approved) {
// Process order
}
}
// API route to approve
import { resumeHook } from 'workflow/api';
export async function POST(request: Request) {
const { orderId, approved } = await request.json();
await resumeHook(`approval-${orderId}`, { approved });
return Response.json({ success: true });
}
Webhook Integration
// Workflow with webhook
export async function paymentWorkflow(checkoutId: string) {
'use workflow';
const webhook = createWebhook({
token: `payment-${checkoutId}`
});
const paymentData = await webhook.then(async (req) => {
const body = await req.json();
await req.respondWith(new Response('OK', { status: 200 }));
return body;
});
// Process payment confirmation
}
// API route to handle webhook
import { resumeWebhook } from 'workflow/api';
export async function POST(
request: Request,
{ params }: { params: { checkoutId: string } }
) {
return await resumeWebhook(`payment-${params.checkoutId}`, request);
}