Skip to main content
Creates and configures an anonymizer instance for PII detection and replacement.

Signature

function createAnonymizer(config?: AnonymizerConfig): Anonymizer

Parameters

config (optional)

mode
string
default:"'pseudonymize'"
Anonymization mode:
  • 'pseudonymize' — Reversible. Returns an encrypted piiMap for later rehydration.
  • 'anonymize' — Irreversible. No piiMap is returned; PII is permanently removed.
ner
NERConfig
NER model configuration
semantic
SemanticConfig
Semantic enrichment configuration
keyProvider
KeyProvider
Encryption key provider. If not provided, generates a random key.
piiStorageProvider
PIIStorageProvider
Storage provider for session-based PII map persistence
defaultPolicy
AnonymizationPolicy
Default anonymization policy
secrets
SecretsConfig
Secrets/credentials detection configuration
tagFormat
TagFormat
Tag format for PII placeholder tags. Controls the delimiters and keyword.
onValidationWarning
(warnings: Array<{ code: string; message: string }>) => void
Callback for validation warnings. When not provided, warnings are silently ignored — check stats.leakScanPassed in the result instead.
registry
RecognizerRegistry
Custom recognizer registry (uses default if not provided)

Returns

An Anonymizer instance with these methods:
MethodDescription
initialize()Initialize the anonymizer (loads models)
anonymize(text, locale?, policy?)Anonymize text
session(sessionId)Create a session for persistent storage
dispose()Release resources
getRegistry()Get the recognizer registry
getNERModel()Get the NER model instance
isInitializedCheck if initialized

Examples

Basic (Regex Only)

import { createAnonymizer } from 'rehydra';

const anonymizer = createAnonymizer();
await anonymizer.initialize();

const result = await anonymizer.anonymize('Contact: test@example.com');
console.log(result.anonymizedText);
// "Contact: <PII type="EMAIL" id="1"/>"

await anonymizer.dispose();

With NER

const anonymizer = createAnonymizer({
  ner: { 
    mode: 'quantized',
    onStatus: (status) => console.log(status)
  }
});

await anonymizer.initialize();

const result = await anonymizer.anonymize('Hello John Smith!');
// "Hello <PII type="PERSON" id="1"/>!"

With Semantic Enrichment

const anonymizer = createAnonymizer({
  ner: { mode: 'quantized' },
  semantic: { enabled: true }
});

await anonymizer.initialize();

const result = await anonymizer.anonymize('Contact Maria in Berlin');
// "Contact <PII type="PERSON" gender="female" id="1"/> in <PII type="LOCATION" scope="city" id="2"/>"

With Storage

import { 
  createAnonymizer,
  InMemoryKeyProvider,
  SQLitePIIStorageProvider 
} from 'rehydra';

const storage = new SQLitePIIStorageProvider('./pii.db');
await storage.initialize();

const anonymizer = createAnonymizer({
  ner: { mode: 'quantized' },
  keyProvider: new InMemoryKeyProvider(),
  piiStorageProvider: storage,
});

await anonymizer.initialize();

// Use sessions for automatic persistence
const session = anonymizer.session('chat-123');
await session.anonymize('Hello John!');

Irreversible Anonymization

const anonymizer = createAnonymizer({
  mode: 'anonymize',  // No piiMap returned, PII permanently removed
  ner: { mode: 'quantized' },
});

await anonymizer.initialize();

const result = await anonymizer.anonymize('Contact John Smith at john@acme.com');
console.log(result.anonymizedText);
// "Contact <PII type="PERSON" id="1"/> at <PII type="EMAIL" id="1"/>"
console.log(result.piiMap);  // undefined

With Inference Server

const anonymizer = createAnonymizer({
  ner: {
    mode: 'quantized',
    backend: 'inference-server',
    inferenceServerUrl: 'http://gpu-server:8000/predict',
  }
});

With Custom Tag Format

const anonymizer = createAnonymizer({
  tagFormat: { open: '[[', close: ']]' }
});

await anonymizer.initialize();

const result = await anonymizer.anonymize('Contact: test@example.com');
console.log(result.anonymizedText);
// "Contact: [[PII type="EMAIL" id="1"]]"

With Custom Thresholds

import { createAnonymizer, PIIType } from 'rehydra';

const anonymizer = createAnonymizer({
  ner: { 
    mode: 'quantized',
    thresholds: {
      [PIIType.PERSON]: 0.8,
      [PIIType.ORG]: 0.7,
    }
  }
});

With Custom Policy

import { createAnonymizer, PIIType, createDefaultPolicy } from 'rehydra';

const policy = createDefaultPolicy();
policy.enabledTypes = new Set([PIIType.EMAIL, PIIType.PHONE, PIIType.PERSON]);
policy.allowlistTerms = new Set(['Support Team', 'Help Desk']);
policy.enableLeakScan = true;

const anonymizer = createAnonymizer({
  ner: { mode: 'quantized' },
  defaultPolicy: policy,
});

Excluding Countries and Regions

const anonymizer = createAnonymizer({
  ner: { mode: 'quantized' },
  semantic: { enabled: true },
  defaultPolicy: {
    excludeLocationScopes: new Set(['country', 'region']),
  },
});

await anonymizer.initialize();

const result = await anonymizer.anonymize('Meeting in Berlin, Germany.');
// "Meeting in <PII type="LOCATION" scope="city" id="1"/>, Germany."