English 中文(简体)
Clerk and Svix web hooks not working with error: "src property must be a valid json object"
原标题:
The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty. Ethan wants to draw more attention to this question.

I m trying to sync my Clerk data to my database in my Next js 13 project. My webhooks are publicly exposed with Ngrok. Here s my code:

import { IncomingHttpHeaders } from "http";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { Webhook, WebhookRequiredHeaders } from "svix";

const webhookSecret = process.env.WEBHOOK_SECRET || "";

async function handler(request: Request) {

  console.log(await request.json())

  const payload = await request.json();
  const headersList = headers();
  const heads = {
    "svix-id": headersList.get("svix-id"),
    "svix-timestamp": headersList.get("svix-timestamp"),
    "svix-signature": headersList.get("svix-signature"),
  };
  const wh = new Webhook(webhookSecret);
  let evt: Event | null = null;

  try {
    evt = wh.verify(
      JSON.stringify(payload),
      heads as IncomingHttpHeaders & WebhookRequiredHeaders
    ) as Event;
  } catch (err) {
    console.error((err as Error).message);
    return NextResponse.json({}, { status: 400 });
  }

  const eventType: EventType = evt.type;
  if (eventType === "user.created" || eventType === "user.updated") {
    const { id, ...attributes } = evt.data;
    console.log(attributes)
  }
}

type EventType = "user.created" | "user.updated" | "*";

type Event = {
  data: Record<string, string | number>;
  object: "event";
  type: EventType;
};

export const GET = handler;
export const POST = handler;
export const PUT = handler;

This code should do the following:

  1. Create an API route under /api/webhooks/user
  2. Fetch the payload and headers
  3. Validate this info with Svix
  4. Console the information

However, only step 1 is working as far as I can tell. In my Clerk dashboard I get an error:

{
  "message": "src property must be a valid json object"
}

Where am I going wrong?

问题回答

暂无回答




相关问题
Error when trying to use webhook with Google Forms

I am using Google Forms, and when I send the answer, I want for the answer to be sent via webhook. Here is my current code: var POST_URL = "my webhook url"; function onSubmit(e) { var ...

Testing http callback or webhooks [closed]

I wish to see the content of a http callback generated by an application (i.e. paypal or Google s pubsubhubbub). Does anybody know of a simple service that I can use as endpoint for a http post and ...

Create Wufoo webhook with PUT request in ColdFusion

I m having troubles with building correct PUT request to the Wufoo. In all my attempts I see the same error: 404 A WebHook must contain a url parameter. Here is the version with JSON data type: &...

Web hooks in Python: Any particular library?

I wanted to implement web hooks in python. Both at server end and client end. Is there any particular library for implementing web hooks? Or does django or twisted python handle this?

Implementing the server side of Webhooks

If I want to Webhooks-enable a web application (I m referring to the server-side of things, ie the server where the event happens and the callback is initiated from), are there libraries for this, or ...

热门标签