Azure AI Content Safety client library for JavaScript - version 1.0.0-beta.1

This package contains an isomorphic SDK (runs both in Node.js and in browsers) for analyzing harmful content, evaluating Agent Control Specification policies, detecting content provenance, and managing text blocklists.

Key links:

This package has not yet been published to npm. The npm package page and hosted API reference will be available after the first release.

Getting started

Currently supported environments

See our support policy for more details.

Prerequisites

Install the @azure/ai-content-safety package

After the first beta release, install the Azure AI Content Safety client library for JavaScript with npm:

npm install @azure/ai-content-safety@next

Create and authenticate clients

To create a client, you will need the endpoint of your Azure AI Content Safety resource and a credential. You can find the endpoint in the Azure Portal.

You can authenticate with Microsoft Entra ID using a credential from the @azure/identity library or an existing Microsoft Entra token.

To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please install the @azure/identity package:

npm install @azure/identity

You will also need to register a new Microsoft Entra application and grant access to Azure AI Content Safety by assigning a suitable role to your service principal (note: roles such as "Owner" will not grant the necessary permissions).

For more information about how to create a Microsoft Entra application check out this guide.

Using Node.js and Node-like environments, you can use the DefaultAzureCredential class to authenticate the client.

import { DefaultAzureCredential } from "@azure/identity";
import {
  ContentSafetyClient,
  ContentProvenanceClient,
  BlocklistClient,
} from "@azure/ai-content-safety";

const credential = new DefaultAzureCredential();
const contentSafetyClient = new ContentSafetyClient("<endpoint>", credential);
const contentProvenanceClient = new ContentProvenanceClient("<endpoint>", credential);
const blocklistClient = new BlocklistClient("<endpoint>", credential);

For browser environments, use the InteractiveBrowserCredential from the @azure/identity package to authenticate.

import { InteractiveBrowserCredential } from "@azure/identity";
import { ContentSafetyClient } from "@azure/ai-content-safety";

const credential = new InteractiveBrowserCredential({
  tenantId: "<YOUR_TENANT_ID>",
  clientId: "<YOUR_CLIENT_ID>",
});
const client = new ContentSafetyClient("<endpoint>", credential);

JavaScript Bundle

To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.

Key concepts

ContentSafetyClient

ContentSafetyClient analyzes text and images, detects protected material and prompt injection, and evaluates content against an Agent Control Specification policy.

import { ContentSafetyClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ContentSafetyClient("<endpoint>", new DefaultAzureCredential());
async function moderateInput(): Promise<void> {
  const result = await client.unifiedModerate({
    policyId: "<policy-id>",
    source: "input",
    content: "Text to evaluate",
  });
  console.log(result.verdict);
}
void moderateInput;

ContentProvenanceClient

ContentProvenanceClient starts and monitors long-running detection operations for Microsoft-issued C2PA and watermark signals.

import { ContentProvenanceClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ContentProvenanceClient("<endpoint>", new DefaultAzureCredential());
async function detectProvenance(): Promise<void> {
  const poller = client.detect({ content: { uri: "<media-blob-uri>" } });
  const result = await poller.pollUntilDone();
  console.log(result.outcome);
}
void detectProvenance;

Serialized poller state can be restored in another process:

import { ContentProvenanceClient, restorePoller } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ContentProvenanceClient("<endpoint>", new DefaultAzureCredential());
async function resumeDetection(serializedState: string): Promise<void> {
  const poller = restorePoller(client, serializedState, client.detect.bind(client));
  const result = await poller.pollUntilDone();
  console.log(result.outcome);
}
void resumeDetection;

BlocklistClient

BlocklistClient creates, updates, lists, and deletes text blocklists and their items.

import { BlocklistClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new BlocklistClient("<endpoint>", new DefaultAzureCredential());
async function updateBlocklist(): Promise<void> {
  const result = await client.createOrUpdateTextBlocklist("<blocklist-name>", {
    description: "Blocklist description",
  });
  console.log(result.blocklistName);
}
void updateBlocklist;

Use byPage({ maxPageSize }) to request a maximum page size when listing blocklist items:

import { BlocklistClient } from "@azure/ai-content-safety";
import { DefaultAzureCredential } from "@azure/identity";

const client = new BlocklistClient("<endpoint>", new DefaultAzureCredential());
async function listBlocklistItemPages(): Promise<void> {
  const pages = client.listTextBlocklistItems("<blocklist-name>").byPage({ maxPageSize: 20 });
  for await (const page of pages) {
    for (const item of page) {
      console.log(item.blocklistItemId, item.text);
    }
  }
}
void listBlocklistItemPages;

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.