ContentfulEnvironmentAPI

ContentfulEnvironmentAPI

Source:
Contentful Environment API. Contains methods to access any operations at a space level, such as creating and reading entities contained in a space.

Type Definitions

ContentfulEnvironmentAPI

Source:
Properties:
Name Type Description
delete function
update function
getContentType function
getContentTypes function
createContentType function
createContentTypeWithId function
getEntry function
getEntries function
createEntry function
createEntryWithId function
getAsset function
getAssets function
createAsset function
createAssetWithId function
getLocale function
getLocales function
createLocale function
getUiExtension function
getUiExtensions function
createUiExtension function
createUiExtensionWithId function
getAppInstallation function
getAppInstallations function
createAppInstallation function
getEntrySnapshots function
getContentTypeSnapshots function
Type:
  • object

Methods

(static) createAppInstallation(id) → {Promise.<AppInstallation.AppInstallation>}

Source:
Gets an App Installation
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
 .then((space) => space.getEnvironment('<environment-id>'))
 .then((environment) => environment.createAppInstallation('<app_definition_id>', {
   parameters: {
     someParameter: someValue
   }
  })
 .then((appInstallation) => console.log(appInstallation))
 .catch(console.error)
Parameters:
Name Type Description
id string AppDefinition ID
Returns:
Promise for an App Installation
Type
Promise.<AppInstallation.AppInstallation>

(static) createAsset(data) → {Promise.<Asset.Asset>}

Source:
See:
Creates a Asset. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
Example
const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

// Create asset
client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createAsset({
  fields: {
    title: {
      'en-US': 'Playsam Streamliner'
   },
   file: {
      'en-US': {
        contentType: 'image/jpeg',
       fileName: 'example.jpeg',
       upload: 'https://example.com/example.jpg'
     }
   }
  }
}))
.then((asset) => asset.processForLocale("en-US")) // OR asset.processForAllLocales()
.then((asset) => console.log(asset))
.catch(console.error)
Parameters:
Name Type Description
data object Object representation of the Asset to be created. Note that the field object should have an upload property on asset creation, which will be removed and replaced with an url property when processing is finished.
Returns:
Promise for the newly created Asset
Type
Promise.<Asset.Asset>

(static) createAssetFromFiles(data) → {Promise.<Asset.Asset>}

Source:
See:
Creates a Asset based on files. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
Example
const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createAssetFromFiles({
  fields: {
    file: {
      'en-US': {
         contentType: 'image/jpeg',
         fileName: 'filename_english.jpg',
         file: createReadStream('path/to/filename_english.jpg')
      },
      'de-DE': {
         contentType: 'image/svg+xml',
         fileName: 'filename_german.svg',
         file: '<svg><path fill="red" d="M50 50h150v50H50z"/></svg>'
      }
    }
  }
}))
.then((asset) => console.log(asset))
.catch(console.error)
Parameters:
Name Type Description
data object Object representation of the Asset to be created. Note that the field object should have an uploadFrom property on asset creation, which will be removed and replaced with an url property when processing is finished.
Properties
Name Type Description
fields.file.[LOCALE].file object Can be a string, an ArrayBuffer or a Stream.
Returns:
Promise for the newly created Asset
Type
Promise.<Asset.Asset>

(static) createAssetWithId(id, data) → {Promise.<Asset.Asset>}

Source:
See:
Creates a Asset with a custom ID. After creation, call asset.processForLocale or asset.processForAllLocales to start asset processing.
Example
const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

// Create asset
client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createAssetWithId('<asset_id>', {
  title: {
    'en-US': 'Playsam Streamliner'
  },
  file: {
    'en-US': {
      contentType: 'image/jpeg',
      fileName: 'example.jpeg',
      upload: 'https://example.com/example.jpg'
    }
  }
}))
.then((asset) => asset.process())
.then((asset) => console.log(asset))
.catch(console.error)
Parameters:
Name Type Description
id string Asset ID
data object Object representation of the Asset to be created. Note that the field object should have an upload property on asset creation, which will be removed and replaced with an url property when processing is finished.
Returns:
Promise for the newly created Asset
Type
Promise.<Asset.Asset>

