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.
contentful-management.js
Javascript client for Contentful's Content Management API:
Install
In node, using npm:
npm install contentful-management
Note: The next minor version release of dist/contentful-management.min.js
will
be much smaller. Please use a package manager to keep your JS
dependencies up to date and get the newest version right when it's
ready!
Usage
Create an access token for the Content Management API first. Use it as the accessToken
parameter when creating the client.
var contentful = require('contentful-management');
var client = contentful.createClient({
accessToken: 'b4c0n73n7fu1',
secure: true
});
var log = console.log.bind(console);
client.getSpace('foobar').then(log, log);
client.getSpace('foobar').then(function(space) {
return space.getEntries();
}).then(log, log);
For now, please check out the
Content Management API documentation
to learn how the API and the JavaScript client work.
Examples
This library comes with a few example scripts
Cloning a Space
View Source
This clones a Space's complete content model & content.
It's intended to be used to create one-time clones of Spaces,
not for synchronization.
$ example/clone-space.js \
--access-token $CONTENTFUL_ACCESS_TOKEN \
--source-space-id $SOURCE_SPACE_ID \
--destination-space-id $DESTINATION_SPACE_ID
Omit the destination-space-id
parameter to make the script create a
Space. When doing that you might have to specify a
destination-organization-id
parameter if your user is in multiple
organizations.
Cloning a Space's Content Model
View Source
Note: Destination Space has to exist. It won't be created by the Script.
$ example/mirror-content-model.js $CONTENTFUL_ACCESS_TOKEN $SOURCE_SPACE_ID $DEST_SPACE_ID
Migrating Entry fields
Sometimes you need to migrate content from one field to another.
This is a script which migrates all values from one field to another
field, using a specific mapping function if it's provided.
It'll do this for each entry of a specific Content Type in a Space,
going through it slice by slice.
Currently this supports mapping from Text to Symbol.
But it would be very simple to convert e.g. numbers to symbols
or even location strings to locations by geocoding.
PRs are very welcome!
View Source
$ example/migrate-fields.js \
--access-token $CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN \
--space-id $SPACE_ID \
--content-type-id $CONTENT_TYPE_ID \
--source-field-id $SOURCE_FIELD_ID \
--destination-field-id $DESTINATION_FIELD_ID
Unit Tests
Set the following environment variables to valid values:
CONTENTFUL_ACCESS_TOKEN
- a valid access token valueCONTENTFUL_MANAGEMENT_HOSTNAME
- the Contentful host name (without protocol)
Then execute the unit tests:
npm test
License
MIT