upstash / qstash-js

Message queue for serverless
https://docs.upstash.com/qstash
MIT License
135 stars 12 forks source link

Receving via Next.js API in Vercel Function #7

Closed kay-is closed 1 year ago

kay-is commented 1 year ago

I'm trying to verify a qStash request in a Next.js API route that runs on Vercel functions.

This is the code of the route that is called by qStash:

import { Receiver } from "@upstash/qstash"

const qstashReceiver = new Receiver({
  currentSigningKey: process.env.QSTASH_CURRENT_SIGNING_KEY,
  nextSigningKey: process.env.QSTASH_NEXT_SIGNING_KEY,
})

export default async function handler(request, response) {
  const isValid = await qstashReceiver.verify({
    signature: request.headers["Upstash-Signature"],
    body: request.body,
  })

  console.log("Is Valid:", isValid)

  response.end(isValid)
}

I get the following error:

{"email":"test@example.com","text":"a quick reminder"}
2022-08-05T13:14:01.812Z    237e58ef-6d32-484b-9bcd-0f05b7be7380    ERROR   TypeError: Cannot read properties of undefined (reading 'split')
    at Receiver.verifyWithKey (file:///var/task/node_modules/@upstash/qstash/esm/pkg/receiver.js:55:37)
    at Receiver.verify (file:///var/task/node_modules/@upstash/qstash/esm/pkg/receiver.js:45:36)
    at handler (/var/task/.next/server/pages/api/notify.js:50:42)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:184:9)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:381:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/base-server.js:491:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:213:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:610:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:311:20)
2022-08-05T13:14:01.812Z    237e58ef-6d32-484b-9bcd-0f05b7be7380    ERROR   TypeError: Cannot read properties of undefined (reading 'split')
    at Receiver.verifyWithKey (file:///var/task/node_modules/@upstash/qstash/esm/pkg/receiver.js:55:37)
    at Receiver.verify (file:///var/task/node_modules/@upstash/qstash/esm/pkg/receiver.js:45:36)
    at handler (/var/task/.next/server/pages/api/notify.js:50:42)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:184:9)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:381:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/base-server.js:491:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:213:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:610:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:311:20)
RequestId: 237e58ef-6d32-484b-9bcd-0f05b7be7380 Error: Runtime exited without providing a reason
Runtime.ExitError
chronark commented 1 year ago

the headers are all lower cased. try request.headers["upstash-signature"],

Or you can use the this shortcut:

import { verifySignature } from "@upstash/qstash/nextjs";

async function handler(req, res) {
  console.log("If this is printed, the signature has already been verified");

  // do stuff
  res.status(200).end();
}

export default verifySignature(handler);

export const config = {
  api: {
    bodyParser: false,
  },
};
chronark commented 1 year ago

Seeing that you have finished the blog post, I assume this issue is resolved, @kay-is ?

kay-is commented 1 year ago

Yes, thanks!