Plume URL

Using the NPM package

How to use Plume URL 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-url@latest

Using the package

The documentation of the package is available here.

example.js
const { PlumeURL } = require("@sodiumlabs/plume-url");

const plumeURL = new PlumeURL({ apiKey: "YOUR-API-KEY" });

const data = await plumeURL.createURL({
    url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
    description: "A cool video"
});

console.log("Shortened URL:", data.shorten);
example.js
import { PlumeURL } from "@sodiumlabs/plume-url";

const plumeURL = new PlumeURL({ apiKey: "YOUR-API-KEY" });

const data = await plumeURL.createURL({
    url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
    description: "A cool video"
});

console.log("Shortened URL:", data.shorten);

Using discord.js

Let's see how to integrate it with discord.js and how to handle errors.

It is recommended to add Plume URL directly on your client, so you can use it anywhere in your project:

index.js
const { Client } = require("discord.js");
const { PlumeURL } = require("@sodiumlabs/plume-url"); 

// Your discord.js client
const client = new Client({
    /* ... */
});

// Attach PlumeURL to your client
client.plumeURL = new PlumeURL(); 

Creating a shortened URL

For the next codes, we consider that you have the slash commands file structure as given in the guide.

commands/url/create.js
const { PlumeURLError } = require("@sodiumlabs/plume-url"); 

// ...

async execute(interaction) {
    const client = interaction.client;

    await interaction.deferReply({ flags: MessageFlags.Ephemeral });

    const url = interaction.options.getString("url", true);
    const description = interaction.options.getString("description");

    try { 
        // Create the shortened URL
        const data = await client.plumeURL.createURL({ 
            url, 
            description, 
            // The customId will let us know which url the user created,
            // so we can find them later in the search command
            customId: interaction.user.id, 
        }); 

        await interaction.editReply(`URL created! ${data.shorten}`); 
    } catch (err) { 
        if (err instanceof PlumeURLError && err.res?.status === 400) { 
            await interaction.editReply("Your URL is invalid!"); 
            return; 
        } 

        throw err; // other error, handle it how you want
    } 
}

Search the user's shortened URLs

commands/url/search.js
async execute(interaction) {
    const client = interaction.client;

    await interaction.deferReply({ flags: MessageFlags.Ephemeral });

    // which page to fetch (if you want to make a paginated menu)
    const page = interaction.options.getInteger("page") || 1; 

    // Search the shortened URLs
    const data = await this.client.plumeURL.search({ 
        customId: interaction.user.id, // only the urls of the user
        page, 
        limit: 10, // 10 urls per page
        expired: true, // also show the expired urls
    }); 

    if(!data.urls.length) { 
        await interaction.editReply("No URL found!"); 
        return; 
    } 

    // Display the user's URLs
    await interaction.editReply({ 
        embeds: { 
            title: "Your URLs", 
            fields: data.urls.map(u => ({ 
                name: `${u.shorten}`, 
                value: 
                    `Info: ${u.infoPage}` +
                    `\nViews: ${u.views}`, 
            })) 
        } 
    }); 
}

Delete a user's shortened URL

commands/url/delete.js
const { PlumeURLError } = require("@sodiumlabs/plume-url"); 

// ...

async execute(interaction) {
    const client = interaction.client;

    await interaction.deferReply({ flags: MessageFlags.Ephemeral });

    // The shortened URL id
    const id = interaction.options.getString("id", true); 

    try { 
        // Get the URL to verify that the user owns it
        const data = await this.client.plumeURL.getURL(id); 
        if (data.customId !== interaction.user.id) { 
            await interaction.editReply("Not your URL!"); 
            return; 
        } 

        // Delete the url
        await this.client.plumeURL.deleteURL(id); 

        await interaction.editReply("URL deleted!"); 
    } catch (err) { 
        if (err instanceof PlumeURLError && err.res?.status === 404) { 
            await interaction.editReply("URL not found!"); 
            return; 
        } 

        throw err; // other error, handle it how you want
    } 
}

On this page