UNPKG

14.3 kBMarkdownView Raw
1 # contentful-management.js
2
3> JavaScript SDK for [Contentful's](https://www.contentful.com) Content Management API.
4
5[![npm](https://img.shields.io/npm/v/contentful-management.svg)](https://www.npmjs.com/package/contentful-management)
6[![Build Status](https://travis-ci.org/contentful/contentful-management.js.svg?branch=master)](https://travis-ci.org/contentful/contentful-management.js)
7[![codecov](https://codecov.io/gh/contentful/contentful-management.js/branch/master/graph/badge.svg)](https://codecov.io/gh/contentful/contentful-management.js)
8[![Dependency Status](https://david-dm.org/contentful/contentful-management.js.svg)](https://david-dm.org/contentful/contentful-management.js)
9[![devDependency Status](https://david-dm.org/contentful/contentful-management.js/dev-status.svg)](https://david-dm.org/contentful/contentful-management.js#info=devDependencies)
10
11[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
12[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
13[![npm downloads](https://img.shields.io/npm/dm/contentful-management.svg)](http://npm-stat.com/charts.html?package=contentful-management)
14[![gzip bundle size](http://img.badgesize.io/https://unpkg.com/contentful-management/dist/contentful-management.browser.min.js?compression=gzip)](https://unpkg.com/contentful-management/dist/contentful-management.browser.min.js)
15
16[Contentful](https://www.contentful.com) provides a content infrastructure for digital teams to power content in 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 enable developers and content creators to ship digital products faster.
17
18## Features
19
20- Content management and retrieval through Contentful's [Content Management API](https://www.contentful.com/developers/docs/references/content-management-api/).
21- Built in rate limiting with recovery procedures
22- Asset processing helpers
23
24## Supported environments
25
26Browsers and Node.js:
27
28- Chrome
29- Firefox
30- Edge
31- Safari
32- node.js (LTS)
33- IE11 (with [legacy version](#legacy-browsers) of the library)
34
35Other browsers should also work, but at the moment we're only running automated tests on the browsers and Node.js versions specified above.
36
37# Getting started
38
39To get started with the Contentful Management JS SDK you'll need to install it, and then get credentials which will allow you to access your content in Contentful.
40
41- [Installation](#installation)
42- [Authentication](#authentication)
43- [Using ES6 import](#using-es6-import)
44- [Your first request](#your-first-request)
45- [Troubleshooting](#troubleshooting)
46- [Documentation/References](#documentationreferences)
47
48## Installation
49
50### Node:
51
52Using [npm](http://npmjs.org):
53
54```sh
55npm install contentful-management
56```
57
58Using [yarn](https://yarnpkg.com/lang/en/):
59
60```sh
61yarn add contentful-management
62```
63
64### Browser:
65
66For browsers, we recommend to download the SDK via npm or yarn to ensure 100% availability.
67
68If you'd like to use a standalone built file you can use the following script tag or download it from [jsDelivr](https://www.jsdelivr.com/package/npm/contentful-management), under the `dist` directory:
69
70```html
71<script src="https://cdn.jsdelivr.net/npm/contentful-management@latest/dist/contentful-management.browser.min.js"></script>
72```
73
74**It's not recommended to use the above URL for production.**
75
76Using `contentful@latest` will always get you the latest version, but you can also specify a specific version number:
77
78```html
79<!-- Avoid using the following url for production. You can not rely on its availability. -->
80<script src="https://cdn.jsdelivr.net/npm/contentful-management@4.0.1/dist/contentful-management.browser.min.js"></script>
81```
82
83The Contentful Management SDK will be accessible via the `contentfulManagement` global variable.
84
85Check the [releases](https://github.com/contentful/contentful-management.js/releases) page to know which versions are available.
86
87### Typings
88
89This library also comes with typings to use with typescript.
90
91### Legacy browsers:
92
93This library also comes with a legacy version to support Internet Explorer 11 and other older browsers. It already contains a polyfill for Promises.
94
95To support legacy browsers in your application, use `contentful-management.legacy.min.js` instead of `contentful-management.browser.min.js`
96
97## Authentication
98
99To get content from Contentful, an app should authenticate with an OAuth bearer token.
100
101If 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](https://www.contentful.com/developers/docs/references/authentication/).
102
103If 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](https://www.contentful.com/developers/docs/references/authentication/#creating-an-oauth-20-application)
104
105## Using ES6 import
106
107You can use the es6 import with the SDK as follow
108
109```js
110// import createClient directly
111import { createClient } from 'contentful-management'
112var client = createClient({
113 // This is the access token for this space. Normally you get the token in the Contentful web app
114 accessToken: 'YOUR_ACCESS_TOKEN',
115})
116//....
117```
118
119OR
120
121```js
122// import everything from contentful
123import * as contentful from 'contentful-management'
124var client = contentful.createClient({
125 // This is the access token for this space. Normally you get the token in the Contentful web app
126 accessToken: 'YOUR_ACCESS_TOKEN',
127})
128// ....
129```
130
131## Your first request
132
133The following code snippet is the most basic one you can use to get content from Contentful with this SDK:
134
135```js
136var contentful = require('contentful-management')
137var client = contentful.createClient({
138 // This is the access token for this space. Normally you get the token in the Contentful web app
139 accessToken: 'YOUR_ACCESS_TOKEN',
140})
141
142// This API call will request a space with the specified ID
143client.getSpace('spaceId').then((space) => {
144 // Now that we have a space, we can get entries from that space
145 space.getEntries().then((entries) => {
146 console.log(entries.items)
147 })
148
149 // let's get a content type
150 space.getContentType('product').then((contentType) => {
151 // and now let's update its name
152 contentType.name = 'New Product'
153 contentType.update().then((updatedContentType) => {
154 console.log('Update was successful')
155 })
156 })
157})
158```
159
160You can try and change the above example at [Tonic](https://tonicdev.com/npm/contentful-management).
161
162## Troubleshooting
163
164- **I can't Install the package via npm** - Check your internet connection - It is called `contentful-management` and not `contenful-management` ¯\\\_(ツ)\_
165- **Can I use the SDK in react native projects** - Yes it is possible
166- **I get the error: Unable to resolve module `http`** - Our SDK is supplied as node and browser version. Most non-node environments, like React Native, act like a browser. To force using of the browser version, you can require it via: `const { createClient } = require('contentful-management/dist/contentful-management.browser.min.js')`
167- **I am not sure what payload to send when creating and entity (Asset/Entity/ContentType etc...)** - Check the Content Management API [docs](https://www.contentful.com/developers/docs/references/content-management-api/) or the examples in the reference [docs](https://contentful.github.io/contentful-management.js) - Feel free to open an issue if you didn't find what you need in the above links
168- 😱 **something is wrong what should I do** - If it is a bug related to the code create a GitHub issue and make sure to remove any credential for your code before sharing it. - If you need to share your credentials, for example you have an issue with your space, please create a support ticket. - Please **do not** share your management token in a GitHub issue
169
170## Documentation/References
171
172To 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.
173
174### Configuration
175
176The `createClient` method supports several options you may set to achieve the expected behavior:
177
178```js
179contentful.createClient({
180 ... your config here ...
181})
182```
183
184#### accessToken (required)
185
186Your CMA access token.
187
188#### host (default: `'api.contentful.com'`)
189
190Set the host used to build the request URI's.
191
192#### hostUpload (default: `'upload.contentful.com'`)
193
194Set the host used to build the upload related request uri's.
195
196#### basePath (default: ``)
197
198This path gets appended to the host to allow request urls like `https://gateway.example.com/contentful/` for custom gateways/proxies.
199
200#### httpAgent (default: `undefined`)
201
202Custom agent to perform HTTP requests. Find further information in the [axios request config documentation](https://github.com/mzabriskie/axios#request-config).
203
204#### httpsAgent (default: `undefined`)
205
206Custom agent to perform HTTPS requests. Find further information in the [axios request config documentation](https://github.com/mzabriskie/axios#request-config).
207
208#### headers (default: `{}`)
209
210Additional headers to attach to the requests. We add/overwrite the following headers:
211
212- Content-Type: `application/vnd.contentful.management.v1+json`
213- X-Contentful-User-Agent: `sdk contentful-management.js/1.2.3; platform node.js/1.2.3; os macOS/1.2.3`
214 (Automatically generated)
215
216#### proxy (default: `undefined`)
217
218Axios proxy configuration. See the [axios request config documentation](https://github.com/mzabriskie/axios#request-config) for further information about the supported values.
219
220#### retryOnError (default: `true`)
221
222By default, this SDK is retrying requests which resulted in a 500 server error and 429 rate limit response. Set this to `false` to disable this behavior.
223
224#### logHandler (default: `function (level, data) {}`)
225
226Errors and warnings will be logged by default to the node or browser console. Pass your own log handler to intercept here and handle errors, warnings and info on your own.
227
228#### requestLogger (default: `function (config) {}`)
229
230Interceptor called on every request. Takes Axios request config as an arg. Default does nothing. Pass your own function to log any desired data.
231
232#### responseLogger (default: `function (response) {}`)
233
234Interceptor called on every response. Takes Axios response object as an arg. Default does nothing. Pass your own function to log any desired data.
235
236### Reference documentation
237
238The [Contentful's JS SDK reference](https://contentful.github.io/contentful-management.js) documents what objects and methods are exposed by this library, what arguments they expect and what kind of data is returned.
239
240Most methods also have examples which show you how to use them.
241
242You can start by looking at the top level `contentfulManagement` namespace.
243
244The `ContentfulClientAPI` namespace defines the methods at the Client level which allow you to create and get spaces.
245
246The `ContentfulSpaceAPI` namespace defines the methods at the Space level which allow you to create and get entries, assets, content types and other possible entities.
247
248The `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.
249
250> 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>`
251
252### Contentful JavaScript resources
253
254Read the [Contentful for JavaScript](https://www.contentful.com/developers/docs/javascript/) page for Tutorials, Demo Apps, and more information on other ways of using JavaScript with Contentful
255
256### REST API reference
257
258This 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](https://www.contentful.com/developers/docs/references/content-management-api/), and you can also get a better understanding of how the requests look under the hood.
259
260### Legacy contentful-management.js
261
262For versions prior to 1.0.0, you can access documentation at [https://github.com/contentful/contentful-management.js/tree/legacy](https://github.com/contentful/contentful.js/tree/legacy)
263
264## Versioning
265
266This project strictly follows [Semantic Versioning](http://semver.org/) by use of [semantic-release](https://github.com/semantic-release/semantic-release).
267
268This means that new versions are released automatically as fixes, features or breaking changes are released.
269
270You can check the changelog on the [releases](https://github.com/contentful/contentful-management.js/releases) page.
271
272## Migration from contentful-management.js 3.x
273
274The bundle for browsers is now called `contentful-management.browser.min.js` to mark it clearly as browser only bundle. If you need to support IE 11 or other old browsers, you may use the `contentful-management.legacy.min.js`. Node will automatically use the `contentful-management.node.min.js` while bundlers like Webpack will resolve to the new ES-modules version of the library.
275
276No changes to the API of the library were made.
277
278## Migration from contentful-management.js 1.x and older
279
280contentful.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.
281
282See the [migration guide](MIGRATION.md) for more information.
283
284## Support
285
286If you have a problem with this library, please file an [issue](https://github.com/contentful/contentful-management.js/issues/new) here on GitHub.
287
288If you have other problems with Contentful not related to this library, you can contact [Customer Support](https://support.contentful.com).
289
290## Contributing
291
292See [CONTRIBUTING.md](CONTRIBUTING.md)
293
294## License
295
296MIT