Trustpilot Node.js API Client
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
-
TypeError: Cannot read properties of undefined (reading 'data')
cause Attempting to access the response directly after upgrading to v4, without using the `.data` property, after the switch from `request-promise-native` to `axios`.fixEnsure that after any `await client.apiRequest()` or `await oauthClient.get()`, you access the actual response body through `.data`. For example, `const result = (await client.apiRequest('/path')).data;` -
ReferenceError: require is not defined in ES module scope
cause Using CommonJS `require()` syntax for the `trustpilot` package in a Node.js project configured as an ES module, or when running newer Node.js versions where ESM is preferred, after the library's conversion to TypeScript in v3.fixSwitch to ES module `import` syntax: `import { TrustpilotApi } from 'trustpilot';`. Ensure your `package.json` has `"type": "module"` or use `.mjs` file extension for ES modules.
Warnings
- breaking Version 4.0.0 replaced `request-promise-native` with `axios` as the underlying HTTP client. This changes the response schema; direct access to the response body now requires `.data`.
- breaking Version 3.0.0 converted the entire library to TypeScript. This introduced type safety and modern JavaScript patterns, but may require changes to existing JavaScript codebases or build processes.
- gotcha The default OAuth grant type is 'password'. For service-to-service communication, it's often more appropriate and secure to use the 'client_credentials' grant type.
Install
-
npm install trustpilot -
yarn add trustpilot -
pnpm add trustpilot
Imports
- TrustpilotApi
const { TrustpilotApi } = require('trustpilot');import { TrustpilotApi } from 'trustpilot';
Quickstart
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);