API Reference
AnonDocs provides a RESTful API for anonymizing text and documents. You can interact with the API directly using HTTP requests or use our official SDK for TypeScript/JavaScript.
SDK (Recommended)
The easiest way to use AnonDocs is through our official SDK.
Installation
npm install @aismarttalk/anondocs-sdk
Quick Start
import { AnonDocsClient } from '@aismarttalk/anondocs-sdk';
// Initialize the client
const client = new AnonDocsClient({
baseUrl: 'http://localhost:3000'
});
// Anonymize text
const result = await client.anonymizeText(
'My name is John Smith and my email is john@example.com'
);
console.log(result.anonymizedText);
// Output: "My name is [NAME] and my email is [EMAIL]"
console.log(result.piiDetected);
// Output: { names: ['John Smith'], emails: ['john@example.com'], ... }
SDK Features
- 🔒 Privacy-first anonymization
- 📄 Document support (PDF, DOCX, TXT)
- ✨ NEW in v1.2.0: DOCX formatting preservation with downloadable files
- 🔄 NEW in v1.2.0: Precise PII replacement mappings
- 🌊 Streaming with real-time progress updates
- 🎯 Full TypeScript support
- 🚀 Works in Node.js and Browser
- ⚡ Automatic provider selection
SDK Methods
Anonymize Text
const result = await client.anonymizeText('Your text here', {
provider: 'ollama' // Optional, omit to use default
});
console.log(result.anonymizedText);
console.log(result.piiDetected);
console.log(result.replacements); // NEW in v1.2.0 - precise PII mappings
Anonymize Document
Browser:
const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const result = await client.anonymizeDocument(file);
// NEW in v1.2.0: DOCX files return download URL for formatted documents
if (result.downloadUrl) {
// DOCX file with preserved formatting
window.open(result.downloadUrl); // Download formatted DOCX
console.log('Original:', result.originalFilename);
console.log('Anonymized:', result.filename);
} else {
// PDF or TXT - plain text only
console.log(result.anonymizedText);
}
Node.js:
import { readFileSync } from 'fs';
const fileBuffer = readFileSync('./document.docx');
const result = await client.anonymizeDocument(fileBuffer);
// Download formatted DOCX (v1.2.0+)
if (result.downloadUrl) {
const response = await fetch(result.downloadUrl);
const buffer = await response.arrayBuffer();
writeFileSync(result.filename, Buffer.from(buffer));
}
Streaming Anonymization
Get real-time progress updates:
await client.streamAnonymizeText(longText, {
onProgress: (event) => {
console.log(`${event.progress}% - ${event.message}`);
},
onComplete: (result) => {
console.log('Done!', result.anonymizedText);
},
onError: (error) => {
console.error('Error:', error.message);
}
});
Health Check
const health = await client.health();
// Returns: { status: 'ok', timestamp: '2025-11-04T...' }
SDK Documentation
For complete SDK documentation, visit: npm package
REST API
If you prefer to use the REST API directly, here are the available endpoints.
Base URL
http://localhost:3000
Endpoints
Anonymize Text
POST /api/anonymize
Anonymize plain text input.
Request Body:
{
"text": "Your text to anonymize",
"provider": "ollama"
}
Response:
{
"success": true,
"data": {
"anonymizedText": "Anonymized text",
"piiDetected": {
"names": ["John Doe"],
"emails": ["john@example.com"],
"phoneNumbers": [],
"addresses": [],
"dates": [],
"organizations": [],
"other": []
},
"replacements": [
{"original": "John Doe", "anonymized": "[NAME]"},
{"original": "john@example.com", "anonymized": "[EMAIL]"}
],
"chunksProcessed": 1,
"wordsPerMinute": 450,
"processingTimeMs": 1234
}
}
Anonymize Document
POST /api/document
Upload and anonymize PDF, DOCX, or TXT files.
Request: Multipart form data
file: The document fileprovider: (optional) "ollama" or "openai"
Response (DOCX files):
{
"success": true,
"data": {
"anonymizedText": "Anonymized text",
"piiDetected": { ... },
"replacements": [ ... ],
"chunksProcessed": 1,
"wordsPerMinute": 450,
"processingTimeMs": 1234,
"downloadUrl": "http://localhost:3000/api/document/download/anonymized-123.docx",
"filename": "anonymized-123.docx",
"originalFilename": "original.docx"
}
}
Response (PDF/TXT files): Same as text anonymization (no downloadUrl)
Stream Text Anonymization
POST /api/stream/anonymize
Stream anonymization with real-time progress updates using Server-Sent Events (SSE).
Request Body:
{
"text": "Your text to anonymize",
"provider": "ollama"
}
Response: text/event-stream with progress events
Stream Document Anonymization
POST /api/stream/document
Stream document anonymization with real-time progress updates.
Request: Multipart form data
file: The document fileprovider: (optional) "ollama" or "openai"
Response: text/event-stream with progress events
Health Check
GET /health
Check API health status.
Response:
{
"status": "ok",
"timestamp": "2024-11-03T12:00:00.000Z"
}
Error Responses
{
"success": false,
"error": "Error message"
}
Provider Options
ollama: Use local Ollama instance (default)openai: Use OpenAI-compatible API (vLLM, LM Studio, etc.)