resumeHook

Resumes a workflow run by sending a payload to a hook identified by its token.

It creates a hook_received event and re-triggers the workflow to continue execution.

resumeHook is a runtime function that must be called from outside a workflow function.

import { resumeHook } from "workflow/api";

export async function POST(request: Request) {
  const { token, data } = await request.json();

  try {
    const result = await resumeHook(token, data); 
    return Response.json({
      runId: result.runId
    });
  } catch (error) {
    return new Response("Hook not found", { status: 404 });
  }
}

API Signature

Parameters

NameTypeDescription
tokenstringThe unique token identifying the hook
payloadNonNullable<T>The data payload to send to the hook

Returns

Returns a Promise<Hook> that resolves to:

NameTypeDescription
runIdstringThe unique identifier of the workflow run this hook belongs to.
hookIdstringThe unique identifier of this hook within the workflow run.
tokenstringThe secret token used to reference this hook.
ownerIdstringThe owner ID (team or user) that owns this hook.
projectIdstringThe project ID this hook belongs to.
environmentstringThe environment (e.g., "production", "preview", "development") where this hook was created.
createdAtDateThe timestamp when this hook was created.
metadataunknownOptional metadata associated with the hook, set when the hook was created.

Examples

Basic API Route

Using resumeHook in a basic API route to resume a hook:

import { resumeHook } from "workflow/api";

export async function POST(request: Request) {
  const { token, data } = await request.json();

  try {
    const result = await resumeHook(token, data); 

    return Response.json({
      success: true,
      runId: result.runId
    });
  } catch (error) {
    return new Response("Hook not found", { status: 404 });
  }
}

With Type Safety

Defining a payload type and using resumeHook to resume a hook with type safety:

import { resumeHook } from "workflow/api";

type ApprovalPayload = {
  approved: boolean;
  comment: string;
};

export async function POST(request: Request) {
  const { token, approved, comment } = await request.json();

  try {
    const result = await resumeHook<ApprovalPayload>(token, { 
      approved, 
      comment, 
    }); 

    return Response.json({ runId: result.runId });
  } catch (error) {
    return Response.json({ error: "Invalid token" }, { status: 404 });
  }
}

Server Action (Next.js)

Using resumeHook in Next.js server actions to resume a hook:

"use server";

import { resumeHook } from "workflow/api";

export async function approveRequest(token: string, approved: boolean) {
  try {
    const result = await resumeHook(token, { approved });
    return result.runId;
  } catch (error) {
    throw new Error("Invalid approval token");
  }
}

Webhook Handler

Using resumeHook in a generic webhook handler to resume a hook:

import { resumeHook } from "workflow/api";

// Generic webhook handler that forwards data to a hook
export async function POST(request: Request) {
  const url = new URL(request.url);
  const token = url.searchParams.get("token");

  if (!token) {
    return Response.json({ error: "Missing token" }, { status: 400 });
  }

  try {
    const body = await request.json();
    const result = await resumeHook(token, body);

    return Response.json({ success: true, runId: result.runId });
  } catch (error) {
    return Response.json({ error: "Hook not found" }, { status: 404 });
  }
}