(static) createContentType(data) → {Promise.<ContentType.ContentType>}

Source:
See:
Creates a Content Type
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createContentType({
  name: 'Blog Post',
  fields: [
    {
      id: 'title',
      name: 'Title',
      required: true,
      localized: false,
      type: 'Text'
    }
  ]
}))
.then((contentType) => console.log(contentType))
.catch(console.error)
Parameters:
Name Type Description
data object Object representation of the Content Type to be created
Returns:
Promise for the newly created Content Type
Type
Promise.<ContentType.ContentType>

(static) createContentTypeWithId(id, data) → {Promise.<ContentType.ContentType>}

Source:
See:
Creates a Content Type with a custom ID
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createContentTypeWithId('<content-type-id>', {
  name: 'Blog Post',
  fields: [
    {
      id: 'title',
      name: 'Title',
      required: true,
      localized: false,
      type: 'Text'
    }
  ]
}))
.then((contentType) => console.log(contentType))
.catch(console.error)
Parameters:
Name Type Description
id string Content Type ID
data object Object representation of the Content Type to be created
Returns:
Promise for the newly created Content Type
Type
Promise.<ContentType.ContentType>

(static) createEntry(contentTypeId, data) → {Promise.<Entry.Entry>}

Source:
See:
Creates a Entry
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createEntry('<content_type_id>', {
  fields: {
    title: {
      'en-US': 'Entry title'
    }
  }
}))
.then((entry) => console.log(entry))
.catch(console.error)
Parameters:
Name Type Description
contentTypeId string The Content Type ID of the newly created Entry
data object Object representation of the Entry to be created
Returns:
Promise for the newly created Entry
Type
Promise.<Entry.Entry>

(static) createEntryWithId(contentTypeId, id, data) → {Promise.<Entry.Entry>}

Source:
See:
Creates a Entry with a custom ID
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

// Create entry
client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createEntryWithId('<content_type_id>', '<entry_id>', {
  fields: {
    title: {
      'en-US': 'Entry title'
    }
  }
}))
.then((entry) => console.log(entry))
.catch(console.error)
Parameters:
Name Type Description
contentTypeId string The Content Type of the newly created Entry
id string Entry ID
data object Object representation of the Entry to be created
Returns:
Promise for the newly created Entry
Type
Promise.<Entry.Entry>

(static) createLocale(data) → {Promise.<Locale.Locale>}

Source:
See:
Creates a Locale
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

// Create locale
client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createLocale({
  name: 'German (Austria)',
  code: 'de-AT',
  fallbackCode: 'de-DE',
  optional: true
}))
.then((locale) => console.log(locale))
.catch(console.error)
Parameters:
Name Type Description
data object Object representation of the Locale to be created
Returns:
Promise for the newly created Locale
Type
Promise.<Locale.Locale>

(static) createUiExtension(data) → {Promise.<UiExtension.UiExtension>}

Source:
See:
Creates a UI Extension
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createUiExtension({
  extension: {
    name: 'My awesome extension',
    src: 'https://example.com/my',
    fieldTypes: [
      {
        type: 'Symbol'
      },
      {
        type: 'Text'
      }
    ],
    sidebar: false
  }
}))
.then((uiExtension) => console.log(uiExtension))
.catch(console.error)
Parameters:
Name Type Description
data object Object representation of the UI Extension to be created
Returns:
Promise for the newly created UI Extension
Type
Promise.<UiExtension.UiExtension>

(static) createUiExtensionWithId(id, data) → {Promise.<UiExtension.UiExtension>}

Source:
See:
Creates a UI Extension with a custom ID
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createUiExtensionWithId('<extension_id>', {
  extension: {
    name: 'My awesome extension',
    src: 'https://example.com/my',
    fieldTypes: [
      {
        type: 'Symbol'
      },
      {
        type: 'Text'
      }
    ],
    sidebar: false
  }
}))
.then((uiExtension) => console.log(uiExtension))
.catch(console.error)
Parameters:
Name Type Description
id string Extension ID
data object Object representation of the UI Extension to be created
Returns:
Promise for the newly created UI Extension
Type
Promise.<UiExtension.UiExtension>

