Readme · Migration · Advanced · TypeScript · Contributing
JavaScript library for the Contentful Content Delivery API and Content Preview API. It helps you to easily access your content stored in Contentful with your JavaScript applications.
What is Contentful?
Contentful provides content infrastructure for digital teams to power websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enables developers and content creators to ship their products faster.
Table of contents
v6.0.0)For the minimum supported browser versions, refer to the package.json of this library.
To ensure compatibility across various JavaScript environments, this library is built as an ECMAScript Module (ESM) by default, using the "type": "module" declaration in package.json.
We also offer a bundle for the legacy CommonJS (CJS) require syntax, allowing usage in environments that do not support ESM.
Additionally, there is a bundle available for direct usage within browsers.
For more details on the different variants of this library, see Installation.
In order to get started with the Contentful JS library you'll need not only to install it, but also to get credentials which will allow you to have access to your content in Contentful.
npm install contentful
In a modern environment, you can import this library using:
import * as contentful from 'contentful'
Typically, your system will default to our CommonJS export when you use the require syntax:
const contentful = require('contentful')
If this does not work, you can directly require the CJS-compatible code:
const contentful = require('contentful/dist/contentful.cjs')
For browsers, we recommend downloading the library via npm or yarn to ensure 100% availability.
If you'd like to use a standalone built file you can use the following script tag or download it from jsDelivr, under the dist directory:
<script src="https://cdn.jsdelivr.net/npm/contentful@latest/dist/contentful.browser.min.js"></script>
Using contentful@latest will always get you the latest version, but you can also specify a specific version number.
<script src="https://cdn.jsdelivr.net/npm/contentful@9.0.1/dist/contentful.browser.min.js"></script>
The Contentful Delivery library will be accessible via the contentful global variable.
Check the releases page to know which versions are available.
The following code snippet is the most basic one you can use to get some content from Contentful with this library:
import * as contentful from 'contentful'
const client = contentful.createClient({
// This is the space ID. A space is like a project folder in Contentful terms
space: 'developer_bookshelf',
// This is the access token for this space. Normally you get both ID and the token in the Contentful web app
accessToken: '0b7f6x59a0',
})
// This API call will request an entry with the specified ID from the space defined at the top, using a space-specific access token
client
.getEntry('5PeGS2SoZGSa4GuiQsigQu')
.then((entry) => console.log(entry))
.catch((err) => console.log(err))
Check out this JSFiddle version of our Product Catalogue demo app.
This library can also be used with the Preview API. In order to do so, you need to use the Preview API Access token, available on the same page where you get the Delivery API token, and specify the host of the preview API, such as:
import * as contentful from 'contentful'
const client = contentful.createClient({
space: 'developer_bookshelf',
accessToken: 'preview_0b7f6x59a0',
host: 'preview.contentful.com',
})
You can find all available methods of our client in our reference documentation.
To get your own content from Contentful, an app should authenticate with an OAuth bearer token.
You can create API keys using the Contentful web interface. Go to the app, open the space that you want to access (top left corner lists all the spaces), and navigate to the APIs area. Open the API Keys section and create your first token. Done.
Don't forget to also get your Space ID.
For more information, check the Contentful REST API reference on Authentication.
Cursor-based pagination is supported on collection endpoints for entries and assets:
const response = await client.getEntriesWithCursor({ limit: 10 })
console.log(response.items) // Array of items
console.log(response.pages?.next) // Cursor for next page
Use the value from response.pages.next to fetch the next page or response.pages.prev to fetch the previous page.
const nextPageResponse = await client.getEntriesWithCursor({
limit: 10,
pageNext: response.pages?.next,
})
console.log(nextPageResponse.items) // Array of items
console.log(nextPageResponse.pages?.next) // Cursor for next page
console.log(nextPageResponse.pages?.prev) // Cursor for prev page
To help you get the most out of this library, we've prepared all available client configuration options, a reference documentation, tutorials and other examples that will help you learn and understand how to use this library.
The createClient method supports several options you may set to achieve the expected behavior:
contentful.createClient({
...your config here...
})
The configuration options belong to two categories: request config and response config.
| Name | Default | Description |
|---|---|---|
accessToken |
Required. Your CDA access token. | |
space |
Required. Your Space ID. | |
environment |
'master' |
Set the environment that the API key has access to. |
host |
'cdn.contentful.com' |
Set the host used to build the request URI's. |
basePath |
'' |
This path gets appended to the host to allow request urls like https://gateway.example.com/contentful/ for custom gateways/proxies. |
$ claude mcp add contentful.js \
-- python -m otcore.mcp_server <graph>