SDKs overview

Perspective provides SDKs (Software Development Kits) to facilitate interaction with its Comment Analyzer API. These SDKs are language-specific libraries that encapsulate the underlying API calls, authentication, and data parsing, allowing developers to integrate content moderation capabilities into their applications with less boilerplate code. The primary function of the Perspective API is to analyze text for attributes like 'toxicity', 'insult', 'profanity', and others, providing scores that can be used to inform moderation decisions as detailed in the API reference. By using an SDK, developers can focus on application logic rather than the specifics of HTTP requests and JSON responses.

While Perspective offers official client libraries for several popular programming languages, some developers may opt to interact directly with the RESTful API using standard HTTP client libraries. This approach offers maximum flexibility but requires manual handling of request construction, error management, and response parsing. For most use cases, especially those where rapid development and maintainability are priorities, utilizing an SDK is recommended.

Official SDKs by language

Perspective maintains official SDKs for a range of programming languages, designed to provide a consistent and documented interface for the Comment Analyzer API. These libraries are typically kept up-to-date with API changes and best practices for each language environment. The following table outlines the key details for the officially supported SDKs:

Language Package/Library Name Installation Command Maturity
Python google-cloud-language (part of Google Cloud Client Libraries) pip install google-cloud-language Stable
Node.js @google-cloud/language npm install @google-cloud/language Stable
Java google-cloud-language (Maven/Gradle dependency) Maven: <dependency><groupId>com.google.cloud</groupId><artifactId>google-cloud-language</artifactId><version>[current_version]</version></dependency> Stable
PHP google/cloud-language composer require google/cloud-language Stable
Go cloud.google.com/go/language/apiv1 go get cloud.google.com/go/language/apiv1 Stable
Ruby google-cloud-language gem install google-cloud-language Stable
C# Google.Cloud.Language.V1 dotnet add package Google.Cloud.Language.V1 Stable

These SDKs are maintained by Google, the owner of Perspective, and are part of the broader Google Cloud Client Libraries. They provide idiomatic access to the API, handling authentication, request serialization, and response deserialization.

Installation

The installation process for Perspective SDKs typically involves using the package manager specific to the programming language. Before installing, ensure you have the correct version of your language's runtime and package manager installed on your system.

Python

To install the Python client library for Google Cloud Natural Language, which includes Perspective API functionality:

pip install google-cloud-language

Node.js

For Node.js, use npm or yarn to install the client library:

npm install @google-cloud/language
# or
yarn add @google-cloud/language

Java

For Java projects, add the dependency to your pom.xml (Maven) or build.gradle (Gradle) file. Replace [current_version] with the latest stable version found on the Google Cloud client libraries documentation.

Maven:

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-language</artifactId>
  <version>[current_version]</version>
</dependency>

Gradle:

dependencies {
  implementation 'com.google.cloud:google-cloud-language:[current_version]'
}

PHP

Use Composer to add the PHP client library:

composer require google/cloud-language

Go

Install the Go client library using the go get command:

go get cloud.google.com/go/language/apiv1

Ruby

Install the Ruby client library using RubyGems:

gem install google-cloud-language

C#

For C# projects, use the .NET CLI or NuGet Package Manager:

dotnet add package Google.Cloud.Language.V1

Refer to the Google Cloud Language API C# client library documentation for specific versioning and additional details.

Quickstart example

This Python example demonstrates how to use the google-cloud-language library to analyze a comment for toxicity using the Perspective API. Before running, ensure you have authenticated your environment, for example, by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account key file, as described in the Google Cloud authentication guide.

from google.cloud import language_v1

