Official Javascript SDK for the Amplience Dynamic Content Delivery API
This sdk is designed to help build client side and server side content managed applications.
- Fetch content and slots using the Content Delivery Service
- Fetch preview content using Virtual Staging
- Transform content using the Content Rendering Service
- Localize content
- Transform images on the fly using the Dynamic Media Service
So we can have nice things:
- ES6 module & tree-shaking support for tools capable of using ES6 imports (like Rollup, Webpack, or Parcel)
- Backwards compatibility for Node.js-style (CommonJS) imports
- TypeScript type definitions
- Universal Module Definition (UMD) to support direct use in the browser
Using npm:
npm install dc-delivery-sdk-js --save
Using cdn:
<script src="https://unpkg.com/dc-delivery-sdk-js/dist/dynamicContent.browser.umd.min.js"></script>
for legacy browsers:
<script src="https://unpkg.com/dc-delivery-sdk-js/dist/dynamicContent.browser.umd.legacy.min.js"></script>
This sdk supports browser and node.js applications using ES6 or CommonJS style imports.
ES6:
import { ContentClient } from 'dc-delivery-sdk-js';
const client = new ContentClient({
account: 'myaccount'
});
CommonJS:
var ContentClient = require('dc-delivery-sdk-js').ContentClient;
var client = new ContentClient({
account: 'myaccount'
});
If your application does not use a package manager you can directly include the pre-bundled version of the sdk and access the features using the global "ampDynamicContent".
<script src="https://unpkg.com/dc-delivery-sdk-js/dist/dynamicContent.browser.umd.min.js"></script>
var client = new ampDynamicContent.ContentClient({
account: 'myaccount'
});
If you need to support old browsers a legacy version of the bundle is provided, however we strongly recommend using a tool like babel in your project to compile the sdk to your exact browser requirements.
Once your client is created you can request content for a slot or content item id. This will return a promise which will resolve to the JSON of your slot or content item. If no content is found with the provided id the promise will reject with an error.
var slotId = 'cb671f37-0a66-46c3-a011-54ce3cdff241';
client.getContentItem(slotId)
.then(content => {
console.log(content.body);
})
.catch(error => {
console.log('content not found', error);
});
The format of the content object will be specific to your content types, which define the JSON structure of content items and slots, however a set of standard metadata is always included in a property called "_meta".
If the slot or content item requested returns a graph of content, for example a carousel may also return linked slides, these will be included inline in the JSON.
Example:
{
"_meta":{
"schema":"https://www.anyafinn.online/content-types/carousel.json",
"deliveryId":"543246b7-5948-4849-884c-b295402a95b4",
"name":"example-carousel"
},
"slides": [
{
"_meta":{
"schema":"https://www.anyafinn.online/content-types/slide.json",
"deliveryId":"d6ccc158-6ab7-48d0-aa85-d9fbf2aef000",
"name":"example-slide"
},
"heading": "Free shipping until Sunday!"
}
]
}
By default, the content client will request content from the production content delivery services. When a user wants to preview content before it is published you can repoint the client to a virtual staging environment (VSE):
var client = new ContentClient({
account: 'myaccount',
stagingEnvironment: 'fhboh562c3tx1844c2ycknz96-gvzrfgnzc-1546264721816.staging.bigcontent.io'
});
Dynamic Content generates a VSE for each user and typically passes the "stagingEnvironment" value into your application using a URL parameter. This allows each user to effectively have their own staging environment which allows content producers to work in parallel.
Content types can make use of field-level localization to give content producers the ability to enter locale specific values for a field.
By default, every locale value will be returned in the content object:
{
"_meta":{
"schema":"https://www.anyafinn.online/content-types/slide.json",
"deliveryId":"d6ccc158-6ab7-48d0-aa85-d9fbf2aef000",
"name":"example-slide"
},
"heading": {
"_meta":{
"schema":"http://bigcontent.io/cms/schema/v1/core#/definitions/localized-value"
},
"values":[
{
"locale":"en-US",
"value":"Free shipping until Sunday!"
},
{
"locale":"de-de",
"value": "Kostenloser Versand bis Sonntag!"
}
]
}
}
If desired, you can configure the sdk with a locale query. If set, the locale matching is performed server side and only a single value will be returned.
var client = new ContentClient({
account: 'myaccount',
locale: 'en-US,en-*'
});
Returns
{
"_meta":{
"schema":"https://www.anyafinn.online/content-types/slide.json",
"deliveryId":"d6ccc158-6ab7-48d0-aa85-d9fbf2aef000",
"name":"example-slide"
},
"heading": "Free shipping until Sunday!"
}
In addition to serving image and Video content, Dynamic Content can also transform media on the fly allowing you to target multiple channels and deliver just the pixels required from a single master asset.
The sdk attaches helper functions to Image and Video properties to simplify constructing Dynamic Media URLs:
var ImageFormat = require('dc-delivery-sdk-js').ImageFormat;
var imageUrl =
content.body.imageProperty
.url()
.width(500)
.height(500)
.sharpen()
.format(ImageFormat.WEBP)
.build();
See the sdk reference documentation for further details.
Using the Content Rendering Service, you can convert the JSON content into any format you choose by applying a template previously setup in the back-office. This is typically used to convert content into fragments of HTML, XML or even rewrite the JSON.
client.renderContentItem('b322f84a-9719-42ff-a6a0-6e2924608d19', 'templateName')
.then(response => {
console.log(response.body);
})
.catch(error => {
console.log('unable to find content', error);
});
When displaying content you may need to detect the content type to decide which UI widget should be used to display the content.
Every content item in the body includes a built-in property _meta.schema which identifies the content type that was used to create that fragment of content. This can be used by your application to influence how the content is processed.
Example:
{
"_meta":{
"schema":"https://www.anyafinn.online/content-types/slot.json",
"deliveryId":"62ece7d6-b541-411c-b776-0a6704ede1fb",
"name":"homepage-hero"
},
"slotContent": {
"_meta":{
"schema":"https://www.anyafinn.online/content-types/banner.json",
"deliveryId":"28583572-c964-4755-825b-044718312a29",
"name":"example-banner"
},
"heading": "Free shipping until Sunday!"
}
}
import React from 'react'
import { Banner, Carousel, Empty } from './components'
class App extends React.Component {
//...
getComponentForContentType(contentItem) {
switch(contentItem._meta.schema) {
case 'https://www.anyafinn.online/content-types/banner.json':
return Banner;
case 'https://www.anyafinn.online/content-types/carousel.json':
return Carousel;
default:
return Empty;
}
}
render() {
const slotContent = this.props.content.slotContent;
const TagName = this.getComponentForContentType(slotContent);
return <TagName content={slotContent} />
}
}
Applications that support TypeScript can optionally create interfaces to represent content types within the code. This can be passed as a generic parameter when loading content which will result in a typed content body.
interface Banner extends ContentBody {
heading: string;
}
client.getContentItem<Banner>('ec5d12cc-b1bb-4df4-a7b3-fd7796326cfe');
If you have previously configured custom CNAMEs for your media hosting, you can override the hostname used by the sdk when constructing image URLs as shown below:
var client = new ContentClient({
account: 'myaccount',
mediaHost: 'images.mybrand.com',
secureMediaHost: 'images.mybrand.com'
});
Option | Description |
---|---|
account | Required: Account to retrieve content from. |
stagingEnvironment | If set, the SDK will request content and media from the staging environment host name specified. |
locale | If set, the SDK will request content using the locale settings provided. |
mediaHost | Allows users with custom hostnames to override the hostname used when constructing media URLs. |
secureMediaHost | Allows users with custom hostnames to override the hostname used when constructing secure media URLs. |
baseUrl | Override for the content delivery API base URL |
adaptor | Allows custom handling of requests which makes testing and supporting non-standard environments easier. |
Please use the following documentation resources to assist building your application:
- Dynamic Content SDK Reference documentation
- Dynamic Content Delivery API Reference documentation
- Dynamic Content User guide
If you need help using the sdk please reach out using one of the following channels:
- Ask a question on StackOverflow using the tag
amplience-dynamic-content
- Open a support ticket with Amplience Support
- Contact your Amplience Customer Success representative
- If you have found a bug please report it by opening an issue
This software is licensed under the Apache License, Version 2.0,
Copyright 2019 Amplience
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.