-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathroute.ts
More file actions
39 lines (31 loc) · 1.13 KB
/
route.ts
File metadata and controls
39 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// The Replicate webhook is a POST request where the request body is a prediction object.
// Identical webhooks can be sent multiple times, so this handler must be idempotent.
import { NextResponse } from "next/server";
import { validateWebhook } from "replicate";
export async function POST(request: Request) {
console.log("Received webhook...");
const secret = process.env.REPLICATE_WEBHOOK_SIGNING_SECRET;
if (!secret) {
console.log(
"Skipping webhook validation. To validate webhooks, set REPLICATE_WEBHOOK_SIGNING_SECRET"
);
const body = await request.json();
console.log(body);
return NextResponse.json(
{ detail: "Webhook received (but not validated)" },
{ status: 200 }
);
}
const webhookIsValid = await validateWebhook(request.clone(), secret);
if (!webhookIsValid) {
return NextResponse.json(
{ detail: "Webhook is invalid" },
{ status: 401 }
);
}
// Process validated webhook here...
console.log("Webhook is valid!");
const body = await request.json();
console.log(body);
return NextResponse.json({ detail: "Webhook is valid" }, { status: 200 });
}