PasteDB Node.js SDK

The official Node.js SDK for interacting with the PasteDB API.

Quick Start (30 seconds)

1. Install

npm install pastedb-js

2. Import

const { Client } = require("pastedb-js");

3. Create client

const client = new Client("YOUR_API_KEY");

4. First API call

async function main() {
  const me = await client.me();
  console.log(me);
}
main();

Expected Output

{
  "email": "user@example.com",
  "id": "usr_123"
}

Authentication

API key is optional. Pass it to the constructor.

const user = await client.me();

Paste Methods

📄 createPaste()

Create a new paste in seconds.


            const { Client } = require("pastedb-js");

const client = new Client("YOUR_API_KEY");

async function main() {
    const paste = await client.createPaste({
        title: "Hello",
        content: "Hello World",
        language: "javascript",
        images:[]
    });

    console.log(paste);
}

main().catch(console.error);
📥 getPaste(id)

Fetch a paste by ID.

await const paste = await client.getPaste("abc123");
✏️ updatePaste(id, data)

Update title, content, etc.

await client.updatePaste("abc123", { title: "Updated" });
📊 pasteStats(id)

Get views, copies, etc.

const stats = await client.pasteStats("abc123");
🖼️ getImages(id)

Get all images attached to a paste.

const images = await client.getImages("abc123");
🔍 explore()

Get public trending pastes.

const posts = await client.explore();
▶️ runCode(lang, code)

Execute code in the sandbox.

const result = await client.runCode("python", "print('Hi')");
🔗 checkCustomId(id)

Check if a custom URL is available.

const available = await client.checkCustomId("my-paste");

API Key Management

🔑 generateApiKey(name)
await client.generateApiKey("My App");
📋 myApiKeys()
await client.myApiKeys();
🗑️ deleteApiKey(key)
await client.deleteApiKey(API_KEY);

Error Handling

const { Client, PasteDBError } = require("pastedb-js");
try {
    await client.me();
} catch (err) {
    if (err instanceof PasteDBError) {
        console.log(err.message);
    }
}

Throws `PasteDBError` for API and timeout errors. Uses native `fetch()` Node 18+

Full Example

const { Client } = require("pastedb-js");
const client = new Client("YOUR_API_KEY");
async function run() {
  const paste = await client.createPaste({
    title: "Test",
    content: "Hello from SDK",
    language: "js"
  });
  console.log("Created:", paste.id);
}
run();
Copied!