HAL Usage
ContentGrid applications expose a REST API using JSON as a response format. All JSON responses use HAL as a hypertext format, linking to related resources. Actions that can be taken on resources are specified using the HAL-FORMS format.
Before using the API, you need to authenticate and obtain an access token.
This guide explains how HAL features make the API discoverable and how to leverage HAL features to build robust integrations.
Basic usage of the API, where no automatic adaptations to the data model are necessary is described in API Usage.
When to use HAL?
Using HAL is only recommended when building integrations with ContentGrid that have to dynamically discover the datamodel and adjust to it.
It is particularly useful for systems that integrate with multiple distinct ContentGrid applications, which will have different datamodels.
When building such a dynamic system, you want it to automatically discover the datamodel and automatically adjust to it when it changes.
HAL provides several benefits for these systems:
- Discoverability: Navigate the API by following links instead of constructing URLs manually
- Flexibility: Discover available operations dynamically instead of hardcoding them
- Evolvability: Adapt to data model changes without breaking your integration
- Self-documentation: The API tells you what actions are possible on each resource
HAL Basics
HAL and HAL-FORMS are built on top of JSON. They add 3 special fields to the resource objects:
_links: Contains links to related resources_embedded: Directy embeds related resources_templates: Describes operations that can be performed on this resource (HAL-FORMS)
Working with Links
Links connect resources together. Instead of constructing URLs yourself, you use the provided link to go to the related resource.
Link Relation Types
Links express a relation between the resource they appear on (the link context), and the link target. The link relation type (RFC8288 Sec 3.3) determines the kind of relation between the link context and the link target.
Next to the standard link relation types, ContentGrid defines extension link relation types.
CURIEs
CURIEs (Compact URIs) are a shorthand notation for extension link relation types. They are used as defined in the HAL specification. Before comparing a link relation, a CURIE must be expanded into the full URI.
See the Link Relation Types reference for the formal definition of CURIE expansion and examples.
Working with Embedded Resources
Instead of linking to a related resource, it is also possible to directly embed it in the main resource. This is of only possible when the resource to be embedded is also a HAL resource.
In the ContentGrid application, this is mostly used for the entity-collection resource, where the page is the main resource, and all items on the page are embedded resources.
Link Relation Types work the same for embedded resources as they do for links.
An embedded resource should have a self link.
The link attributes on the self-link of the embedded resource can be considered as
the link attributes that would be present on a normal link to the embedded resource.
Working with HAL-FORMS Templates
Where HAL links describe related resources, HAL-FORMS templates describe the operations available on the resource.
HAL-FORMS are described in their own specification. ContentGrid adds additional functionality to the base specification, described in HAL-FORMS extensions.
The keys in _templates tell you what forms are available.
ContentGrid standardizes the following keys:
| Resource | Key | Description |
|---|---|---|
| entity-item | default |
Update the resource |
| entity-item | delete |
Delete the resource |
| entity-item | set-<relation> |
Update a to-one relation |
| entity-item | add-<relation> |
Add items to a to-many relation |
| entity-item | clear-<relation> |
Remove all items from a relation |
| entity-profile | create-form |
Create a new item of the type described by the profile |
| entity-profile | search |
Search within the collection of the type described by the profile |
HAL-FORMS Extensions
ContentGrid extends the HAL-FORMS specification with support for nested JSON objects, additional body encodings (multipart/form-data and text/uri-list), and extended options element behavior.
See the HAL-FORMS Extensions reference for the full specification.
Navigating Collections
The entity-collection resource provides HAL links that can be used pagination.
Pagination uses the standard IANA-registered link relation types for their purpose:
first: First page of results (may be absent if you’re already on the first page)prev: Previous page of results (absent when on the first page)next: Next page of results (absent when on the last page)self: Current page
To navigate through pages, follow the next link until it’s absent. The cursors in these links are opaque values managed by the server - don’t try to parse or construct them yourself.
Discovering the API Structure
Start at the entities-root resource to discover the entire API.
The root resource contains cg:entity links to all entity collections in your data model. Use these to discover what entities are available without hardcoding entity names.
Alternatively, follow the profile link to arrive at the profile-root, and follow the cg:entity links there to arrive at the entity-profiles for every entity in your data model.
Using Entity Profiles
Entity profiles provide machine-readable metadata about entity types in a ContentGrid application. They describe the attributes, relations, constraints, and available operations for each entity type.
The entity-profile supports content negotiation between two formats:
| Accept Header | Format |
|---|---|
application/prs.hal-forms+json |
HAL-FORMS format with embedded resources and templates |
application/schema+json |
JSON Schema format (JSON Schema 2020-12) |
Profiles are linked from:
- The profile-root resource (
/profile) viacg:entitylinks - entity-collection resources via the
profilelink relation type
HAL-FORMS Format
The HAL-FORMS profile format is the richest representation. It describes the full model of an entity type, its attributes and relations, and provides search and create-form templates.
The full HAL-FORMS entity profile format is documented in the Entity Profiles: HAL-FORMS Format reference.
JSON Schema Format
Request a profile with Accept: application/schema+json to receive a JSON Schema (2020-12) representation of the entity type.
In this format:
- Attributes are mapped to
propertieswith their corresponding JSON Schema types - Relations are represented as properties with
"format": "uri" - Content attributes use
"$ref": "#/$defs/content", with sub-attribute properties defined in$defs.content - Required attributes are listed in the
requiredarray