Complete reference guide to the Delivery client library

Complete reference guide to the Delivery client library

The first thing that needs to be done is initiating an instance of Contentful\Delivery\Client by giving it three parameters: an access token, a space ID, and an environment ID (which is optional and defaults to master).

1$client = new \Contentful\Delivery\Client($accessToken, $spaceId, $environmentId);

The client constructor also takes other optional parameters. This is the complete configuration:

1$client = new \Contentful\Delivery\Client(
2 $accessToken,
3 $spaceId,
4 $environmentId = 'master',
5 $options = null
6);

Usage

Assets

NameFetch oneFetch collection
Asset$client->getAsset($assetId, $locale = null)$client->getAssets($query = null)
File$asset->getFile()-
1$asset = $client->getAsset($assetId, 'en-US');
2echo $asset->getTitle('en-US');
3echo $asset->getDescription('en-US');
4/** @var Contentful\Core\File\FileInterface $file */
5$file = $asset->getFile('en-US');
6
7$assets = $client->getAssets();
8foreach ($assets as $asset) {
9 echo $asset->getTitle('en-US')
10}

Content types

NameFetch oneFetch collection
ContentType$client->getContentType($contentTypeId)$client->getContentTypes($query = null)
1$contentType = $client->getContentType($contentTypeId);
2echo $contentType->getName('en-US');
3
4$contentTypes = $client->getContentTypes();
5foreach ($contentTypes as $contentType) {
6 echo $contentType->getName('en-US');
7}

Entries

NameFetch oneFetch collection
Entry$client->getEntry($assetId, $locale = null)$client->getEntries($query = null)
1$entry = $client->getEntry($assetId, 'en-US');
2// Entries have magic getters which correspond to field names
3// This assumes you have a field named "title"
4echo $entry->getTitle('en-US');
5
6$entries = $client->getEntries();
7foreach ($entries as $entry) {
8 echo $entry->getTitle('en-US')
9}

Environment and locales

NameFetch oneFetch collection
Environment$client->getEnvironment()-
Locale-$environment->getLocales()

Environments in this client library are a virtual resource, which is used as a container for locales:

1$environment = $client->getEnvironment();
2
3foreach ($environment->getLocales() as $locale) {
4 /** @var \Contentful\Delivery\Resource\Locale $locale */
5 echo $locale->getCode();
6}

Space

NameFetch oneFetch collection
Space$client->getSpace()-
1$space = $client->getSpace();
2echo $space->getName();