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:
import { PasteClient } from "pastebin-api";
typescript
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);
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:
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 PasteexpireDate
: When should the paste expire?format
: The format, "javascript", "java", "lua", etcname
: The name of the pastepublicity
: 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