(static) createUpload(data) → {Promise.<Upload>}

Source:
Creates a Upload.
Example
const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})
const uploadStream = createReadStream('path/to/filename_english.jpg')

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.createUpload({file: uploadStream})
.then((upload) => console.log(upload))
.catch(console.error)
Parameters:
Name Type Description
data object Object with file information.
Properties
Name Type Description
file object Actual file content. Can be a string, an ArrayBuffer or a Stream.
Returns:
Upload object containing information about the uploaded file.
Type
Promise.<Upload>

(static) getAppInstallation(id) → {Promise.<AppInstallation.AppInstallation>}

Source:
Gets an App Installation
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
 .then((space) => space.getEnvironment('<environment-id>'))
 .then((environment) => environment.getAppInstallation('<app-definition-id>'))
 .then((appInstallation) => console.log(appInstallation))
 .catch(console.error)
Parameters:
Name Type Description
id string AppDefintion ID
Returns:
Promise for an App Installation
Type
Promise.<AppInstallation.AppInstallation>

(static) getAppInstallations() → {Promise.<AppInstallation.AppInstallationCollection>}

Source:
Gets a collection of App Installation
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
 .then((space) => space.getEnvironment('<environment-id>'))
 .then((environment) => environment.getAppInstallations()
 .then((response) => console.log(response.items))
 .catch(console.error)
Returns:
Promise for a collection of App Installations
Type
Promise.<AppInstallation.AppInstallationCollection>

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

Source:
Gets an Asset Warning: if you are using the select operator, when saving, any field that was not selected will be removed from your entry in the backend
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

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

(static) getAssetFromData(entryData) → {Asset.Asset}

Source:
Creates SDK Asset object (locally) from entry data
Example
environment.getAsset('asset_id').then(asset => {

  // Build a plainObject in order to make it usable for React (saving in state or redux)
  const plainObject = asset.toPlainObject();

  // The asset is being updated in some way as plainObject:
  const updatedPlainObject = {
    ...plainObject,
    fields: {
      ...plainObject.fields,
      title: {
        'en-US': 'updatedTitle'
      }
    }
  };

  // Rebuild an sdk object out of the updated plainObject:
  const assetWithMethodsAgain = environment.getAssetFromData(updatedPlainObject);

  // Update with help of the sdk method:
  assetWithMethodsAgain.update();

});
Parameters:
Name Type Description
entryData object Asset ID
Returns:
Asset
Type
Asset.Asset

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

Source:
Gets a collection of Assets Warning: if you are using the select operator, when saving, any field that was not selected will be removed from your entry in the backend
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.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.<Asset.AssetCollection>

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

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

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getContentType('<content_type_id>'))
.then((contentType) => console.log(contentType))
.catch(console.error)
Parameters:
Name Type Description
id string Content Type ID
Returns:
Promise for a Content Type
Type
Promise.<ContentType.ContentType>

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

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

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.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.<ContentType.ContentTypeCollection>

(static) getContentTypeSnapshots(contentTypeId, queryopt) → {Promise.<Snapshot.SnapshotCollection>}

Source:
Gets all snapshots of a contentType
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getContentTypeSnapshots('<contentTypeId>'))
.then((snapshots) => console.log(snapshots.items))
.catch(console.error)
Parameters:
Name Type Attributes Description
contentTypeId string Content Type ID
query object <optional>
query additional query paramaters
Properties
Name Type Attributes Description
skip number <optional>
optional, number of items to skip
limit number <optional>
optional, limit total number of snapshots returned
Returns:
Promise for a collection of Content Type Snapshots
Type
Promise.<Snapshot.SnapshotCollection>

(static) getEditorInterfaceForContentType(contentTypeId) → {Promise.<EditorInterface.EditorInterface>}

