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
getTag function
getTags function
getContentTypes function
getEntry function
getEntries function
getAsset function
getAssets function
createAssetKey function
parseEntries function
sync function
Type:
  • Object

Methods

(async, static) createAssetKey(expiresAt) → {Promise.<Entities.AssetKey>}

Source:
Creates an asset key for signing asset URLs (Embargoed Assets)
Example
const contentful = require('contentful')

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

const assetKey = await client.getAssetKey(<UNIX timestamp>)
console.log(assetKey)
Parameters:
Name Type Description
expiresAt number UNIX timestamp in the future, maximum of 48h from now
Returns:
Promise for an AssetKey
Type
Promise.<Entities.AssetKey>

(async, 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>'
})

const asset = await client.getAsset('<asset_id>')
console.log(asset)
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>

(async, 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>'
})

const response = await client.getAssets()
console.log(response.items)
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>

(async, 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>'
})

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

(async, 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>'
})

const response = await client.getContentTypes()
console.log(response.items)
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>

(async, 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>'
})

const response = await client.getEntries()
console.log(response.items)
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>

(async, 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>'
})

const entry = await client.getEntry('<entry_id>')
console.log(entry)
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>

(async, static) getLocales(queryopt) → {Promise.<Entities.LocaleCollection>}

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

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

const response = await client.getLocales()
console.log(response.items)
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 Locale
Type
Promise.<Entities.LocaleCollection>

(async, 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>
const space = await client.getSpace()
console.log(space)
Returns:
Promise for a Space
Type
Promise.<Entities.Space>

(async, static) getTag(id) → {Promise.<Entities.Tag>}

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

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

const tag = await client.getTag('<asset_id>')
console.log(tag)
Parameters:
Name Type Description
id string
Returns:
Promise for a Tag
Type
Promise.<Entities.Tag>

(async, static) getTags(queryopt) → {Promise.<Entities.TagCollection>}

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

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

const response = await client.getTags()
console.log(response.items)
Parameters:
Name Type Attributes Description
query Object <optional>
Object with search parameters.
Returns:
Promise for a collection of Tags
Type
Promise.<Entities.TagCollection>

(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

(async, static) sync(query, options) → {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>'
})

const response = await client.sync({
  initial: true
})
console.log({
  entries: response.entries,
  assets: response.assets,
  nextSyncToken: response.nextSyncToken
})
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 (all (default), Entry, Asset, Deletion, DeletedAsset or DeletedEntry)
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.
options Object
Properties
Name Type Attributes Default Description
paginate boolean <optional>
true Set to false to disable pagination
Returns:
Promise for the collection resulting of a sync operation
Type
Promise.<Sync.SyncCollection>