webhooks | clerk turbo repo | createCaller | Cannot set headers after they are sent to the client

import { Webhook } from "svix";
import { buffer } from "micro";
import { ClerkResponse } from "../../../../utils/clerk";
import { trpc } from "../../../../utils/trpc";
import { PrismaClient } from "@acme/db";
import { btcRouter } from "@acme/api/src/router/btc";
import { createContext, appRouter } from "@acme/api";
import { createContextInner } from "@acme/api/src/context";

// import { createContext } from "@acme/api";

const prisma = new PrismaClient()


export const config = {
api: {
bodyParser: false,
},
}

const secret = process.env.CLERK_WEBHOOK_SECRET;

export default async function handler(req, res) {
const ctx = await createContext({ req, res });
const caller = appRouter.createCaller(ctx);


const payload = (await buffer(req)).toString();
const headers = req.headers;

const wh = new Webhook(secret as string);
try {
// Verify the webhook signature and parse the payload
const msg: unknown = wh.verify(payload, headers);
console.log('New user created!', (msg as ClerkResponse).data); // The payload of the webhook
console.log('User ID:', (msg as ClerkResponse).data.id); // The user ID


const new_user = await caller.btc.createVA({ userId: (msg as ClerkResponse).data.id, });

etc...
import { Webhook } from "svix";
import { buffer } from "micro";
import { ClerkResponse } from "../../../../utils/clerk";
import { trpc } from "../../../../utils/trpc";
import { PrismaClient } from "@acme/db";
import { btcRouter } from "@acme/api/src/router/btc";
import { createContext, appRouter } from "@acme/api";
import { createContextInner } from "@acme/api/src/context";

// import { createContext } from "@acme/api";

const prisma = new PrismaClient()


export const config = {
api: {
bodyParser: false,
},
}

const secret = process.env.CLERK_WEBHOOK_SECRET;

export default async function handler(req, res) {
const ctx = await createContext({ req, res });
const caller = appRouter.createCaller(ctx);


const payload = (await buffer(req)).toString();
const headers = req.headers;

const wh = new Webhook(secret as string);
try {
// Verify the webhook signature and parse the payload
const msg: unknown = wh.verify(payload, headers);
console.log('New user created!', (msg as ClerkResponse).data); // The payload of the webhook
console.log('User ID:', (msg as ClerkResponse).data.id); // The user ID


const new_user = await caller.btc.createVA({ userId: (msg as ClerkResponse).data.id, });

etc...
current code of web hook. I am trying to call trpc router from a webhook located at
1 Reply
tainoooo
tainoooo2y ago
@acme/nextjs:dev: User ID: user_29w83sxmDNGwOuEthce5gg56FcC @acme/nextjs:dev: Creating new user in Tatum @acme/nextjs:dev: error - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client halp how to call trpc in webhook? :S
Want results from more Discord servers?
Add your server