Version | Article link |
---|---|
AEM as a Cloud Service | Click here |
AEM 6.5 | This article |
Learn about support for Content Fragments in the Assets HTTP API, an important piece of AEM’s headless delivery feature.
The Assets HTTP API encompasses the:
The current implementation of the Assets HTTP API is based on the REST architectural style.
The Assets REST API allows developers for Adobe Experience Manager to access content (stored in AEM) directly over the HTTP API, via CRUD operations (Create, Read, Update, Delete).
The API lets you operate Adobe Experience Manager as a headless CMS (Content Management System) by providing Content Services to a JavaScript front end application. Or any other application that can execute HTTP requests and handle JSON responses.
For example, Single Page Applications (SPA), framework-based or custom, require content provided over the HTTP API, often in JSON format.
While AEM Core Components provide a very comprehensive, flexible and customizable API that can serve required Read operations for this purpose, and whose JSON output can be customized, they do require AEM WCM (Web Content Management) know-how for implementation as they must be hosted in pages that are based on dedicated AEM templates. Not every SPA development organization has direct access to such knowledge.
This is when the Assets REST API can be used. It allows developers to access assets (for example, images and content fragments) directly, without the need to first embed them in a page, and deliver their content in serialized JSON format.
It is not possible to customize JSON output from the Assets REST API.
The Assets REST API also allows developers to modify content - by creating new, updating, or deleting existing assets, content fragments and folders.
The Assets REST API:
follows the HATEOAS principle
implements the SIREN format
The Assets REST API is available on each out-of-the-box install of a recent AEM version.
The Assets REST API offers REST-style access to assets stored within an AEM instance.
It uses the /api/assets
endpoint and requires the path of the asset to access it (without the leading /content/dam
).
/content/dam/path/to/asset
/api/assets/path/to/asset
For example, to access /content/dam/wknd/en/adventures/cycling-tuscany
, request /api/assets/wknd/en/adventures/cycling-tuscany.json
Access over:
/api/assets
does not need the use of the .model
selector./content/path/to/page
does require the use of the .model
selector.The HTTP method determines the operation to be executed:
The request body and/or URL parameters can be used to configure some of these operations; for example, define that a folder or an asset should be created by a POST request.
The exact format of supported requests is defined in the API Reference documentation.
All requests are atomic.
This means that subsequent (write
) requests cannot be combined into a single transaction that could succeed or fail as a single entity.
Aspect | Assets REST API |
AEM Component (components using Sling Models) |
Supported use-case(s) | General purpose. | Optimized for consumption in a Single Page Application (SPA), or any other (content consuming) context. Can also contain layout information. |
Supported operations | Create, Read, Update, Delete. With additional operations depending on the entity type. |
Read-only. |
Access | Can be accessed directly. Uses the An example path would look like: |
Needs to be referenced through an AEM component on an AEM page. Uses the An example path would look like: |
Security | Multiple options are possible. OAuth is proposed; can be configured separately from standard setup. |
Uses AEM's standard setup. |
Architectural remarks | Write access will typically address an author instance. Read may also be directed to a publish instance. |
As this approach is read-only, it will typically be used for publish instances. |
Output | JSON-based SIREN output: verbose, but powerful. Allows for navigating within the content. | JSON-based proprietary output; configurable through Sling Models. Navigating the content structure is hard to implement (but not necessarily impossible). |
If the Assets REST API is used within an environment without specific authentication requirements, AEM’s CORS filter needs to be configured correctly.
For further information see:
In environments with specific authentication requirements, OAuth is recommended.
Content Fragments are a specific type of Asset, see Working with Content Fragments.
For further information about features available through the API see:
The Assets REST API supports paging (for GET requests) via the URL parameters:
offset
- the number of the first (child) entity to retrievelimit
- the maximum number of entities returnedThe response will contain paging information as part of the properties
section of the SIREN output. This srn:paging
property contains the total number of (child) entities ( total
), the offset and the limit ( offset
, limit
) as specified in the request.
Paging is typically applied on container entities (that is, folders or assets with renditions), as it relates to the children of the requested entity.
GET /api/assets.json?offset=2&limit=3
...
"properties": {
...
"srn:paging": {
"total": 7,
"offset": 2,
"limit": 3
}
...
}
...
Folders act as containers for assets and other folders. They reflect the structure of the AEM content repository.
The Assets REST API exposes access to the properties of a folder; for example, its name, title, and so on. Assets are exposed as child entities of folders, and sub-folders.
Depending on the asset type of the child assets and folders the list of child entities may already contain the full set of properties that defines the respective child entity. Alternatively, only a reduced set of properties may be exposed for an entity in this list of child entities.
If an asset is requested, the response will return its metadata; such as title, name and other information as defined by the respective asset schema.
The binary data of an asset is exposed as a SIREN link of type content
.
Assets can have multiple renditions. These are typically exposed as child entities, one exception being a thumbnail rendition, which is exposed as a link of type thumbnail
( rel="thumbnail"
).
A content fragment is a special type of asset. They can be used to access structured data, such as texts, numbers, dates, among others.
As there are several differences to standard assets (such as images or audio), some additional rules apply to handling them.
Content fragments:
Do not expose any binary data.
Are completely contained in the JSON output (within the properties
property).
Are also considered atomic, that is, the elements and variations are exposed as part of the fragment’s properties vs. as links or child entities. This allows for efficient access to the payload of a fragment.
Currently the models that define the structure of a content fragment are not exposed through an HTTP API. Therefore the consumer needs to know about the model of a fragment (at least a minimum) - although most information can be inferred from the payload; as data types, and so on. are part of the definition.
To create a content fragment, the (internal repository) path of the model has to be provided.
Associated content is currently not exposed.
Usage can differ depending on whether you are using an AEM author or publish environment, together with your specific use case.
It is strongly recommended that creation is bound to an author instance (and currently there is no means to replicate a fragment to publish using this API).
Delivery is possible from both, as AEM serves requested content in JSON format only.
Storage and delivery from an AEM author instance should suffice for behind-the-firewall, media library applications.
For live web delivery, an AEM publish instance is recommended.
The dispatcher configuration on AEM instances might block access to /api
.
For further details, see the API Reference. In particular, Adobe Experience Manager Assets API - Content Fragments.
Usage is via:
GET /{cfParentPath}/{cfName}.json
For example:
http://<host>/api/assets/wknd/en/adventures/cycling-tuscany.json
The response is serialized JSON with the content structured as in the content fragment. References are delivered as reference URLs.
Two types of read operations are possible:
Usage is via:
POST /{cfParentPath}/{cfName}
The body has to contain a JSON representation of the content fragment to be created, including any initial content that should be set on the content fragment elements. It is mandatory to set the cq:model
property and it must point to a valid content fragment model. Failing to do so will result in an error. It is also necessary to add a header Content-Type
which is set to application/json
.
Usage is via
PUT /{cfParentPath}/{cfName}
The body has to contain a JSON representation of what is to be updated for the given content fragment.
This can simply be the title or description of a content fragment, or a single element, or all element values and/or metadata.
Usage is via:
DELETE /{cfParentPath}/{cfName}
There are a few limitations:
The following status codes can be seen in the relevant circumstances:
200 (OK)
Returned when:
GET
PUT
201 (Created)
Returned when:
POST
404 (Not found)
Returned when:
500 (Internal server error)
This error is returned:
The following lists common scenarios when this error status is returned, together with the error message (monospace) generated:
Parent folder does not exist (when creating a content fragment via POST
)
No content fragment model is supplied (cq:model is missing), cannot be read (due to an invalid path or a permission problem) or there is no valid fragment model:
No content fragment model specified
Cannot create a resource of given model '/foo/bar/qux'
The content fragment could not be created (potentially a permission problem):
Could not create content fragment
Title and or description could not be updated:
Could not set value on content fragment
Metadata could not be set:
Could not set metadata on content fragment
Content element could not be found or could not be updated
Could not update content element
Could not update fragment data of element
The detailed error messages are usually returned in the following manner:
{
"class": "core/response",
"properties": {
"path": "/api/assets/foo/bar/qux",
"location": "/api/assets/foo/bar/qux.json",
"parentLocation": "/api/assets/foo/bar.json",
"status.code": 500,
"status.message": "...{error message}.."
}
}
See here for detailed API references:
For further information see: