Using the NPM package
How to use Plume API using the official NPM package.
To make it easier to use our API, we have created an official NPM package for JavaScript and TypeScript. Here is a quick tutorial on how to use it.
Installing the package
To install the package, run this command in your project:
npm i @sodiumlabs/plume-api@latestUsing the package
The documentation of the package is available here.
const { PlumeAPI } = require("@sodiumlabs/plume-api");
// create a PlumeAPI class instance
const plumeAPI = new PlumeAPI();
// fetch the /api/math endpoint
const data = await plumeAPI.math("100*sqrt(15^30)/sin(10PI)");
// log the data
console.log(data);
// {
// "result": "-3.575674967555855e+34"
// }import { PlumeAPI } from "@sodiumlabs/plume-api";
// create a PlumeAPI class instance
const plumeAPI = new PlumeAPI();
// fetch the /api/math endpoint
const data = await plumeAPI.math("100*sqrt(15^30)/sin(10PI)");
// log the data
console.log(data);
// {
// "result": "-3.575674967555855e+34"
// }Using discord.js
If you are using discord.js, it is recommended to add Plume API directly on your client, so you can use it anywhere in your project.
const { Client } = require("discord.js");
const { PlumeAPI } = require("@sodiumlabs/plume-api");
// Your discord.js client
const client = new Client({
/* ... */
});
// Attach PlumeAPI to your client
client.plumeAPI = new PlumeAPI(); You can now use Plume API anywhere in your bot. For example, if you followed the guide you can use the API in a slash command like that:
async execute(interaction) {
const client = interaction.client;
// Get a random joke
const joke = await client.plumeAPI.joke();
// Show the response
await interaction.reply(
`Question: ${joke.question}`
+ `\nAnswer: ||${joke.answer}||`
);
}Images endpoints
To send an image received from the API, you can use the AttachmentBuilder class from discord.js:
const { AttachmentBuilder } = require("discord.js");
async execute(interaction) {
const client = interaction.client;
// Since downloading the image can take some time
// depending on your connection speed, you should defer.
await interaction.deferReply();
const buffer = await client.plumeAPI.facts("PlumeAPI is cool!"); // this returns an image (buffer)
const attachment = new AttachmentBuilder(buffer, { name: "image.png" });
await interaction.editReply({ files: [attachment] });
}