Skip to main content

Sapphire Logo

@sapphire/plugin-api

Plugin for @sapphire/framework to expose a REST API.

GitHub npm bundle size npm

Description

This plugin provides an API endpoint for your bot that can be called from external services. A good exemplary use case for this is once your bot grows to have enough configuration options that you want to offer a website to your end-users to change those settings, and your website needs to interface with the bot for this to work.

Features

  • Fully ready for TypeScript!
  • Follows common REST standards.
  • Includes ESM ready entrypoint
  • Premade OAuth 2.0 endpoints

Installation

@sapphire/plugin-api depends on the following packages. Be sure to install these along with this package!

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install @sapphire/plugin-api @sapphire/framework discord.js discord-api-types

Usage

Start by importing the registration file in your project to use the plugin:

JavaScript

require('@sapphire/plugin-api/register');

TypeScript

import '@sapphire/plugin-api/register';

Then, you can use the following configuration options in your SapphireClient extension class or initializer. This will either be located in your new SapphireClient constructor call, or super in your constructor method if you use an extension class.

{
auth: {
// The application/client ID of your bot.
// You can find this at https://discord.com/developers/applications
id: '',
// The client secret of your bot.
// You can find this at https://discord.com/developers/applications
secret: '',
// The name of the authentication cookie.
cookie: 'SAPPHIRE_AUTH',
// The URL that users should be redirected to after a successful authentication
redirect: '',
// The scopes that should be given to the authentication.
scopes: [OAuth2Scopes.Identify],
// Transformers to transform the raw data from Discord to a different structure.
transformers: []
},
// The prefix for all routes, e.g. / or v1/.
prefix: '',
// The origin header to be set on every request at 'Access-Control-Allow-Origin.
origin: '*',
// Any options passed to the NodeJS "net" internal server.listen function
// See https://nodejs.org/api/net.html#net_server_listen_options_callback
listenOptions: {
// The port the API will listen on.
port: 4000
}
}

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, Paypal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate WithAddress
Open CollectiveClick Here
Ko-fiClick Here
PatreonClick Here
PayPalClick Here

Contributors

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to Sapphire!

Namespaces

NamespaceDescription
Middleware-
Route-

Enumerations

EnumerationDescription
HttpCodes-
ServerEvent-

Classes

ClassDescription
ApiRequest-
ApiResponse-
Auth-
CookieStore-
Middleware-
MiddlewareStore-
Route-
RouterBranch-
RouterNode-
RouterRoot-
RouteStore-
Server-

Interfaces

InterfaceDescription
AuthDataDefines the authentication data, this is to be encrypted and decrypted by the server.
LoginDataThe login data sent when fetching data from a user.
LoginDataTransformerA login data transformer.
MiddlewareOptionsThe options for all middlewares.
RouteOptions-
SecureCookieStoreSetOptions-
ServerEvents-
ServerOptionsThe API options.
ServerOptionsAuthDefines the authentication options.

Type Aliases

Type AliasDescription
AuthLessServerOptionsThe ServerOptions without ServerOptions.auth.
ContentTypeParameterRFC 1341 4: Defines a Content-Type's parameter, which follows the following structure:
ContentTypeTypeRFC 1341 4: Defines a Content-Type's type, which follows the following structure:
GenericMimeTypeRFC 1341 4: Defines the syntax for a Content-Type field without parameters, which follows the following structure: type "/" subtype.
GenericParametrizedMimeTypeRFC 1341 4: Defines the syntax for a Content-Type field, which follows the following structure: type "/" subtype *[";" parameter].
MethodName-
MimeType-
ValidatorFunction-

Variables

VariableDescription
MethodNames-
versionThe @sapphire/plugin-api version that you are currently using. An example use of this is showing it of in a bot information command.

Functions

FunctionDescription
loadListeners-
loadMiddlewares-
loadRoutes-