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
getEntrySnapshots function
getContentTypeSnapshots function
Type:
  • Object

Methods

(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.process())
.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('<custom-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 which this Entry is based on
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 which this Entry is based on
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
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, 'image/png'})
.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) 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
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) 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
Returns:
Promise for a Content Type
Type
Promise.<ContentType.ContentType>

(static) getContentTypes({Object) → {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 Description
{Object 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()

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:
Type Description
string contentTypeId id of the content type
object query additional query paramaters
Returns:
Promise

(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
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
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) getEntrySnapshots()

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:
Type Description
string entryId id of the entry
object query additional query paramaters
Returns:
Promise

(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
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('<ui-extension-id>'))
.then((uiExtension) => console.log(uiExtension))
.catch(console.error)
Parameters:
Name Type Description
id string
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
Returns:
Promise for an Upload
Type
Promise.<Upload>