-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from replicate/webhook-validation-on-app-router
add support for optionally requesting, receiving, and validating webhooks
- Loading branch information
Showing
7 changed files
with
414 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Find your Replicate API token at https://replicate.com/account/api-tokens | ||
# REPLICATE_API_TOKEN= | ||
|
||
# Use ngrok to expose your local server to the internet | ||
# so the Replicate API can send it webhooks | ||
# | ||
# e.g. https://8db01fea81ad.ngrok.io | ||
NGROK_HOST= | ||
|
||
# Shared secret for verifying that incoming webhooks are sent by Replicate | ||
# See https://replicate.com/docs/webhooks#retrieving-the-webhook-signing-key | ||
REPLICATE_WEBHOOK_SIGNING_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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) { | ||
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 }); | ||
} |
Oops, something went wrong.