The Node.js client for the Perspective API, from sloria/perspective-api-client (archived), with the host configurable. Google's Perspective API stops answering after 2026-12-31, and the original package hardcodes Google's address. This one posts the same request to any Perspective-compatible host. The default is Unprofane's drop-in, which answers the same comments:analyze requests with the same JSON; set host to keep using Google's until then.
Maintained by Unprofane. MIT, like the original.
npm install @unprofane/perspective-api-clientOr keep every require('perspective-api-client') as it is and swap the package under it:
npm install perspective-api-client@npm:@unprofane/perspective-api-clientconst Perspective = require('@unprofane/perspective-api-client');
// An Unprofane key (unprofane.com/app/), sent as ?key= like Google's.
const perspective = new Perspective({apiKey: process.env.PERSPECTIVE_API_KEY});
(async () => {
const result = await perspective.analyze('you empty-headed animal food trough wiper!');
console.log(result.attributeScores.TOXICITY.summaryScore.value);
})();| default | https://api.unprofane.com |
new Perspective({apiKey, host}) |
any Perspective-compatible host, such as https://commentanalyzer.googleapis.com until 2026-12-31 |
PERSPECTIVE_API_HOST |
the same, from the environment, when host is not passed |
The client appends /v1alpha1/comments:analyze?key=… to the host, as the original did to Google's.
TOXICITY is requested by default. Pass options.attributes for others:
const result = await perspective.analyze('fools!', {attributes: ['toxicity', 'insult', 'profanity']});Unprofane serves TOXICITY, SEVERE_TOXICITY, IDENTITY_ATTACK, INSULT, PROFANITY, THREAT, SEXUALLY_EXPLICIT and FLIRTATION. It refuses the other New York Times attributes and the bridging ones (SPAM, UNSUBSTANTIAL, COMPASSION_EXPERIMENTAL and others) with the 400 Google gives a name it does not know, or drops them when the request sets dropUnsupportedAttributes: true.
const result = await perspective.analyze({
comment: {text: 'hooray for tests'},
requestedAttributes: {TOXICITY: {}, INSULT: {}},
languages: ['en'],
});attributes: the attributes to request, as an array of names or an object.doNotStore: defaulttrue.stripHTML: strip HTML tags from the text, defaulttrue.truncate: cut text over 20,480 characters instead of refusing it, defaultfalse.validate: refuse an empty or over-long text before sending, defaulttrue.
ResponseError (from the API, with error.response.status and error.response.data), TextEmptyError and TextTooLongError, all subclasses of PerspectiveAPIClientError, as before.
- The host is configurable (
host,PERSPECTIVE_API_HOST), Unprofane's by default. - Requests go through Node's built-in
fetch(Node 18 or later); axios and lodash are gone.error.responsekeeps axios'sstatus,statusText,headersanddata. - An invalid text is refused without sending a request.
MIT. Copyright Steven Loria; changes since 3.1.0 copyright Unprofane.