Skip to content

SDK Quick Start

Terminal window
npm install anonpaste-sdk
# or
yarn add anonpaste-sdk
# or
pnpm add anonpaste-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();
const { id } = await anonpaste.pastes.create({
name: 'Hello from the SDK',
text: 'This paste was created programmatically!',
});
console.log(`https://anonpaste.com/${id}`);
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);
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);
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 URL

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}`);
}
}