Boclips API Client
The Boclips API Client is a JavaScript/TypeScript library designed to provide frontend applications with a structured interface for interacting with the Boclips API. Currently at version 113.5.2, this client likely follows a rapid release cadence, indicated by its high major version number, suggesting frequent updates aligned with backend API evolution. A key differentiator is its robust testing strategy leveraging Pact for contract testing, which generates contracts against a mock HTTP layer and then verifies them against the staging API gateway. This ensures client-backend compatibility without relying solely on brittle end-to-end integration tests. The library also includes a `FakeBoclipsClient` implementation, offering an in-memory test client that simplifies integration testing for consumer applications by removing the need for HTTP mocks. Users are expected to provide their own configured Axios instance, including authentication, to the client.
Common errors
-
TypeError: ApiBoclipsClient.initialize is not a function
cause Attempting to use `require()` for CommonJS import in an ESM context, or vice-versa, or incorrect named import.fixEnsure you are using `import { ApiBoclipsClient } from 'boclips-api-client';` for modern JavaScript/TypeScript applications. Avoid `const ApiBoclipsClient = require('boclips-api-client');`. -
Error: Request failed with status code 401
cause The `AxiosInstance` provided to `ApiBoclipsClient.initialize` did not include valid authentication credentials, or the credentials have expired.fixVerify that your `axiosInstance` is correctly configured with `Authorization` headers (e.g., `Bearer` token) before passing it to `ApiBoclipsClient.initialize`. Ensure your authentication token is valid and refreshed if necessary. -
TypeError: Cannot read properties of undefined (reading 'get') on client.collectionsClient
cause `client` was not successfully initialized, or `collectionsClient` is not available on the client instance.fixEnsure that `await ApiBoclipsClient.initialize(axios, prefix)` completed successfully and returned a valid client object. Check that the `prefix` is correct and the API is accessible. Verify that the sub-client (e.g., `collectionsClient`) actually exists in the current version of the library.
Warnings
- gotcha The `ApiBoclipsClient` requires an `AxiosInstance` to be passed during initialization. It is the consumer's responsibility to configure this Axios instance, including setting up all necessary authentication (e.g., `Authorization` headers, interceptors for token refresh). Incorrect authentication setup will lead to unauthorized API requests.
- breaking Given the high major version (113.x.x), this package likely undergoes frequent updates, often including breaking changes to align with the evolving Boclips API. New major versions can introduce changes in client method signatures, return types, or data models.
- gotcha When developing locally, using `pnpm link` (or `npm link`) to symlink `boclips-api-client` into a consumer application can lead to issues, particularly with duplicate React instances or inconsistent dependency resolutions if the linked client and consumer application have conflicting versions of shared dependencies.
Install
-
npm install boclips-api-client -
yarn add boclips-api-client -
pnpm add boclips-api-client
Imports
- ApiBoclipsClient
const { ApiBoclipsClient } = require('boclips-api-client');import { ApiBoclipsClient } from 'boclips-api-client'; - FakeBoclipsClient
const { FakeBoclipsClient } = require('boclips-api-client');import { FakeBoclipsClient } from 'boclips-api-client'; - collectionsClient
import { collectionsClient } from 'boclips-api-client';const existingCollection = await client.collectionsClient.get('sample-collection-id');
Quickstart
import { ApiBoclipsClient } from 'boclips-api-client';
import axios from 'axios';
const initializeClient = async () => {
// In a real application, replace this with your actual environment variable or configuration.
const apiUrlPrefix = process.env.BOCLIPS_API_URL_PREFIX ?? 'https://api.staging-boclips.com';
// Configure Axios. This is where you'd typically set up authentication (e.g., Bearer tokens).
const axiosInstance = axios.create({
baseURL: apiUrlPrefix,
headers: {
// Example: 'Authorization': `Bearer ${yourAuthToken}`
}
});
try {
const client = await ApiBoclipsClient.initialize(axiosInstance, apiUrlPrefix);
// Example: Query an existing collection
const sampleCollectionId = 'sample-collection-id'; // Replace with a real ID for testing
const existingCollection = await client.collectionsClient.get(sampleCollectionId);
console.log(`Successfully fetched collection: ${existingCollection?.title}`);
return existingCollection;
} catch (error) {
console.error('Failed to initialize BoclipsClient or fetch collection:', error);
throw error;
}
};
// To run this example, ensure you have an environment variable for the API prefix
// and handle authentication in your axios instance.
// initializeClient();