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 model configuration Show NERConfig properties
Model mode: 'disabled' | 'quantized' | 'standard' | 'custom'
Custom model path (required when mode is 'custom')
Custom vocab path (required when mode is 'custom')
Auto-download model if not present
Confidence thresholds per PII type (0.0-1.0)
Download progress callback
Enable case-insensitive fallback for detecting lowercase names. Runs a second NER pass on title-cased text and merges new detections. Doubles NER inference time but catches names like "tom" that the case-sensitive model would otherwise miss.
Confidence penalty multiplier for case-fallback detections (0.0–1.0). Applied as confidence * caseFallbackPenalty. Lower values are stricter.
Inference backend: 'local' (ONNX on device) or 'inference-server' (remote GPU server)
URL of the inference server (required when backend is 'inference-server')
Inference server request timeout in milliseconds
Semantic enrichment configuration Show SemanticConfig properties
Enable semantic enrichment (gender/scope attributes)
Auto-download semantic data if not present
Download progress callback
Encryption key provider. If not provided, generates a random key.
Storage provider for session-based PII map persistence
Default anonymization policy
Secrets/credentials detection configuration Show SecretsConfig properties
Enable secrets detection (API keys, tokens, connection strings, etc.)
Paths to .env files for literal value redaction
Redact secret values found in .env files
Additional patterns to match secret key names
Minimum value length to consider a secret
Tag format for PII placeholder tags. Controls the delimiters and keyword. Show TagFormat properties
Opening delimiter (e.g., "<" for XML, "[[" for bracket style)
Closing delimiter (e.g., "/>" for XML self-closing, "]]" for bracket style)
Tag keyword. Allows using e.g., "REDACTED" instead of "PII"
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.
Custom recognizer registry (uses default if not provided)
Returns
An Anonymizer instance with these methods:
Method Description 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' ,
}
});
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."