ContentfulClientAPI

ContentfulClientAPI

Source:
See:
Contentful Delivery API Client. Contains methods which allow access to the different kinds of entities present in Contentful (Entries, Assets, etc).

Type Definitions

ClientAPI

Source:
Properties:
Name Type Description
getSpace function
getContentType function
getContentTypes function
getEntry function
getEntries function
getAsset function
getAssets function
parseEntries function
sync function
Type:
  • Object

Methods

(static) getAsset(id, queryopt) → {Promise.<Entities.Asset>}

Source:
Gets an Asset
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.getAsset('<asset_id>')
.then((asset) => console.log(asset))
.catch(console.error)
Parameters:
Name Type Attributes Description
id string
query Object <optional>
Object with search parameters. In this method it's only useful for `locale`.
Returns:
Promise for an Asset
Type
Promise.<Entities.Asset>

(static) getAssets(queryopt) → {Promise.<Entities.AssetCollection>}

Source:
Gets a collection of Assets
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.getAssets()
.then((response) => console.log(response.items))
.catch(console.error)
Parameters:
Name Type Attributes Description
query Object <optional>
Object with search parameters. Check the JS SDK tutorial and the REST API reference for more details.
Returns:
Promise for a collection of Assets
Type
Promise.<Entities.AssetCollection>

(static) getContentType(id) → {Promise.<Entities.ContentType>}

Source:
Gets a Content Type
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.getContentType('<content_type_id>')
.then((contentType) => console.log(contentType))
.catch(console.error)
Parameters:
Name Type Description
id string
Returns:
Promise for a Content Type
Type
Promise.<Entities.ContentType>

(static) getContentTypes(queryopt) → {Promise.<Entities.ContentTypeCollection>}

Source:
Gets a collection of Content Types
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.getContentTypes()
.then((response) => console.log(response.items))
.catch(console.error)
Parameters:
Name Type Attributes Description
query Object <optional>
Object with search parameters. Check the JS SDK tutorial and the REST API reference for more details.
Returns:
Promise for a collection of Content Types
Type
Promise.<Entities.ContentTypeCollection>

(static) getEntries(queryopt) → {Promise.<Entities.EntryCollection>}

Source:
Gets a collection of Entries
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.getEntries()
.then((response) => console.log(response.items))
.catch(console.error)
Parameters:
Name Type Attributes Description
query Object <optional>
Object with search parameters. Check the JS SDK tutorial and the REST API reference for more details.
Returns:
Promise for a collection of Entries
Type
Promise.<Entities.EntryCollection>

(static) getEntry(id, queryopt) → {Promise.<Entities.Entry>}

Source:
Gets an Entry
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.getEntry('<entry_id>')
.then((entry) => console.log(entry))
.catch(console.error)
Parameters:
Name Type Attributes Description
id string
query Object <optional>
Object with search parameters. In this method it's only useful for `locale`.
Returns:
Promise for an Entry
Type
Promise.<Entities.Entry>

(static) getSpace() → {Promise.<Entities.Space>}

Source:
Gets the Space which the client is currently configured to use
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})
// returns the space object with the above <space-id>
client.getSpace()
.then((space) => console.log(space))
.catch(console.error)
Returns:
Promise for a Space
Type
Promise.<Entities.Space>

(static) parseEntries(raw)

Source:
Parse raw json data into collection of entry objects.Links will be resolved also
Example
let data = {items: [
   {
   sys: {type: 'Entry', locale: 'en-US'},
   fields: {
     animal: {sys: {type: 'Link', linkType: 'Animal', id: 'oink'}},
     anotheranimal: {sys: {type: 'Link', linkType: 'Animal', id: 'middle-parrot'}}
   }
 }
],
includes: {
 Animal: [
   {
     sys: {type: 'Animal', id: 'oink', locale: 'en-US'},
     fields: {
       name: 'Pig',
       friend: {sys: {type: 'Link', linkType: 'Animal', id: 'groundhog'}}
     }
   }
  ]
 }
}
console.log( data.items[0].fields.foo ); // undefined
let parsedData = client.parseEntries(data);
console.log( parsedData.items[0].fields.foo ); // foo
Parameters:
Name Type Description
raw Object json data

(static) sync(query) → {Promise.<Sync.SyncCollection>}

Source:
Synchronizes either all the content or only new content since last sync See Synchronization for more information. Important note: The the sync api endpoint does not support include or link resolution. However contentful.js is doing link resolution client side if you only make an initial sync. For the delta sync (using nextSyncToken) it is not possible since the sdk wont have access to all the data to make such an operation.
Example
const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  accessToken: '<content_delivery_api_key>'
})

client.sync({
  initial: true
})
.then((response) => console.log({
  entries: response.entries,
  assets: response.assets,
  nextSyncToken: response.nextSyncToken
}))
.catch(console.error)
Parameters:
Name Type Description
query Object Query object for the sync call. One of initial or nextSyncToken always needs to be specified, but not both.
Properties
Name Type Attributes Description
initial boolean <nullable>
Indicates if this is the first sync. Use it if you don't have a sync token.
nextSyncToken string <nullable>
The token you got the last time you used this method. Ensures you only get changed content.
type string <optional>
Filter by this type (Entry or Asset)
content_type string <optional>
Filter by this content type id
resolveLinks boolean <optional>
When true, links to other Entries or Assets are resolved. Default: true.
Returns:
Promise for the collection resulting of a sync operation
Type
Promise.<Sync.SyncCollection>