Azure AI Content Safety — TypeScript SDK Quick Reference
Condensed from azure-ai-contentsafety-ts. Full patterns (blocklist CRUD, image moderation, severity thresholds) in the azure-ai-contentsafety-ts plugin skill if installed.
Install
bash
npm install @azure-rest/ai-content-safety @azure/identity @azure/core-authQuick Start
typescript
import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety";
import { AzureKeyCredential } from "@azure/core-auth";
const client = ContentSafetyClient(endpoint, new AzureKeyCredential(key));Non-Obvious Patterns
- REST client —
ContentSafetyClientis a function, not a class - Text:
client.path("/text:analyze").post(\{ body: \{ text, categories: [...] \} \}) - Image:
client.path("/image:analyze").post(\{ body: \{ image: \{ content: base64 \} \} \}) - Blocklist create:
.path("/text/blocklists/\{blocklistName\}", name).patch(\{...\}) - API key import:
AzureKeyCredentialfrom@azure/core-auth(not@azure/identity)
Best Practices
- Always use
isUnexpected()— type guard for error handling - Set appropriate thresholds — different categories may need different severity levels
- Use blocklists for domain-specific terms to supplement AI detection
- Log moderation decisions — keep audit trail for compliance
- Handle edge cases — empty text, very long text, unsupported image formats