{"id":17386,"library":"trustpilot","title":"Trustpilot Node.js API Client","description":"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.","status":"active","version":"4.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/trustpilot/node-trustpilot","tags":["javascript","API","Trustpilot","reviews","review","typescript"],"install":[{"cmd":"npm install trustpilot","lang":"bash","label":"npm"},{"cmd":"yarn add trustpilot","lang":"bash","label":"yarn"},{"cmd":"pnpm add trustpilot","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core HTTP client for making API requests, replaced `request-promise-native` in v4.","package":"axios","optional":false}],"imports":[{"note":"The library was converted to TypeScript in v3 and is primarily designed for ESM usage. While CommonJS might technically work with transpilation, direct `require` is not the idiomatic approach for modern Node.js development with this package.","wrong":"const { TrustpilotApi } = require('trustpilot');","symbol":"TrustpilotApi","correct":"import { TrustpilotApi } from 'trustpilot';"}],"quickstart":{"code":"import { TrustpilotApi } from 'trustpilot';\n\nasync function main() {\n  const client = new TrustpilotApi({\n    key: process.env.TRUSTPILOT_API_KEY ?? '',\n    secret: process.env.TRUSTPILOT_SECRET ?? '',\n    username: process.env.TRUSTPILOT_B2B_USERNAME ?? '',\n    password: process.env.TRUSTPILOT_B2B_PASSWORD ?? '',\n    baseUrl: 'https://api.trustpilot.com'\n  });\n\n  try {\n    // Example 1: Basic call using API key authentication (client.apiRequest)\n    console.log('Fetching image resources with API key...');\n    const apiResponse = await client.apiRequest('/v1/resources/images');\n    console.log('API Key Response Data:', apiResponse.data);\n\n    // Example 2: OAuth-authenticated call (client.authenticate() returns axios instance)\n    console.log('\\nAuthenticating with OAuth...');\n    const oauthClient = await new TrustpilotApi({\n      key: process.env.TRUSTPILOT_API_KEY ?? '',\n      secret: process.env.TRUSTPILOT_SECRET ?? '',\n      username: process.env.TRUSTPILOT_B2B_USERNAME ?? '',\n      password: process.env.TRUSTPILOT_B2B_PASSWORD ?? '',\n      baseUrl: 'https://api.trustpilot.com'\n    }).authenticate();\n\n    const businessUnitId = process.env.TRUSTPILOT_BUSINESS_UNIT_ID ?? 'YOUR_BUSINESS_UNIT_ID';\n    if (businessUnitId === 'YOUR_BUSINESS_UNIT_ID') {\n      console.warn('WARNING: Replace YOUR_BUSINESS_UNIT_ID with an actual ID for the OAuth example to work.');\n    }\n    const oauthResponse = await oauthClient.get(`/v1/private/business-units/${businessUnitId}/reviews`);\n    console.log('OAuth Response Data:', oauthResponse.data);\n\n  } catch (error: any) {\n    console.error('An error occurred:', error.response?.data || error.message);\n  }\n}\n\nmain().catch(console.error);\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Update all API call responses to access the actual data payload via the `.data` property (e.g., `(await client.apiRequest(...)).data`).","message":"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`.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Adopt TypeScript for better integration or ensure your JavaScript environment correctly transpiles ESM imports. Avoid `require()` for this package and use `import` statements.","message":"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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"When initializing `TrustpilotApi` for OAuth, explicitly set `grantType: 'client_credentials'` in the constructor options if you are performing server-to-server authentication without a user context.","message":"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.","severity":"gotcha","affected_versions":">=3.4.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure 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;`","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`.","error":"TypeError: Cannot read properties of undefined (reading 'data')"},{"fix":"Switch to ES module `import` syntax: `import { TrustpilotApi } from 'trustpilot';`. Ensure your `package.json` has `\"type\": \"module\"` or use `.mjs` file extension for ES modules.","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.","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null}