How to use the pastebin-api npm package

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

2 min read

views

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.

Installation

npm install pastebin-api
bash

Creating the client

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

src/index.ts
import { PasteClient } from "pastebin-api";
typescript

Initializing the client

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

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

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:

PASTEBIN_API_KEY="REPLACE_ME"
bash

Creating a new paste

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

src/index.ts
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.
typescript
  • 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