Moderate Image

To moderate image, you can use the image.moderate method. This method takes in a image string and returns a Promise that resolves to a ImageModerationResponse object.

Image from URL

index.ts
const contentMod = new ContentMod({
  publicKey: "<YOUR_PUBLIC_KEY>",
  secretKey: "<YOUR_SECRET_KEY>",
});

// Simple image moderation with a link
const response = await contentMod.image.moderate(
  "https://example.com/image.jpg"
);

// Image moderation with additional metadata to be saved with the moderation request
const response2 = await contentMod.image.moderate(
  "https://example.com/image.jpg",
  {
    meta: {
      userId: "1234",
    },
  }
);

// Optionally you can pass in an webhook callback url if you want to be notified when the moderation request is completed
const response3 = await contentMod.image.moderate(
  "https://example.com/image.jpg",
  {
    meta: {
      userId: "1234",
    },
    callbackUrl: "https://example.com/webhook",
  }
);

Webhook Callback

If you pass in a webhook callback url, the response will only be an object with the id property.

Image from Base64 encoded string

index.ts
import { ContentMod } from "@contentmod/sdk";
import fs from "fs";
const contentMod = new ContentMod({
  publicKey: "<YOUR_PUBLIC_KEY>",
  secretKey: "<YOUR_SECRET_KEY>",
});

// Read file from disk
const img = fs.readFileSync("./pingu.jpeg");
// Convert to base64 encoded string
const base64 = img.toString("base64");
// Submit image moderation
const imgResponse = await contentMod.image.moderate(base64, {
  meta: {
    userId: "1234",
  },
});

Properties

image
string
required

Image to moderate

Example: https://example.com/image.jpg

options
object

An object of options for the moderation request

meta
object

Any additional metadata you want to include with the moderation request that will be saved.

Example:{"userId": "1234"}

callbackUrl
string

A webhook callback url that will be called when the moderation request is completed.

Example: https://example.com/webhook

defer
boolean

Whether to defer the moderation request. If this value is true the request will be queued and you will receive the response later in a webhook that you have set up. Or you can look it up later using the id returned.

Example: true

Response

The ImageModerationResponse object contains the following properties:

id
string
required

The ID of the moderation request.

Example: 27fbdc0b-b295-46ce-93f5-81b2fc08a381

isSafe
boolean
required

Whether the image is considered safe.

Example: true

confidence
number
required

Confidence of the safety of the image from 1-100.

Example: 90

sentiment
string
required

Sentiment of the image, either negative, neutral or positive.

available sentiment values are:

  • negative
  • neutral (default)
  • positive

Example: positive

sentimentScore
number
required

Sentiment score from 1-100 (negative, neutral, positive).

Example: 90

riskScores
object
required

Overall risk score from 1-100.

Example: 90

topics
array
required

General topics the image depicts, lowercase and no punctuation.

Example: ["politics", "sports"]

nsfwCategories
array
required

An array of the categories of the image. The available categories are:

  • adult_content
  • suggestive_imagery
  • strong_language
  • violence_gore
  • horror_disturbing
  • alcohol
  • tobacco
  • substance_use
  • gambling
  • dating_relationship
  • medical_procedures”
  • crude_humor
  • political_content

Example: [{"category": "sexual", "severity": 90}]

summary
object
required

Summary of the image.

totalFlags
number
required

Total flags.

Example: 1

contentRating
string
required

Content rating of the image (e.g. G, PG, PG-13, R, NC-17).

Example: G

language
string
required

The language code of the text, eg. en, fr, de.

Example: en

Example: {"profanity": true, "totalFlags": 1, "contentRating": "G", "language": "en"}

suggestedActions
object
required

Suggested actions for the image.

reject
boolean
required

Whether to reject the image.

Example: true

review
boolean
required

Whether to flag the image.

Example: true

Example: {"reject": true, "review": true}

request
object
required

The request object.

Example: {"requestId": "27fbdc0b-b295-46ce-93f5-81b2fc08a381", "timestamp": "2023-04-05T12:00:00.000Z"}

meta
object
required

The metadata of the request object that the user provided.

Example: {"userId": "12345"}

hash
string
required

The SHA-256 hash of the image. Used for content comparison purposes.

Example: 93a08bd10cd367bf83f12ba6590fcaef2f8deec8a47ce4ce88a15c7dda325ffa