Skip to main content

validateWebhookSignature()

从 v3.2.30 版本开始可用

验证由 webhook 端点接收的签名是否真实。如果验证失败,将抛出错误。

API

该函数接受一个包含三个键值对的对象:

secret

renderMediaOnLambda() 的 webhook 选项传递的相同的 webhook 密钥。

body

端点接收的主体 - 接受解析后的 JSON 对象,而不是 string

signatureHeader

从端点接收的请求的 X-Remotion-Signature 标头。

示例

在下面的 Next.JS webhook 端点中,如果签名与预期的签名不匹配或缺失,则会抛出错误。

pages/api/webhook.ts
tsx
import {
validateWebhookSignature,
WebhookPayload,
} from "@remotion/lambda/client";
 
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
validateWebhookSignature({
secret: process.env.WEBHOOK_SECRET as string,
body: req.body,
signatureHeader: req.headers["x-remotion-signature"] as string,
});
 
// If code reaches this path, the webhook is authentic.
const payload = req.body as WebhookPayload;
if (payload.type === "success") {
// ...
} else if (payload.type === "timeout") {
// ...
}
 
res.status(200).json({
success: true,
});
}
pages/api/webhook.ts
tsx
import {
validateWebhookSignature,
WebhookPayload,
} from "@remotion/lambda/client";
 
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
validateWebhookSignature({
secret: process.env.WEBHOOK_SECRET as string,
body: req.body,
signatureHeader: req.headers["x-remotion-signature"] as string,
});
 
// If code reaches this path, the webhook is authentic.
const payload = req.body as WebhookPayload;
if (payload.type === "success") {
// ...
} else if (payload.type === "timeout") {
// ...
}
 
res.status(200).json({
success: true,
});
}

查看 Webhooks 以获取 Express 示例。

另请参阅