Skip to Content
nyx logonyx guide

Configuration

Replace the parts of the HTTP client.

Reading time:2 min read

Every part of the client can be replaced through clientInjections (or injections, when calling HttpNyxClient.create() yourself):

InjectionDefaultDescription
verifierDefaultSignatureVerifierVerifies Discord's Ed25519 signatures.
restA REST with the bot's tokenThe REST client used by api.
apiAn API over restThe API passed to commands and subscribers.
emitterA BasicAwaitableEventEmitterThe emitter interactions and webhook events are emitted on. Its emit() resolves when the listeners finish, which is what handled waits for.
acknowledgeTimeout3_000How long to wait for an interaction to be acknowledged, in milliseconds.
maxTimestampAge300_000How old (or how far in the future) a request can be before it's rejected, in ms.
import {  } from '@nyx-discord/http';

const  = .(() => ({
  : .APP_ID,
  : .DISCORD_PUBLIC_KEY,
  : .TOKEN,
  : ,
  : true,
  : {
    : 2_500,
    : 60_000,
  },
}));

Signature verification

The default verifier checks signatures with the Web Crypto API (crypto.subtle), so it needs no dependencies and works on Node.js, Bun, Deno and Cloudflare Workers.

On runtimes without a global crypto.subtle, like older Node.js versions, pass one yourself:

import {  } from 'node:crypto';
import { ,  } from '@nyx-discord/http';

const  = .(() => ({
  : .APP_ID,
  : .DISCORD_PUBLIC_KEY,
  : .TOKEN,
  : ,
  : true,
  : {
    : .({
      : .DISCORD_PUBLIC_KEY,
      : . as ,
    }),
  },
}));

Or implement SignatureVerifier docs to use any other library. For example, with tweetnacl:

import  from 'tweetnacl';
import { , type SignatureVerifier } from '@nyx-discord/http';

class  implements SignatureVerifier {
  protected readonly : ;

  protected readonly  = new ();

  constructor(: string) {
    this. = .(, 'hex');
  }

  public async (: string | , : string, : string) {
    // Discord signs the timestamp followed by the raw body
    const  = typeof  === 'string' ? this..() : ;
    const  = .([this..(), ]);

    try {
      return ...(, .(, 'hex'), this.);
    } catch {
      // tweetnacl throws on malformed signatures, which are just invalid requests
      return false;
    }
  }
}

const  = .(() => ({
  : .APP_ID,
  : .DISCORD_PUBLIC_KEY,
  : .TOKEN,
  : ,
  : true,
  : {
    : new (.DISCORD_PUBLIC_KEY),
  },
}));
Edit on GitHub

Last updated on

On this page