Setting Up Webhooks with ComfyDeploy

Enhance your application with real-time updates using ComfyDeploy's webhook support

By ComfyDeploy

|

2023-04-15

|

API

We have recently added webhook support.

When you create a run, you can also pass in a webhook endpoint to receive the callback.

export async function generate(prompt: string) { const headersList = headers(); const host = headersList.get("host") || ""; const protocol = headersList.get("x-forwarded-proto") || ""; const endpoint = ${protocol}://${host};

return await client.run({
    deployment_id: process.env.COMFY_DEPLOYMENT_ID!,
    inputs: {
        "positive_prompt": prompt
    },
    webhook: `${endpoint}/api/webhook`
})

}

For the webhook route, here’s an example.

import { parseWebhookDataSafe } from "comfydeploy"; import { NextResponse } from "next/server";

export async function POST(request: Request) {
    const [_data, error] = await parseWebhookDataSafe(request);
    if (!_data || error) return error;

    const { status, run_id, outputs } = _data;

    // Do your things
    console.log(status, run_id, outputs);

    return NextResponse.json({ message: "success" }, { status: 200 });
}

p.s. You might want to keep track of some extra metadata for the run, you can also do so with query pararms

webhook: ${endpoint}/api/webhook?userId=b190ce97-d7ae-40fa-948c-79304ae7ac9b

And here’s how to get it back.

export async function POST(request: Request) {
    const [_data, error] = await parseWebhookDataSafe(request);
    if (!_data || error) return error;

    const { status, run_id, outputs } = _data;

    const url = new URL(request.url);
    const queryParams = Object.fromEntries(url.searchParams);
    const userId = queryParams["userId"]

    // Do your things
    console.log(status, run_id, outputs);

    return NextResponse.json({ message: "success" }, { status: 200 });
}