Perspective API
Tags: javascript, tutorial, node, googleapis
Published: 2022-03-21 20:22:04

## What is Perspective?
**[Perspective](https://www.perspectiveapi.com/)** is a **free API that uses machine learning to identify "toxic" contents**, making it easier to organize better online conversations.

By scoring a sentence based on the perceived impact the text may have in a conversation, developers and editors can use this score to provide feedback to commenters, help moderators review comments more easily, or help readers filter out "toxic" language. Perspective provides scores for several attributes, such as:

- **Severe toxicity**
- Insults
- Injuries
- Identity attacks
- The **threats**
- And the sexually explicit

![Perspective API solutions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptby26e3vzs7g2d5yovy.gif)

So we have a very good solution to protect ourselves from threatening comments and so on to avoid broadcasting bad content whether it is in a comment, in a forum or in a live chat. There are multitudes of functionality to realize with this API.

**Perspective API** is a very good way to filter out insults and phrases that are **toxic**.

> Toxicity online poses a serious challenge for platforms and publishers. Online abuse and harassment silence important voices in conversation, forcing already marginalized people offline.


## An example of code

The example is under JavaScript, but can totally work with any other language.

[See the documentation dev](https://developers.perspectiveapi.com/s/) 



```javascript
// Since the official documentation and slightly modified 
// https://developers.perspectiveapi.com/s/docs-sample-requests

const {google} = require('googleapis')
require('dotenv').config()

CONTENT = "You're really crap at this game"

// Create an .env file to recover GOOGLE_API_KEY.
API_KEY = process.env.GOOGLE_API_KEY
DISCOVERY_URL =
  'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1'

google.discoverAPI(DISCOVERY_URL).then(client => {
  const analyzeRequest = {
    comment: {
      text: CONTENT,
    },
    requestedAttributes: {
      TOXICITY: {},
    },
  }

  console.info(`Input Text : ${analyzeRequest.comment.text}`)

  client.comments.analyze(
    {
      key: API_KEY,
      resource: analyzeRequest,
    },
    async (err, response) => {
      if (err) throw err
      let ScoreValue = response.data.attributeScores.TOXICITY.summaryScore.value
      await console.log(`TOXICITY Score : ${ScoreValue}`)
      console.table(JSON.stringify(response.data, null, 2))
    })
}).catch(err => {
  throw err
})
```

It's so easy to set up, just install `googleapis` and `dotenv` and get your Perspective API key and test the code. 🎉

```bash
yarn add googleapis dotenv
```

Or if you prefer NPM : 

```bash
npm i googleapis dotenv
```

____

I have already made a project that is **Open Source** on GitHub called **No Toxic Discussions**. It's an _GitHub Action_ that identifies the message in the discussion space and checks if its content is toxic or not.

{% github https://github.com/thomasbnt/NoToxicDiscussions  no-readme %}

[![No Toxic Discussions, a GitHub Action to detect toxicity in discussions area.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9u7cit4h5tnr3qpdxvi.png)](https://github.com/thomasbnt/NoToxicDiscussions)

You also have a DEV post about this Action. [Click here to read him](https://dev.to/thomasbnt/no-toxic-discussions-38h3).

_____

## Credits

Some texts have been copied from the website, as for the banner the logo "Perspective API" comes from Jigsaw of Google Inc. The source code has been taken from the example on their website and modified so that the result is visible. It comes from the modification I made for the No Toxic Discussions project.


|☕|Check my [Twitter account](https://twitter.com/thomasbnt_). You can see many projects and updates. You can also [support me on Buy Me a Coffee, Stripe or GitHub Sponsors](https://thomasbnt.dev/donate). Thanks for read my post ! 🤩|
|---|---|