Discord-Interation : Getting "Bad request signature" error while verifying the key

933 Views Asked by At

I am trying this official discord bot example https://github.com/discord/discord-example-app. While running the code and setting up the interaction URL in discord developer portal. I am getting "Bad request signature" error in NodeJS.

enter image description here

The bot verification code is as per below

app.js

// Parse request body and verifies incoming requests using discord-interactions package
app.use(express.json({verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));

utils.js

export function VerifyDiscordRequest(clientKey) {
  return function (req, res, buf, encoding) {
    const signature = req.get('X-Signature-Ed25519');
    const timestamp = req.get('X-Signature-Timestamp');

    const isValidRequest = verifyKey(buf, signature, timestamp, clientKey);
    if (!isValidRequest) {
      res.status(401).send('Bad request signature');
      throw new Error('Bad request signature');
    }
  };
}

I am assuming express body-parser is causing some issues but not completely sure. Reference - https://github.com/discord/discord-example-app

3

There are 3 best solutions below

1
discordtinkerer On

While running the code and setting up the interaction URL

To anyone, who's trying out the discord bot example and is getting thrown "Bad request signature" error like I am, make sure your "Interactions endpoint URL" string (in your bot's General Information page) ends with /interactions just like the following example URL: https://mybotserverurl.com/interactions

0
Tom Schulz On

I was doing the same tutorial and I decided to use ngrok to run the server vs Glitch. I followed the setup, copied the ngrok url, added /interactions and still got the "Error: Bad request signature" output from my node instance.

Then I reread the ngrok setup in the github repository, and it said: "If you aren't following the getting started guide, you can move the contents of examples/app.js (the finished app.js file) to the top-level app.js."

So, I thought I was following along with the getting started guide, but why not jump to the end in case there was some step I missed.

Boom, my hello world bot started working.

This is probably the same problem in Glitch, too. I guess the writer didn't think people would immediately start hitting endpoints before completing the tutorial.

0
Elton On

Import verifyKeyMiddleware form 'discord-interactions' and directly include it in your interactions post route.

import express from 'express';
import {verifyKeyMiddleware} from 'discord-interactions';
const app = express();
app.post('/interactions', verifyKeyMiddleware(publickey), (req, res) => {
  const message = req.body;
  console.log(message)
});