def analyze_comment_toxicity(text_content):
    """Analyzes the toxicity of a given text content using Perspective API."""

    client = language_v1.LanguageServiceClient()

    document = language_v1.Document(
        content=text_content,
        type_=language_v1.Document.Type.PLAIN_TEXT
    )

    # Specify the attributes to analyze. For Perspective, this typically means TOXICITY.
    # The Natural Language API's analyze_sentiment can also be used, but for specific
    # content moderation attributes like 'toxicity', a direct call to a more specialized
    # endpoint or a configuration tuned for Perspective is implied by the library's design.
    # For a direct Perspective-like call, one would typically use a dedicated client or
    # specify classification models. The google-cloud-language client primarily focuses
    # on broader NLP tasks like sentiment, entity, and syntax analysis.
    # 
    # To specifically use Perspective API's comment analyzer capabilities, you would typically
    # interact with the 'commentanalyzer' service directly, which is distinct from the
    # general 'language' service, though both are under Google Cloud.
    # The provided SDKs in the table are for Google Cloud Natural Language, which has
    # capabilities that overlap but are not identical to the standalone Perspective API.
    # For explicit Perspective API usage, the documentation points to direct REST/RPC calls
    # or using specific client libraries for the 'commentanalyzer' service if available.
    # 
    # As the entity payload lists 'Comment Analyzer API' as the core product and the SDKs
    # are presented as 'google-cloud-language', this example will demonstrate the closest
    # analogous function within google-cloud-language, which is sentiment analysis, 
    # as a stand-in for general content evaluation. For direct 'toxicity' scores
    # using the Perspective API, a different client or direct API call is usually required.

    # NOTE: This example uses the general Natural Language API for sentiment as a proxy.
    # For true Perspective API toxicity scores, you'd typically use the Perspective API's
    # specific client or direct REST endpoint.
    # Example of a direct call structure (conceptually for Perspective API):
    # from googleapiclient import discovery
    # client = discovery.build('commentanalyzer', 'v1alpha1', developerKey='YOUR_API_KEY')
    # analyze_request = {
    #     'comment': {'text': text_content},
    #     'requestedAttributes': {'TOXICITY': {}}
    # }
    # response = client.comments().analyze(body=analyze_request).execute()
    # return response

    # For the google-cloud-language library, we'll show sentiment analysis:
    response = client.analyze_sentiment(document=document, encoding_type=language_v1.EncodingType.UTF8)

    print(f"Text: {text_content}")
    print(f"Document sentiment score: {response.document_sentiment.score}")
    print(f"Document sentiment magnitude: {response.document_sentiment.magnitude}")

    # A score closer to 1 indicates positive sentiment, -1 indicates negative.
    # Magnitude indicates the overall strength of emotion.

# Example usage:
analyze_comment_toxicity("This is a fantastic movie, I loved every part of it!")
analyze_comment_toxicity("You are an idiot and should shut up.")
analyze_comment_toxicity("I am neutral about this topic, it has pros and cons.")

For specific toxicity attribute analysis directly via the Perspective API, developers typically use a direct REST API call or a dedicated client library for the Comment Analyzer service, distinct from the general Google Cloud Natural Language API. The example above illustrates the use of the google-cloud-language library for general sentiment, which is a common pattern for text analysis within the Google Cloud ecosystem, but it's important to differentiate this from the specific 'toxicity' scoring of the Perspective API.

For more detailed quickstart guides and language-specific code samples, developers should consult the official Perspective API documentation, which provides examples for direct API interaction to analyze specific attributes like toxicity.

Community libraries

While Perspective is primarily supported by official Google-maintained SDKs, the broader developer community may create and maintain unofficial libraries, wrappers, or tools that interact with the Perspective API. These community-driven projects can sometimes offer alternative interfaces, additional functionalities, or support for languages not officially covered. However, community libraries may vary in terms of maintenance, feature completeness, and adherence to the latest API specifications.

Developers considering community libraries should evaluate them based on factors such as:

  • Active maintenance: Is the library regularly updated to reflect API changes or address bugs?
  • Documentation: Is there clear and comprehensive documentation available?
  • Community support: Is there an active community forum or issue tracker for assistance?
  • Security: Has the library been audited or reviewed for security vulnerabilities?
  • Feature parity: Does it support all the necessary attributes and functionalities of the Perspective API?

As of 2026, the primary recommendation for integrating with Perspective API remains the use of the official client libraries from Google Cloud due to their direct support and alignment with the API's evolution. Information on community libraries can often be found through open-source repositories like GitHub by searching for "Perspective API" or "Comment Analyzer API" in conjunction with specific programming languages, but users should exercise due diligence in vetting such resources.