Complete reference guide to the Management client library

Complete reference guide to the Management client library

The first thing that needs to be done is initiating an instance of Contentful\Management\Client by giving it an access token. All actions performed using this instance of the Client will be performed with the privileges of the user this token belongs to.

1$client = new \Contentful\Management\Client('access-token');

When working with space-scoped or environment-scoped resources, you can use proxies. They are lazy-references to a space or an environment, and they allow you to avoid repeating the space and environment ID when making API calls:

1// Without space proxy
2$deliveryApiKeys = $client->getDeliveryApiKeys($spaceId);
3$roles = $client->getRoles($spaceId);
4
5// With space proxy
6$spaceProxy = $client->getSpaceProxy($spaceId);
7$deliveryApiKeys = $spaceProxy->getDeliveryApiKeys();
8$roles = $spaceProxy->getRoles();
9
10
11// Without environment proxy
12$assets = $client->getAssets($spaceId, $environmentId);
13$entries = $client->getEntries($spaceId, $environmentId);
14
15// With environment proxy
16$environmentProxy = $client->getEnvironmentProxy($spaceId, $environmentId);
17$assets = $environmentProxy->getAssets();
18$entries = $environmentProxy->getEntries();

In the following guide, snippets will be using $environmentProxy and $spaceProxy whenever possible.

Usage

Api Keys

NameScopeFetch oneFetch collectionAvailable actions
DeliveryApiKeySpace$spaceProxy->getDeliveryApiKey($deliveryApiKeyId)$spaceProxy->getDeliveryApiKeys($query = null)Create, delete, update
PreviewApiKeySpace$spaceProxy->getPreviewApiKey($previewApiKeyId)$spaceProxy->getPreviewApiKeys($query = null)-

Fetching:

1$deliveryApiKeys = $spaceProxy->getDeliveryApiKeys();
2$deliveryApiKey = $spaceProxy->getDeliveryApiKey($deliveryApiKeyId);
3
4echo $deliveryApiKey->getSystemProperties()->getId();
5echo $deliveryApiKey->getName();
6echo $deliveryApiKey->getAccessToken();
7$previewApiKeyLink = $deliveryApiKey->getPreviewApiKey();
8
9$previewApiKey = $spaceProxy->resolveLink($previewApiKeyLink);
10echo $previewApiKey->getAccessToken();

Creating and modifying:

1$deliveryApiKey = new \Contentful\Management\Resource\DeliveryApiKey('Mobile');
2
3$spaceProxy->create($deliveryApiKey);
4echo $deliveryApiKey->getSystemProperties()->getId();
5echo $deliveryApiKey->getAccessToken();
6
7$deliveryApiKey->delete();

Assets

NameScopeFetch oneFetch collectionAvailable actions
AssetEnvironment$environmentProxy->getAsset($assetId)$environmentProxy->getAssets($query = null)Create, delete, update, archive, unarchive, publish, unpublish, process

Fetching:

1$assets = $environmentProxy->getAssets();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$assets = $environmentProxy->getAssets($query);
6
7$asset = $environmentProxy->getAsset($assetId);
8
9echo $asset->getSystemProperties()->getId();
10echo $asset->getTitle('en-US');

Creating and modifying:

1$asset = new \Contentful\Management\Resource\Asset();
2$file = new \Contentful\Core\File\RemoteUploadFile('Contentful.svg', 'image/svg+xml', $url);
3$asset->setTitle('en-US', 'My asset')
4 ->setDescription('en-US', 'My description')
5 ->setFile('en-US', $file);
6
7$environmentProxy->create($asset);
8
9// Omit the locale to process the files for all locales
10$asset->process('en-US');
11
12$asset->setDescription('en-US', 'An even better description');
13$asset->update();
14
15$asset->archive();
16$asset->unarchive();
17
18$asset->publish();
19$asset->unpublish();
20
21$asset->delete();

Content types and content type snapshots

NameScopeFetch oneFetch collectionAvailable actions
ContentTypeEnvironment$environmentProxy->getContentType($contentTypeId)$environmentProxy->getContentTypes($query = null)Create, delete, update, publish, unpublish
ContentTypeSnapshotEnvironment$environmentProxy->getContentTypeSnapshot($contentTypeId, $snapshotId)$environmentProxy->getContentTypeSnapshots($contentTypeId, $query = null)-

