kstulgys
kstulgys15mo ago

Why webhooks does not work with TRPC?

I have a what suppose to be a webhook:
deposit: publicProcedure.input(z.any()).mutation(async ({ ctx, input }) => {
console.log(input);
return { input };
}),
deposit: publicProcedure.input(z.any()).mutation(async ({ ctx, input }) => {
console.log(input);
return { input };
}),
When I POST to this endpoint it responds with:
{
"result": {
"data": {
"json": {
"input": null
},
"meta": {
"values": {
"input": [
"undefined"
]
}
}
}
}
}
{
"result": {
"data": {
"json": {
"input": null
},
"meta": {
"values": {
"input": [
"undefined"
]
}
}
}
}
}
Why? console logs undefined on the server.
5 Replies
Nick
Nick15mo ago
It’s because you have a transformer set up, that’s the transformer format
kstulgys
kstulgysOP15mo ago
well, same thing without transformer:
{
"result": {
"data": {}
}
}
{
"result": {
"data": {}
}
}
NEO
NEO15mo ago
import { buffer } from "micro";

const stripeWebhook = publicProcedure
.meta({
openapi: {
method: "POST",
path: "/stripe-webhook",
},
})
.input(z.void())
.output(
z.object({
status: z.string(),
})
)
.mutation(async ({ ctx }) => {
const buf = await buffer(ctx.req);
const request = buf.toString("utf8");
});
import { buffer } from "micro";

const stripeWebhook = publicProcedure
.meta({
openapi: {
method: "POST",
path: "/stripe-webhook",
},
})
.input(z.void())
.output(
z.object({
status: z.string(),
})
)
.mutation(async ({ ctx }) => {
const buf = await buffer(ctx.req);
const request = buf.toString("utf8");
});
this is how I handle my stripe webhook, the body is somewhere is request you'll have to figure it out
Nick
Nick15mo ago
Undefined keys get dropped from normal JSON serialisation, so that also looks correct You haven’t said if you’re posting any body to the mutation, so I have to assume it’s just a straight post
kstulgys
kstulgysOP15mo ago
actually it's working, all good
deposit: publicProcedure.input(z.any()).mutation(({ ctx, input }) => {
console.log(input);
return { input };
}),
deposit: publicProcedure.input(z.any()).mutation(({ ctx, input }) => {
console.log(input);
return { input };
}),
{
"result": {
"data": {
"input": {
"hello": "world"
}
}
}
}
{
"result": {
"data": {
"input": {
"hello": "world"
}
}
}
}
I've been posting to this endpoint without content-type: application/json header before.

Did you find this page helpful?