Trustpilot Node.js API Client

4.0.1 · active · verified Wed Apr 22

The `trustpilot` package is an official Node.js HTTP client for interacting with the Trustpilot APIs. Currently stable at version 4.0.1, released in April 2024, it provides a structured and type-safe way to access Trustpilot's data, including business reviews and company profile information. Built with TypeScript and leveraging `axios` for HTTP requests, it offers a modern promise-based interface with async/await support, targeting Node.js `v20.x`. Key differentiators include built-in support for both API key and OAuth-based authentication, with the flexibility to specify different OAuth grant types (e.g., `client_credentials` for service-to-service communication). The release cadence is generally infrequent, with major versions (like v3 and v4) typically driven by significant architectural changes, such as the underlying HTTP client library. It abstracts away much of the complexity of direct API interactions, providing a client instance with sane defaults.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing the Trustpilot API client, making a basic API key authenticated request, and an OAuth authenticated request to fetch data, including handling potential errors.

import { TrustpilotApi } from 'trustpilot';

async function main() {
  const client = new TrustpilotApi({
    key: process.env.TRUSTPILOT_API_KEY ?? '',
    secret: process.env.TRUSTPILOT_SECRET ?? '',
    username: process.env.TRUSTPILOT_B2B_USERNAME ?? '',
    password: process.env.TRUSTPILOT_B2B_PASSWORD ?? '',
    baseUrl: 'https://api.trustpilot.com'
  });

  try {
    // Example 1: Basic call using API key authentication (client.apiRequest)
    console.log('Fetching image resources with API key...');
    const apiResponse = await client.apiRequest('/v1/resources/images');
    console.log('API Key Response Data:', apiResponse.data);

    // Example 2: OAuth-authenticated call (client.authenticate() returns axios instance)
    console.log('\nAuthenticating with OAuth...');
    const oauthClient = await new TrustpilotApi({
      key: process.env.TRUSTPILOT_API_KEY ?? '',
      secret: process.env.TRUSTPILOT_SECRET ?? '',
      username: process.env.TRUSTPILOT_B2B_USERNAME ?? '',
      password: process.env.TRUSTPILOT_B2B_PASSWORD ?? '',
      baseUrl: 'https://api.trustpilot.com'
    }).authenticate();

    const businessUnitId = process.env.TRUSTPILOT_BUSINESS_UNIT_ID ?? 'YOUR_BUSINESS_UNIT_ID';
    if (businessUnitId === 'YOUR_BUSINESS_UNIT_ID') {
      console.warn('WARNING: Replace YOUR_BUSINESS_UNIT_ID with an actual ID for the OAuth example to work.');
    }
    const oauthResponse = await oauthClient.get(`/v1/private/business-units/${businessUnitId}/reviews`);
    console.log('OAuth Response Data:', oauthResponse.data);

  } catch (error: any) {
    console.error('An error occurred:', error.response?.data || error.message);
  }
}

main().catch(console.error);

view raw JSON →