How to use the pastebin-api npm package

Learn more how to use the pastebin-api npm package I created.

2 min read


pastebin-api is a npm package I created to interact with the Pastebin API to create pastes, list pastes and delete pastes. It has a very intuitive API and is very easy to use.


npm install pastebin-api

Creating the client

Firstly, we must import the PasteClient class from the package:

import { PasteClient } from "pastebin-api";

Initializing the client

Secondly, we must initialize the client with your API key from your pastebin account

import { PasteClient } from "pastebin-api";
const client = new PasteClient(process.env.PASTEBIN_API_KEY);

Securely storing the Pastebin API token

To securely store your Pastebin API token, I recommend you create a .env file in the root of your project and add the following line to it:


Creating a new paste

Now we'll create a new paste using the .createPaste method:

import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
const client = new PasteClient(process.env.PASTEBIN_API_KEY);
const pasteUrl = await client.createPaste({
  code: "const something = 'Hello World!'",
  expireDate: ExpireDate.Never,
  format: "javascript",
  name: "something.js",
  publicity: Publicity.Public,
console.log(pasteUrl); // if an error occurred, it'll be thrown to the console.
  • code: The code we want to send in the Paste
  • expireDate: When should the paste expire?
  • format: The format, "javascript", "java", "lua", etc
  • name: The name of the paste
  • publicity: Whether a paste should be public, unlisted or private

Woohoo 🎉

There you have it! We've successfully created a new paste! Read more on the docs