SDK Quick Start
Installation
Section titled “Installation”npm install anonpaste-sdk# oryarn add anonpaste-sdk# orpnpm add anonpaste-sdkInitialise the SDK
Section titled “Initialise the SDK”import { AnonPaste } from 'anonpaste-sdk';
const anonpaste = AnonPaste.init({ apiKey: process.env.ANONPASTE_API_KEY!,});AnonPaste.init() returns an instance and stores it globally. You can retrieve it later from anywhere in your app:
import { AnonPaste } from 'anonpaste-sdk';
const anonpaste = AnonPaste.getInstance();Create your first paste
Section titled “Create your first paste”const { id } = await anonpaste.pastes.create({ name: 'Hello from the SDK', text: 'This paste was created programmatically!',});
console.log(`https://anonpaste.com/${id}`);Create an encrypted paste
Section titled “Create an encrypted paste”const { id } = await anonpaste.pastes.encryptAndCreate({ name: 'My Secret', text: 'Only I can read this', encryptionKey: 'my-strong-passphrase',});
// Later, decrypt it:const paste = await anonpaste.pastes.getAndDecrypt(id, 'my-strong-passphrase');console.log(paste.decryptedText);Store ephemeral data with PasteLite
Section titled “Store ephemeral data with PasteLite”const { id } = await anonpaste.pasteLite.create({ name: 'session-token', data: JSON.stringify({ userId: 42, token: 'abc123' }), burnAfterView: true, // self-destructs on first read expire: new Date(Date.now() + 60 * 60 * 1000), // 1 hour});
// Read and auto-delete in one call:const paste = await anonpaste.pasteLite.get(id.split(':')[0]);const { userId, token } = JSON.parse(paste.data);Upload a file
Section titled “Upload a file”import { readFileSync } from 'fs';
const { fileId, readUrl } = await anonpaste.files.upload({ fileName: 'report.pdf', contentType: 'application/pdf', fileData: readFileSync('./report.pdf'), tags: ['reports'],});console.log(readUrl); // presigned download URLError handling
Section titled “Error handling”All SDK methods throw AnonPasteError on failure:
import { AnonPaste, AnonPasteError } from 'anonpaste-sdk';
try { await anonpaste.pastes.get('doesnotexist');} catch (err) { if (err instanceof AnonPasteError) { console.error(`[${err.status}] ${err.reason}`); }}