JSPM

  • Created
  • Published
  • Downloads 6055
  • Score
    100M100P100Q116803F
  • License MIT

A library for verifying the authenticity of requests coming from the Discord Interactions API

Package Exports

  • discord-verify

Readme

discord-verify

This package is used to efficiently verify Discord HTTP interactions.

Installation

npm install discord-verify

Usage

Web Environments

import { isValidRequest } from "discord-verify";

const isValid = await isValidRequest(request, publicKey);

Node Environments

import { isValidRequest } from "discord-verify/node";

const isValid = await isValidRequest(request, publicKey);

Custom Validation

If you want to validate requests from frameworks such as Express or Fastify that have their own request classes, you can import the validate function and pass raw values to it.

import { validate, hexToBinary } from "discord-verify";

function handleRequest(
    req: FastifyRequest<{
        Body: APIInteraction;
        Headers: {
            "x-signature-ed25519": string;
            "x-signature-timestamp": string;
        };
    }>,
    res: FastifyReply
) {
    const signature = req.headers["x-signature-ed25519"];
    const timestamp = req.headers["x-signature-timestamp"];
    const rawBody = JSON.stringify(req.body);

    const isValid = await validate(
        rawBody,
        hexToBinary(signature),
        timestmap,
        this.client.publicKey,
        crypto.subtle
    );

    if (!isValid) {
        return res.code(401).send("Invalid signature");
    }
}

Options

isValidRequest takes an optional third argument to specify the algorithm to use. This can be a string or object containing name and namedCurve. For convenience, discord-verify exports PlatformAlgorithm that contains values used by common platforms. You can use it like this:

import { isValidRequest, PlatformAlgorithm } from "discord-verify";

const isValid = await isValidRequest(
    request,
    publicKey,
    PlatformAlgorithm.Vercel
);

The following platforms are currently supported:

  • Vercel
  • CloudFlare