pastedb-js
v1.0.4
Published
Official PasteDB Node.js SDK
Maintainers
Readme
PasteDB JavaScript SDK
Official Node.js SDK for interacting with the PasteDB API.
Create, retrieve, update, delete, and explore pastes, execute code, manage API keys, retrieve paste statistics, and more — directly from your Node.js application.
Installation
npm install pastedb-jsRequirements
- Node.js 18+ recommended
- A PasteDB API key for authenticated operations
The SDK uses the built-in
fetchAPI available in modern versions of Node.js.
Quick Start
const { Client } = require("pastedb-js");
const client = new Client("YOUR_API_KEY");
async function main() {
const user = await client.me();
console.log(user);
}
main().catch(console.error);Configuration
Client
const { Client } = require("pastedb-js");
const client = new Client(
"YOUR_API_KEY",
"https://pastedb-rw62.onrender.com"
);Both parameters are optional:
const client = new Client();The default API URL is:
https://pastedb-rw62.onrender.comIf an API key is provided, the SDK automatically sends it using both:
x-api-keyAuthorization: Bearer <API_KEY>
API Reference
Get Current User
const user = await client.me();Equivalent endpoint:
GET /api/meCreate a Paste
const paste = await client.createPaste({
title: "My Paste",
content: "Hello from PasteDB!",
images:[]
});Equivalent endpoint:
POST /createThe data object is passed directly to the API, allowing you to provide the fields supported by PasteDB.
Get a Paste
const paste = await client.getPaste("paste-id");Equivalent endpoint:
GET /p/:pasteIdUpdate a Paste
const updated = await client.updatePaste("paste-id", {
title: "Updated Title",
content: "Updated content",
images:["url1","url2"]
});Equivalent endpoint:
PUT /api/paste/:pasteIdExplore Public Pastes
const pastes = await client.explore();Equivalent endpoint:
GET /exploreRun Code
const result = await client.runCode(
"javascript",
'console.log("Hello, PasteDB!")'
);Equivalent endpoint:
POST /runRequest body:
{
"language": "javascript",
"code": "console.log(\"Hello, PasteDB!\")"
}Get Paste Images
const images = await client.getImages("paste-id");Equivalent endpoint:
GET /images/:pasteIdGet Paste Statistics
const stats = await client.pasteStats("paste-id");Equivalent endpoint:
GET /stats/:pasteIdCheck a Custom Paste ID
const result = await client.checkCustomId("my-custom-id");Equivalent endpoint:
GET /check-id?id=my-custom-idAPI Key Management
Generate an API Key
const apiKey = await client.generateApiKey("My Application");Equivalent endpoint:
POST /generate-api-keyRequest body:
{
"name": "My Application"
}List API Keys
const keys = await client.myApiKeys();Equivalent endpoint:
GET /my-api-keysDelete an API Key
await client.deleteApiKey("API_KEY");Equivalent endpoint:
DELETE /delete-api-key/:apiKeyKeep API keys private and never commit them to source control.
Direct API Methods
The SDK also exposes methods corresponding to the /api endpoints.
API User
const user = await client.apiMe();GET /api/meCreate a Paste
const paste = await client.apiCreatePaste({
title: "API Paste",
content: "Created through the API"
});POST /api/createGet a Paste
const paste = await client.apiGetPaste("paste-id");GET /api/paste/:pasteIdDelete a Paste
await client.apiDeletePaste("paste-id");DELETE /api/paste/:pasteIdUpdate a Paste
const paste = await client.apiUpdatePaste("paste-id", {
title: "Updated Paste"
});PUT /api/paste/:pasteIdGet Your Pastes
const pastes = await client.apiUserPastes();GET /api/pastesError Handling
The SDK provides a custom PasteDBError class for API and request errors.
const { Client, PasteDBError } = require("pastedb-js");
const client = new Client("YOUR_API_KEY");
try {
const paste = await client.getPaste("invalid-id");
console.log(paste);
} catch (error) {
if (error instanceof PasteDBError) {
console.error("PasteDB error:", error.message);
} else {
console.error("Unexpected error:", error);
}
}Request Timeout
Requests automatically time out after 30 seconds.
A timeout throws:
PasteDBError: Request timed out.HTTP errors are also converted into PasteDBError instances and include the HTTP status code and API response.
Complete Example
const { Client, PasteDBError } = require("pastedb-js");
const client = new Client(process.env.PASTEDB_API_KEY);
async function main() {
try {
// Check the authenticated user
const user = await client.me();
console.log("User:", user);
// Create a paste
const paste = await client.createPaste({
title: "My First Paste",
content: "Hello from pastedb-js!",
images:[]
});
console.log("Created paste:", paste);
// Retrieve the paste
const fetched = await client.getPaste(paste.id);
console.log("Fetched paste:", fetched);
// Get statistics
const stats = await client.pasteStats(paste.id);
console.log("Stats:", stats);
} catch (error) {
if (error instanceof PasteDBError) {
console.error("PasteDB error:", error.message);
} else {
console.error(error);
}
}
}
main();Exported Classes
The package exports:
const {
Client,
PasteDBError
} = require("pastedb-js");Client
Main SDK client used to communicate with PasteDB.
PasteDBError
Custom error class used for PasteDB request failures and timeouts.
Project Structure
pastedb-js/
├── lib/
│ └── client.js
├── index.js
├── LICENSE
├── package.json
└── README.mdLicense
This project is licensed under the MIT License.
Author
Aditya Sorathiya
Package
pastedb-js — Official PasteDB Node.js SDK.
