Content APIs (JavaScript)
This getting started tutorial walks you through a simple scenario to retrieve a content item by ID from the delivery content service and the authoring content service. You'll learn how to call unauthenticated and authenticated APIs. By the end of the tutorial, you can make your own calls and retrieve responses from the Acoustic Content APIs.
Before you begin
Get familiar with the Acoustic Content APIs
- Content offers you REST-based APIs to create, manage, and deliver your content. For example, use the authoring services API to create and manage content and the delivery services API to integrate content into your applications.
- The Content REST-based APIs can be called from standard browser-based
xmlHttpRequest (xhr)
support. - The format of the Content REST API URLs is
{API URL}/{ API endpoint}
.
The API URL is constructed asDomain name/{path}
where{path}= api/{Content ID}
.
The API endpoints are described in the Content API documentation. For example, use endpoint/delivery/v1/content/{Item ID}
or endpoint/authoring/v1/content/{Item ID}
to access content items. - Most of the APIs require you to authenticate through the
"/login/v1/basicauth"
endpoint before you can access them except for delivery APIs, which are available to anonymous, unauthenticated callers.
Example: To retrieve a published content item by ID
- Get the Item ID from the Content user interface. Open the specific content item that you want and click the API information.
ItemId = "b7abe31d-7763-41a9-b8d5-f7cf78565cbd"
- Provide the Item ID in the delivery API: {APIURL}/delivery/v1/content/{Item ID}.
https://content-XX-N.content-cms.com/api/12345678-...-56789abcdef0/delivery/v1/content/'b7abe31d-7763-41a9-b8d5-f7cf78565cbd'
Get the API URL for your tenant
You can get your API URL for your tenant from the Content Authoring UI.
Click Developer > URL information. Your API URL would look similar to API URL : https://{Domain name}/api/{ContentID}
For example, https://content-XX-N.content-cms.com/api/12345678-9abc-def0-1234-56789abcdef0
.
Retrieve content from delivery content service
Use the sample Content API JS helper to retrieve content from delivery content service
The sample wchhelper.js
provides a simple wrapper that you can use to construct an instance of the helper for a specific tenant API URL. The sample wchhelper.js makes the REST API calls, handles the xhr
request, and the JSON parsing of the response for you.
- Download the sample wchhelper.js helper to a local web application project file as js/wchhelper.js
- Load it with
<script src="js/wchhelper.js">
- Construct an instance of the sample helper with the tenant-specific API URL. For example:
let options = {
url: "https://{Domain name}/api/{ContentID}",
debug: true
};
wchHelper = new WchHelper(options);
- Use the helper to retrieve and display the JSON response for a content item. For example:
contentId = 'b7abe31d-7763-41a9-b8d5-f7cf78565cbd';
wchHelper.getDeliveryContentById(contentId)
.then(content => { alert("Delivery Content response: " + JSON.stringify(content, null, ' ')) })
.catch(err => { alert("getDeliveryContentById encountered an error: " + err); });
Retrieve content from authoring content service
Use the sample Content API JS helper to log in and retrieve content from authoring content service
You must log in to Content to use the Authoring service API as an authenticated user with a role as Editor, Manager, or Admin. You can use the sample wchhelper.js to log in.
- Construct an instance of the sample helper with the tenant-specific API URL. For example:
let options = {
url: "https://{Domain name}/api/{ContentID}",
debug: true
};
wchHelper = new WchHelper(options);
- Make a call and pass the user name and password for the Content user to log in.
- After successfully logging in, the wchhelper.js calls to retrieve and display the JSON response for an authoring content item. For example:
contentId = 'b7abe31d-7763-41a9-b8d5-f7cf78565cbd';
wchHelper.login('[email protected]', 'myPassword')
.then(tenantId => wchHelper.getAuthoringContentById(contentId))
.then(acontent => { alert("Authoring Content response: " + JSON.stringify(acontent, null, ' ')) })
.catch(err => { alert("getAuthoringContentById encountered an error: " + err); });
Updated almost 2 years ago