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

Signature

function createAnonymizer(config?: AnonymizerConfig): Anonymizer

Parameters

config (optional)

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
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: [email protected]');
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!');

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