Fetching:

1$contentTypes = $environmentProxy->getContentTypes();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$contentTypes = $environmentProxy->getContentTypes($query);
6
7$contentType = $environmentProxy->getContentType($contentTypeId);
8
9echo $contentType->getSystemProperties()->getId();
10echo $contentType->getName();
11
12// Fetch the published version of content types
13$contentTypes = $environmentProxy->getPublishedContentTypes($query);
14$contentType = $environmentProxy->getPublishedContentType($contentTypeId);
15
16// Fetch snapshots from a content type, or from an environment proxy
17$snapshots = $contentType->getSnapshots();
18$snapshot = $contentTy->getSnapshot($snapshotId);
19
20$snapshots = $environmentProxy->getContentTypeSnapshots($contentTypeId);
21$snapshot = $environmentProxy->getContentTypeSnapshot($contentTypeId, $snapshotId);

Creating and modifying:

1$contentType = new \Contentful\Management\Resource\ContentType('Blog Post');
2$contentType->setDescription('My description');
3$contentType->addNewField('Symbol', 'title', 'Title');
4$contentType->addNewField('Text', 'body', 'Body');
5
6$customContentTypeId = 'blogPost';
7$environmentProxy->create($contentType, $customContentTypeId);
8
9$contentType->addNewField('Date', 'publishedAt', 'Published At');
10$contentType->update();
11
12$contentType->publish();
13$contentType->unpublish();
14
15$contentType->delete();

Field types

1use Contentful\Management\Resource\ContentType\Field;
2
3// Arrays of strings (Symbol) or links to either assets or entries
4$array = new Field\ArrayField(
5 string $id,
6 string $name,
7 string $itemsType, // Either "Symbol" or "Link"
8 string $itemsLinkType = null // If $itemsType is "Link", then either "Asset" or "Entry"
9);
10// Methods specific to this field
11$array->getItemsType();
12$array->setItemsType(string $itemsType);
13$array->getItemsLinkType();
14$array->setItemsLinkType(string $itemsLinkType);
15$array->getItemsValidations();
16$array->setItemsValidations(ValidationInterface[] $itemsValidations);
17$array->addItemsValidation(ValidationInterface $validation);
18
19// Booleans
20$boolean = new Field\BooleanField(string $id, string $name);
21
22// Dates, with hours
23$date = new Field\DateField(string $id, string $name);
24
25// Integer numbers
26$integer = new Field\IntegerField(string $id, string $name);
27
28// Links to either assets or entries
29$link = new Field\LinkField(
30 string $id,
31 string $name,
32 string $linkType // Either "Asset" or "Entry"
33);
34// Methods specific to this field
35$array->getLinkType();
36$array->setLinkType(string $linkType);
37
38// Location coordinates, with latitude and longitured
39$location = new Field\LocationField(string $id, string $name);
40
41// Float numbers
42$number = new Field\NumberField(string $id, string $name);
43
44// Any JSON-convertible object
45$object = new Field\ObjectField(string $id, string $name);
46
47// Strings
48$symbol = new Field\SymbolField(string $id, string $name);
49
50// Long text
51$text = new Field\TextField(string $id, string $name);
52
53// Shared methods
54$field->getId();
55$field->getName();
56
57$field->isRequired();
58$field->setRequired(bool $required);
59
60$field->isLocalized();
61$field->setLocalized(bool $localized);
62
63$field->isDisabled();
64$field->setDisabled(bool $disabled);
65
66$field->isOmitted();
67$field->setOmitted(bool $omitted);
68
69$field->getValidations();
70$field->setValidations(ValidationInterface[] $validations);
71$field->addValidation(ValidationInterface $validation);

Validations

1use Contentful\Management\Resource\ContentType\Validation;
2
3// Applicable to:
4// - Link (to assets)
5$assetFileSize = new Validation\AssetFileSizeValidation($min = null, $max = null);
6
7// Applicable to:
8// - Link (to image assets)
9$assetImageDimentions = new Validation\AssetImageDimensionsValidation(int $minWidth = null, int $maxWidth = null, int $minHeight = null, int $maxHeight = null);
10
11// Applicable to:
12// - Date
13$dateRange = new Validation\DateRangeValidation(string $min = null, string $max = null);
14
15// Applicable to:
16// - Symbol
17// - Text
18// - Integer
19// - Number
20$in = new Validation\InValidation(array $values = []);
21
22// Applicable to:
23// - Link (to entries)
24$linkContentType = new Validation\LinkContentTypeValidation(array $contentTypes = []);
25
26// Applicable to:
27// - Link (to assets)
28$linkMimetypeGroup = new Validation\LinkMimetypeGroupValidation(array $mimeTypeGroups = []);
29
30// Applicable to:
31// - Integer
32// - Number
33$range = new Validation\RangeValidation(int $min = null, int $max = null);
34
35// Applicable to:
36// - Symbol
37// - Text
38$regexp = new Validation\RegexpValidation(string $pattern = null, string $flags = null);
39
40// Applicable to:
41// - Array
42// - Symbol
43// - Text
44$size = new Validation\SizeValidation($min = null, $max = null);
45
46// Applicable to:
47// - Symbol
48// - Integer
49// - Number
50$unique = new Validation\UniqueValidation();

Editor interfaces

NameScopeFetch oneFetch collectionAvailable actions
EditorInterfaceEnvironment$environmentProxy->getEditorInterface($contentTypeId)-update

Fetching and updating

1$editorInterface = $environmentProxy->getEditorInterface($contentTypeId);
2
3/** @var \Contentful\Management\Resource\EditorInterface\Control $control */
4$control = $editorInterface->getControl('website');
5$control->setWidgetId('urlEditor');
6
7$editorInterface->update();

Entries and entry snapshots

NameScopeFetch oneFetch collectionAvailable actions
EntryEnvironment$environmentProxy->getEntry($entryId)$environmentProxy->getEntries($query = null)create, delete, update, archive, unarchive, publish, unpublish
EntrySnapshotEnvironment$environmentProxy->getEntrySnapshot($entryId, $snapshotId)$environmentProxy->getEntries($entryId, $query = null)-

Fetching:

1$entries = $environmentProxy->getEntries();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$entries = $environmentProxy->getEntries($query);
6
7$entry = $environmentProxy->getEntry($entryId);
8
9echo $entry->getSystemProperties()->getId();
10echo $entry->getField('title', 'en-US');
11
12// Fetch snapshots from an entry, or from an environment proxy
13$snapshots = $entry->getSnapshots();
14$snapshot = $entry->getSnapshot($snapshotId);
15
16$snapshots = $environmentProxy->getEntrySnapshots($contentTypeId);
17$snapshot = $environmentProxy->getEntrySnapshot($entryId, $snapshotId);

Creating and modifying:

1$entry = new \Contentful\Management\Resource\Entry($contentTypeId);
2$entry->setField('title', 'en-US', 'My awesome blog post');
3$entry->setField('body', 'en-US', 'Something something...');
4
5$environmentProxy->create($entry);
6
7$entry->setField('body', 'en-US', 'Updated body');
8$entry->update();
9
10$entry->archive();
11$entry->unarchive();
12
13$entry->publish();
14$entry->unpublish();
15
16$entry->delete();

Environments

NameScopeFetch oneFetch collectionAvailable actions
EnvironmentSpace$spaceProxy->getEnvironment($environmentId)$spaceProxy->getEnvironments($query = null)create, delete, update

Fetching:

1$environments = $spaceProxy->getEnvironments();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$environments = $spaceProxy->getEnvironments($query);
6
7$environment = $spaceProxy->getEnvironment($environmentId);
8
9echo $environment->getSystemProperties()->getId();
10echo $environment->getName();

Creating and modifying:

1$environment = new \Contentful\Management\Resource\Environment('QA');
2$spaceProxy->create($environment);
3
4$environmentId = $environment->getSystemProperties()->getId();
5
6// An environment might take a while to create,
7// depending on the size of the master environment,
8// so it might be a good idea to poll it until it's ready.
9do {
10 $environment = $spaceProxy->getEnvironment($environmentId);
11 $status = $environment->getSystemProperties()->getStatus()->getId();
12} while ($status !== 'ready');
13
14$environment->delete();

Locales

NameScopeFetch oneFetch collectionAvailable actions
LocaleEnvironment$environmentProxy->getLocale($localeId)$environmentProxy->getLocales($query = null)create, delete, update

Fetching:

1$locales = $environmentProxy->getLocales();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$locales = $environmentProxy->getLocales($query);
6
7$locale = $environmentProxy->getLocale($localeId);
8
9echo $locale->getSystemProperties()->getId();
10echo $locale->getName();
11echo $locale->getCode();

Creating and modifying:

1$locale = new \Contentful\Management\Resource\Locale('English (United States)', 'en-US');
2$environmentProxy->create($locale);
3
4$locale->delete();

Organizations

NameScopeFetch oneFetch collectionAvailable actions
Organization--$client->getOrganizations($query = null)-

Fetching:

1$organizations = $client->getOrganizations();
2$organization = $organizations[0];
3
4echo $organization->getSystemProperties()->getId();
5echo $organization->getName();

Personal access tokens

NameScopeFetch oneFetch collectionAvailable actions
PersonalAccessToken-$client->getPersonalAccessToken($personalAccessTokenId)$spaceProxy->getPersonalAccessTokens($query = null)create, update, revoke

Fetching:

1$personalAccessTokens = $client->getPersonalAccessTokens();
2// Optionally, pass a query object
3$personalAccessTokens = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$personalAccessTokens = $client->getPersonalAccessTokens($query);
6
7$personalAccessToken = $client->getPersonalAccessToken($personalAccessTokenId);
8
9echo $personalAccessToken->getSystemProperties()->getId();
10echo $personalAccessToken->getName();

Creating and modifying:

1$readOnly = false;
2$personalAccessToken = new \Contentful\Management\Resource\PersonalAccessToken('Development access token', $readOnly);
3$client->create($personalAccessToken);
4
5// For security reasons, the actual token will only be available after creation.
6echo $personalAccessToken->getToken();
7
8$personalAccessToken->revoke();

Roles

NameScopeFetch oneFetch collectionAvailable actions
RoleSpace$spaceProxy->getRole($roleId)$spaceProxy->getRoles($query = null)create, delete, update

Fetching:

1$roles = $spaceProxy->getRoles();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$roles = $spaceProxy->getRoles($query);
6
7$role = $spaceProxy->getRole($roleId);
8
9echo $role->getSystemProperties()->getId();
10echo $role->getName();

Creating and modifying:

1$role = new \Contentful\Management\Resource\Role('Publisher');
2
3$policy = new \Contentful\Management\Resource\Policy('allow', 'publish');
4$role->addPolicy($policy);
5
6$constraint = new \Contentful\Management\Resource\Role\Constraint\AndConstraint([
7 new \Contentful\Management\Resource\Role\Constraint\EqualityConstraint('sys.type', 'Entry'),
8 new \Contentful\Management\Resource\Role\Constraint\EqualityConstraint('sys.type', 'Asset'),
9]);
10$policy->setConstraint($constraint);
11
12$permissions = $policy->getPermissions();
13$permissions->setContentDelivery('all')
14 ->setContentModel('all')
15 ->setSettings('all');
16
17$spaceProxy->create($role);
18
19$policy->delete();

Contraints

1use Contentful\Management\Resource\Role\Constraint;
2
3$equality = new Constraint\EqualityConstraint(
4 string $doc, // The JSON path of a property, for instance "sys.type"
5 mixed $value // The required value, for instance "Entry"
6);
7
8// Requires that all constraints be satisfied
9$and = new Constraint\AndConstraint(Constraint\ConstraintInterface[] $constraints);
10
11// Requires that only one constraint be satisfied
12$or = new Constraint\OrConstraint(Constraint\ConstraintInterface[] $constraints);
13
14// Negates the content of the inner constraint
15$not = new Constraint\NotConstraint(Constraint\ConstraintInterface $constraint);

Spaces

Fetching:

1$spaces = $client->getSpaces();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$spaces = $client->getSpaces($query);
6
7$space = $client->getSpace($spaceId);
8
9echo $space->getSystemProperties()->getId();
10echo $space->getName();

Creating and modifying:

1$space = new \Contentful\Management\Resource\Space('Website', $organizationId, $defaultLocaleCode);
2$client->create($space);
3
4$space->delete();

Space memberships

NameScopeFetch oneFetch collectionAvailable actions
SpaceMembershipSpace$spaceProxy->getSpaceMembership($environmentId)$spaceProxy->getSpaceMemberships($query = null)create, delete, update

Fetching:

