Client for retrieving content using the read-only Contensis Delivery API
npm install contensis-delivery-apidependencies using npm, or your preferred Node.js package manager.
shell
npm install --save contensis-delivery-api
`
Usage
Follow the examples with the preferred coding style that best suits your project
$3
All of the delivery methods are available under the client instance returned by Client.create(...)
Use the provided intellisense to guide you where it is available
`typescript
// Using TypeScript, or ES Module syntax
import { Client } from "contensis-delivery-api";
const client = Client.create({
rootUrl: "https://my-cms.com",
accessToken: "DELIVERY_API_ACCESS_TOKEN",
projectId: "website",
language: "en-GB",
versionStatus: "published",
});
`
`cjs
// Using Common JS syntax
const Client = require("contensis-delivery-api").Client;
const client = Client.create({
rootUrl: "https://my-cms.com",
accessToken: "DELIVERY_API_ACCESS_TOKEN",
projectId: "website",
language: "en-GB",
versionStatus: "published",
});
`
$3
Reusing the client instance we created above
`typescript
// Using TypeScript, or ES Module syntax
const loadMovies = async () => {
const movieList = await client.entries.list({
contentTypeId: "movie",
pageOptions: { pageIndex: 0, pageSize: 10 },
order: ["-releaseDate"],
});
for (const movie of movieList.items) {
console.log(movie);
}
};
loadMovies();
`
$3
Keep a common client configuration and create the delivery client when you need it
`typescript
// Using async / await syntax (avoiding callbacks)
// Import the additional "Config" type when using TypeScript
import { Client, Config } from "contensis-delivery-api";
// Remove ": Config" annotation when NOT using TypeScript
const contensisConfig: Config = {
rootUrl: "https://my-cms.com",
accessToken: "DELIVERY_API_ACCESS_TOKEN",
projectId: "website",
language: "en-GB",
versionStatus: "published",
};
const loadMovies = async () => {
const client = Client.create(contensisConfig);
const movieList = await client.entries.list({
contentTypeId: "movie",
pageOptions: { pageIndex: 0, pageSize: 10 },
order: ["-releaseDate"],
});
for (const movie of movieList.items) {
console.log(movie);
}
};
loadMovies();
`
`typescript
// Using Promises and callbacks
import { Client } from "contensis-delivery-api";
const contensisConfig = {
rootUrl: "https://my-cms.com",
accessToken: "DELIVERY_API_ACCESS_TOKEN",
projectId: "website",
language: "en-GB",
versionStatus: "published",
};
function loadMovies() {
const client = Client.create(contensisConfig);
client.entries
.list({
contentTypeId: "movie",
pageOptions: { pageIndex: 0, pageSize: 10 },
order: ["-releaseDate"],
})
.then((movieList) => {
console.log(movieList.items);
});
}
loadMovies();
`
_For more examples refer to our documentation on contensis.com_
Fetch API
This library uses the fetch API and relies on it being available at runtime.
$3
In modern browsers fetch is available natively, a polyfill is provided for older browsers.
The build of the library that targets browsers can be found in the bundle folder.
$3
When using this library in Node.js the fetch API is already polyfilled with cross-fetch package (which uses node-fetch when used with a Node.js runtime).
Node.js version 16 introduced experimental support for a native global fetch API and shipped as standard with Node.js versions 18 and 20. The global fetch support in Node.js version 21 and greater is marked as stable.
$3
You can override the built-in fetch API by providing your own fetchFn method when creating the Client.
Method calls that require fetch invoked from this client instance will be made using your chosen API.
`typescript
import { Client } from "contensis-delivery-api";
import enterpriseFetch from "enterprise-fetch";
const client = Client.create({
rootUrl: "https://my-cms.com",
accessToken: "DELIVERY_API_ACCESS_TOKEN",
projectId: "website",
language: "en-GB",
versionStatus: "published",
fetchFn: enterpriseFetch,
});
``