Content types
Defining a content type is a fundamental step in powering your applications with Contentful. A content type consists of a set of fields and other information, read this guide to learn more about modeling your content.
Content type collection
Get all content types of a space
Create a content type with POST
Whilst it’s possible to create content types with POST, it’s strongly discouraged.
When you use this endpoint, the API will automatically generate an ID for the created content type and return it with the response.
Using the method outlined below allows you to control the ID of the created content type. This is important for content type IDs as they are often used as parameters in code.
Content type
Create a content type with PUT
To update a content type, use the above endpoint with its ID.
X-Contentful-Version.Validations
When creating or updating a content type, you can add or remove validations to the fields in the content type schema by specifying the validations property of a field.
To remove a specific validation, update the content type leaving that validation out of the field’s validations collection. To remove all the validations applied to a field, update the content type schema removing the validations property.
To update a content type with validations, use the Create a content type with PUT endpoint and pass validations in the request body, see example:
Validations for hidden and required fields
If a field’s validation is set as both disabled: true and required: true but is left empty, a validation error occurs and you cannot publish the content type. Fields that have a default value set are not considered empty and can still be published.
To continue publishing entries with hidden and required fields, change the validation of these fields to optional required: false. You can manually update the validation for each field, or run the following script to make the changes in bulk:
Rich text node type validations
Rich text node type validations contain validations and allowed resources for the specified node type. See the example below:
For details about allowedResources see Cross-space referencess.
Default values
Using the defaultValue property of a field, you can define a value that is applied when entries are created.
If defaultValue is omitted the field will not be pre-filled with any value. See the example below:
You can localize default values by providing separate values for each locale. When the value is omitted, in the entry creation request, then the configured default value is applied and the entry saved with it. In case of a non-localized field, only the default value targeting the default locale will be applied to the new entry.
You can set the default value for a locale to be null. This can be helpful to interrupt the locale fallback and
see your field empty after the entry is created.
Default values are not validated according to the field validations property. This means that they are applied to new entries even if they are invalid.
In this case, those entries can’t be published until the field value is fixed.
Limitations: If the default locale of an environment changes, the default value configuration is not modified. So the value targeting the previous default locale will still be applied to it. Currently not all the field types support default values. The supported field ones are:
{type: "Symbol"}{type: "Text"}{type: "Integer"}{type: "Number"}{type: "Date"}{type: "Boolean"}{type: "Array", items: {type: "Symbol"}}only short texts arrays are supported
defaultValue for a content type field will not change previously created entries; it will only apply to entries created after defaultValue has been updated.Annotations
Annotations provide you with means to attach semantic metadata to a content type or parts of it. Once assigned, they can be interpreted by applications and services to adjust their behaviour accordingly.
For example, in Compose an annotation can be assigned to any content type. Once assigned, this content type will be available for use as a page type in Compose, making all entries of that content type available for editing.
We provide a small set of system annotations to drive the behaviour of Experiences and Compose. You can find a list of the annotations below.
Some of the annotations are assigned automatically. You can find a list of them below.
Assigning annotations
By adding a metadata.annotations property to the payload, you can assign annotations to either the content type itself
or individual fields of that content type. Annotations that can be assigned to individual fields will usually have additional
restrictions to which type of field they can be assigned (see Contentful:AggregateComponent above).
The example payload below shows you how to assign annotations to a sample content type. It will do the following things:
- Assign the
Contentful:AggregateRootannotation to the content type itself - Assign the
Contentful:AggregateComponentannotation the the reference fieldsections
Changing field IDs
You can change the ID of a content type field in the Contentful web app. You can also change it via the API, by sending the newId property in the field’s payload to override the current ID.
The API will return different data after this change, and this might break your existing code base. Read more about managing changes to content structure in our multiple environments guide.
To update a content type with field ID, use the Create a content type with PUT endpoint and pass the newId in the request body, see example:
Omitting fields
If you have fields in your content type and entries you don’t want to distribute to end users (e.g. workflow states), you can omit fields from the CDA and CPA responses. To do so, update the content type with the omitted property set to true in the chosen field and activate it.
The field will still be available as part of the CMA responses and in the web app but skipped in the CDA and CPA.
To revert this, repeat this but with omitted set to false.
Disabling fields
If you have fields in your content type and entries you don’t want to distribute to end users (e.g. workflow states), you can disable them from the CDA and CPA responses. To do so, update the content type with the disabled property set to true in the chosen field and activate it.
The field will still be available as part of the CMA responses and in the web app but skipped in the CDA and CPA.
To revert this, repeat this but with disabled set to false.
If a field’s validation is set as both disabled: true and required: true but is left empty, a validation error occurs and you cannot publish the content type. Fields that have a default value set are not considered empty and can still be published. For more information, see the Validations for hidden and required fields section.
Deleting fields
To delete fields you no longer need, first, omit the field you’re targeting for deletion and activate the content type. This step is mandatory to avoid accidental data loss. It allows you to try whether your client applications can handle the deletion and provides an easy way to revert that change.
Once you have confirmed it’s safe to delete the field, update your content type with the corresponding field removed from the payload, or with the deleted property set to true on the content type field you intend to delete. The deletion becomes final after you once again activate the content type. This action is permanent and cannot be undone.
Before you can delete a content type you need to deactivate it.
Content type activation
Activated content type collection
Get all activated content types of a space
Retrieves the activated versions of content types, ignoring any changes made since the last activation.