1$spaceMemberships = $spaceProxy->getSpaceMemberships();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$spaceMemberships = $spaceProxy->getSpaceMemberships($query);
6
7$spaceMembership = $spaceProxy->getSpaceMembership($spaceMembershipId);
8
9echo $spaceMembership->getSystemProperties()->getId();
10echo $spaceMembership->getUser()->getId();

Creating and modifying:

1$spaceMembership = new \Contentful\Management\Resource\SpaceMembership();
2$spaceMembership->setEmail($userEmail)
3 ->setAdmin(false)
4 ->addRoleLink($roleId);
5$spaceProxy->create($spaceMembership);
6
7$spaceMembership->delete();

Uploads

NameScopeFetch oneFetch collectionAvailable actions
UploadSpace$spaceProxy->getUpload($uplaodId)-create, delete

Fetching:

1$upload = $spaceProxy->getUpload($uploadId);
2
3echo $upload->getSystemProperties()->getId();

Creating and modifying:

1// You can pass as argument an fopen resource, an actual string, or a PSR-7 compatible stream
2$upload = new \Contentful\Management\Resource\Upload(\fopen($myFile, 'r'));
3$spaceProxy->create($upload);
4
5$asset = new \Contentful\Management\Resource\Asset();
6$asset->setFile('en-US', $upload->asAssetFile());
7
8$environmentProxy->create($asset);
9
10$asset->process();
11
12$upload->delete();

UI extensions

NameScopeFetch oneFetch collectionAvailable actions
ExtensionEnvironment$environmentProxy->getExtension($environmentId)$environmentProxy->getExtensions($query = null)create, delete, update

Fetching:

1$extensions = $environmentProxy->getExtensions();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$extensions = $environmentProxy->getExtensions($query);
6
7$extension = $environmentProxy->getExtension($extensionId);
8
9echo $extension->getSystemProperties()->getId();
10echo $extension->getName();

Creating and modifying:

1$extension = new \Contentful\Management\Resource\Extension('My awesome extension');
2$extension->setSource('https://www.example.com/extension-source')
3 ->addNewFieldType('Symbol');
4
5$environmentProxy->create($extension);
6
7$extension->addNewFieldType('Link', ['Entry']);
8$extension->update();
9
10$extension->delete();

Users

NameScopeFetch oneFetch collectionAvailable actions
User-$client->getUserMe()--

Fetching:

1$user = $client->getUserMe();
2
3echo $user->getSystemProperties()->getId();
4echo $user->getEmail();

Webhooks

NameScopeFetch oneFetch collectionAvailable actions
WebhookSpace$spaceProxy->getWebhook($webhookId)$spaceProxy->getWebhooks($query = null)create, delete, update
WebhookCallSpace$spaceProxy->getWebhookCall($webhookId, $callId)$spaceProxy->getWebhookCalls($webhookId, $query = null)-
WebhookHealthSpace$spaceProxy->getWebhookHealth($webhookId)--

Fetching:

1$webhooks = $spaceProxy->getWebhooks();
2// Optionally, pass a query object
3$query = (new \Contentful\Management\Query())
4 ->setLimit(5);
5$webhooks = $spaceProxy->getWebhooks($query);
6
7$webhook = $spaceProxy->getWebhook($webhookId);
8
9echo $webhook->getSystemProperties()->getId();
10echo $webhook->getName();
11
12// You can get calls and health from a webhook or from a space proxy
13$calls = $webhook->getCalls();
14$call = $webhook->getCall($callId);
15$health = $webhook->getHealth();
16
17$calls = $spaceProxy->getWebhookCalls($webhookId);
18$call = $spaceProxy->getWebhookCall($webhookId, $callId);
19$health = $spaceProxy->getWebhookHealth($webhookId);
20
21echo $call->getStatusCode();
22echo $call->getUrl();
23echo $call->getEventType();
24
25// Request and response are only available when querying the "single" endpoint,
26// not the collection one:
27// Psr\Http\Message\RequestInterface
28$call->getRequest();
29// Psr\Http\Message\ResponseInterface
30$call->getResponse();
31
32echo $health->getTotal();
33echo $health->getHealthy();

Creating and modifying:

1$webhook = new \Contentful\Management\Resource\Webhook('Publish webhook', $url, ['Entry.publish']);
2$spaceProxy->create($webhook);
3
4$webhook->addTopic('Asset.publish');
5$webhook->update();
6
7$webhook->delete();