Source:
Gets an EditorInterface for a ContentType
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getEditorInterfaceForContentType('<content_type_id>'))
.then((EditorInterface) => console.log(EditorInterface))
.catch(console.error)
Parameters:
Name Type Description
contentTypeId string Content Type ID
Returns:
Promise for an EditorInterface
Type
Promise.<EditorInterface.EditorInterface>

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

Source:
Gets a collection of Entries Warning: if you are using the select operator, when saving, any field that was not selected will be removed from your entry in the backend
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getEntries({'content_type': 'foo'})) // you can add more queries as 'key': 'value'
.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.<Entry.EntryCollection>

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

Source:
Gets an Entry Warning: if you are using the select operator, when saving, any field that was not selected will be removed from your entry in the backend
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

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

(static) getEntryFromData(entryData) → {Entry.Entry}

Source:
Creates SDK Entry object (locally) from entry data
Example
environment.getEntry('entryId').then(entry => {

  // Build a plainObject in order to make it usable for React (saving in state or redux)
  const plainObject = entry.toPlainObject();

  // The entry is being updated in some way as plainObject:
  const updatedPlainObject = {
    ...plainObject,
    fields: {
      ...plainObject.fields,
      title: {
        'en-US': 'updatedTitle'
      }
    }
  };

  // Rebuild an sdk object out of the updated plainObject:
  const entryWithMethodsAgain = environment.getEntryFromData(updatedPlainObject);

  // Update with help of the sdk method:
  entryWithMethodsAgain.update();

});
Parameters:
Name Type Description
entryData object Entry Data
Returns:
Entry
Type
Entry.Entry

(static) getEntrySnapshots(entryId, queryopt) → {Promise.<Snapshot.SnapshotCollection>}

Source:
Gets all snapshots of an entry
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getEntrySnapshots('<entry_id>'))
.then((snapshots) => console.log(snapshots.items))
.catch(console.error)
Parameters:
Name Type Attributes Description
entryId string Entry ID
query object <optional>
query additional query paramaters
Properties
Name Type Attributes Description
skip number <optional>
optional, number of items to skip
limit number <optional>
optional, limit total number of snapshots returned
Returns:
Promise for a collection of Entry Snapshots
Type
Promise.<Snapshot.SnapshotCollection>

(static) getLocale(id) → {Promise.<Locale.Locale>}

Source:
Gets a Locale
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getLocale('<locale_id>'))
.then((locale) => console.log(locale))
.catch(console.error)
Parameters:
Name Type Description
id string Locale ID
Returns:
Promise for an Locale
Type
Promise.<Locale.Locale>

(static) getLocales() → {Promise.<Locale.LocaleCollection>}

Source:
Gets a collection of Locales
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getLocales())
.then((response) => console.log(response.items))
.catch(console.error)
Returns:
Promise for a collection of Locales
Type
Promise.<Locale.LocaleCollection>

(static) getUiExtension(id) → {Promise.<UiExtension.UiExtension>}

Source:
Gets an UI Extension
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getUiExtension('<extension-id>'))
.then((uiExtension) => console.log(uiExtension))
.catch(console.error)
Parameters:
Name Type Description
id string Extension ID
Returns:
Promise for an UI Extension
Type
Promise.<UiExtension.UiExtension>

(static) getUiExtensions() → {Promise.<UiExtension.UiExtensionCollection>}

Source:
Gets a collection of UI Extension
Example
const contentful = require('contentful-management')

const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getUiExtensions()
.then((response) => console.log(response.items))
.catch(console.error)
Returns:
Promise for a collection of UI Extensions
Type
Promise.<UiExtension.UiExtensionCollection>

(static) getUpload(id) → {Promise.<Upload>}

Source:
Gets an Upload
Example
const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})
const uploadStream = createReadStream('path/to/filename_english.jpg')

client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment-id>'))
.then((environment) => environment.getUpload('<upload-id>')
.then((upload) => console.log(upload))
.catch(console.error)
Parameters:
Name Type Description
id string Upload ID
Returns:
Promise for an Upload
Type
Promise.<Upload>