What is contentful-management?
The contentful-management npm package is a JavaScript client for the Contentful Content Management API. It allows developers to manage content, assets, and other resources within a Contentful space programmatically.
What are contentful-management's main functionalities?
Create an Entry
This code sample demonstrates how to create a new entry in a Contentful space. You need to provide your access token and space ID, and specify the content type and fields for the new entry.
const contentfulManagement = require('contentful-management');
contentfulManagement.createClient({
accessToken: 'your-access-token'
}).then(client => {
return client.getSpace('your-space-id')
.then(space => space.createEntry('contentType', {
fields: {
title: {
'en-US': 'Hello, World!'
}
}
}))
.then(entry => console.log(entry))
.catch(console.error);
});
Update an Entry
This code sample shows how to update an existing entry in a Contentful space. You need to provide your access token, space ID, and the ID of the entry you want to update. The example updates the title field of the entry.
const contentfulManagement = require('contentful-management');
contentfulManagement.createClient({
accessToken: 'your-access-token'
}).then(client => {
return client.getSpace('your-space-id')
.then(space => space.getEntry('entry-id'))
.then(entry => {
entry.fields.title['en-US'] = 'Updated Title';
return entry.update();
})
.then(entry => console.log(entry))
.catch(console.error);
});
Delete an Entry
This code sample demonstrates how to delete an entry from a Contentful space. You need to provide your access token, space ID, and the ID of the entry you want to delete.
const contentfulManagement = require('contentful-management');
contentfulManagement.createClient({
accessToken: 'your-access-token'
}).then(client => {
return client.getSpace('your-space-id')
.then(space => space.getEntry('entry-id'))
.then(entry => entry.delete())
.then(() => console.log('Entry deleted'))
.catch(console.error);
});
Upload an Asset
This code sample shows how to upload an asset to a Contentful space. You need to provide your access token, space ID, and the path to the file you want to upload. The example also processes and publishes the asset.
const contentfulManagement = require('contentful-management');
const fs = require('fs');
contentfulManagement.createClient({
accessToken: 'your-access-token'
}).then(client => {
return client.getSpace('your-space-id')
.then(space => space.createAssetFromFiles({
fields: {
title: {
'en-US': 'My Asset'
},
file: {
'en-US': {
contentType: 'image/jpeg',
fileName: 'my-asset.jpg',
file: fs.createReadStream('path/to/your/file.jpg')
}
}
}
}))
.then(asset => asset.processForAllLocales())
.then(asset => asset.publish())
.then(asset => console.log(asset))
.catch(console.error);
});
Other packages similar to contentful-management
strapi
Strapi is an open-source headless CMS that provides a robust API and a user-friendly admin panel. Unlike Contentful, which is a SaaS product, Strapi can be self-hosted, giving you more control over your data and infrastructure.
prismic-javascript
Prismic is another headless CMS that offers a content management API. The prismic-javascript package allows you to query and manage content in Prismic. It is similar to Contentful in terms of functionality but offers different pricing and features.
Javascript SDK for Contentful's Content Management API.
About
Contentful is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit & manage content in the cloud and publish it anywhere via a powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations.
Features
- Content management and retrieval through Contentful's Content Management API.
- Built in rate limiting with recovery procedures
- Asset processing helpers
Supported environments
Browsers and Node.js:
- Chrome
- Firefox
- IE11 / Edge
- Safari
- node.js (0.10, iojs-3.x, 4.x, 5.x)
Other browsers should also work, but at the moment we're only running automated tests on the browsers and Node.js versions specified above.
Getting started
In order to get started with the Contentful Management JS SDK 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.
Installation
In node, using npm:
npm install contentful-management
Or, if you'd like to use a standalone built file you can use the following script tag or just download it from npmcdn, under the browser-dist
directory:
<script src="https://npmcdn.com/contentful-management@latest/browser-dist/contentful-management.min.js"></script>
It is not recommended to use the above URL for production.
Using contentful@latest
will always get you the latest version, but you can also specify a specific version number:
<script src="https://npmcdn.com/contentful-management@1.0.0/browser-dist/contentful-management.min.js"></script>
Check the releases page to know which versions are available.
Authentication
To get content from Contentful, an app should authenticate with an with an OAuth bearer token.
If you want to use this SDK for a simple tool or a local app that you won't redistribute or make available to other users, you can get an API key for the Management API at our Authentication page.
If you'd like to create an app which would make use of this SDK but that would be available for other users, where they could authenticate with their own Contentful credentials, make sure to also check out the section about Creating an OAuth Application
Your first request
The following code snippet is the most basic one you can use to get some content from Contentful with this SDK:
var contentful = require('contentful-management')
var client = contentful.createClient({
accessToken: 'YOUR_ACCESS_TOKEN'
})
client.getSpace('spaceId')
.then((space) => {
space.getEntries()
.then((entries) => {
console.log(entries.items)
})
space.getContentType('product')
.then((contentType) => {
contentType.name = 'New Product'
contentType.update()
.then((updatedContentType) => {
console.log('Update was successful')
})
})
})
You can try and change the above example at Tonic.
Documentation/References
To help you get the most out of this SDK, we've prepared reference documentation, tutorials and other examples that will help you learn and understand how to use this library.
Reference documentation
The Contentful's JS SDK reference documents what objects and methods are exposed by this library, what arguments they expect and what kind of data is returned.
Most methods also have examples which show you how to use them.
You can start by looking at the top level contentfulManagement
namespace.
The ContentfulClientAPI
namespace defines the methods at the Client level which allow you to create and get spaces.
The ContentfulSpaceAPI
namespace defines the methods at the Space level which allow you to create and get entries, assets, content types and other possible entities.
The Entry
, Asset
and ContentType
namespaces show you the instance methods you can use on each of these entities, once you retrieve them from the server.
From version 1.0.0 onwards, you can access documentation for a specific version by visiting `https://contentful.github.io/contentful-management.js/contentful-management/<VERSION>`
Contentful JavaScript resources
Check the Contentful for JavaScript page for Tutorials, Demo Apps, and more information on other ways of using JavaScript with Contentful
REST API reference
This library is a wrapper around our Contentful Management REST API. Some more specific details such as search parameters and pagination are better explained on the REST API reference, and you can also get a better understanding of how the requests look under the hood.
Legacy contentful-management.js
For versions prior to 1.0.0, you can access documentation at https://github.com/contentful/contentful-management.js/tree/legacy
Versioning
This project strictly follows Semantic Versioning by use of semantic-release.
This means that new versions are released automatically as fixes, features or breaking changes are released.
You can check the changelog on the releases page.
Migration from contentful-management.js 1.x and older
contentful.js 1.x was a major rewrite, with some API changes. While the base functionality remains the same, some method names have changed, as well as some internal behaviors.
See the migration guide for more information.
Support
If you have a problem with this library, please file an issue here on Github.
If you have other problems with Contentful not related to this library, you can contact Customer Support.
Contributing
See CONTRIBUTING.md
License
MIT