Skip to main content

SDK reference

import { SafetyClient, Policies, isSafe } from '@chidakashi-ai/safety';

Node 18 or newer. Works with ESM and CommonJS, with TypeScript types included.

Credentials​

Both SafetyClient and Policies take the same credentials:

FieldRequiredWhat it is
apiKeyYesYour API key, from the dashboard.
baseUrlNoThe API address. Defaults to SAFETY_BASE_URL if set, otherwise https://api.safety.chidakashi.ai.
timeoutMsNoHow long to wait for each request, in ms. Default 120000.

SafetyClient​

const client = new SafetyClient(creds);

Create one client and reuse it.

checkText(text, policySet, options?)​

Checks text against a policy set. Resolves to { verdict: 'block' | 'pass', reason? }. Only the first 8,000 characters are checked.

checkImage(image, policySet, options?)​

Checks an image against a policy set. image is a file path, a Buffer or a Uint8Array (JPEG, PNG, GIF, BMP or WebP, up to 20 MB). Resolves to { verdict: 'block' | 'pass' | 'unknown', reason? }.

Options​

OptionDefaultWhat it does
showReasonfalseWhen true, the result includes reason, explaining the verdict. Checks can be slower with it on.

Policies​

const policies = new Policies(creds); // or client.policies
MethodWhat it does
save(name, text, { ifNotExists? })Creates or overwrites a policy set, and waits until it's ready (usually under a minute). With ifNotExists: true, fails if the name is taken. Resolves to { savedForTextUse, savedForImageUse }: whether text and image checks can use the set.
get(name)One set: { name, text, createdAt, updatedAt }.
list()Every set in the key's project.
remove(name)Deletes a set.

A name is up to 100 characters. The text is up to 20,000 characters and 40 rules.

isSafe(result)​

true only when the verdict is pass. Use it instead of checking for block, so that unknown isn't treated as safe.

Errors​

Every failure throws a plain Error with a readable message. The SDK doesn't retry. See Errors.

HTTP API​

To call the API without the SDK, send Authorization: Bearer <key> to these routes on https://api.safety.chidakashi.ai:

RouteBody
POST /v1/check/text{ text, policy_set, show_reason }
POST /v1/check/image{ image_base64, policy_set, show_reason }
POST /v1/policy-sets{ name, text, if_not_exists }. Returns { id }; poll GET /v1/policy-set-saves/:id until status is done or failed.
GET /v1/policy-sets, GET /v1/policy-sets/:name, DELETE /v1/policy-sets/:name

A failed request returns an error status with the message as plain text.