Skip to content
Home » Content Hub and REST API example

Content Hub and REST API example

In my previous blog here, there is an example of using Web Client SDK to get assets from Content Hub and generate an Excel report to filter by file extension. This blog has the same functionality but uses REST API methods instead of Web Client SDK.

The GitHub source code for this example is here. This has been created on Visual Studio 2022 and .NET 6.0. The solution uses these two Nugget packages Newtonsoft.Json 13.0.3 and ClosedXML version 0.101.0.

The prerequisite is the API Key for authentication on the execution of REST API services. It can be obtained from the Users option in the Content Hub Manage screen.

The key icon generates the API token for a specific user.

Click on the “New token” option.

Once it is generated, the following screen is displayed with the new token generated. Save this value that will be used later.

First, I will show you the execution of an API service using Postman. This is a utility to call any REST API service with a POST or GET method and retrieves data. The following example gets all assets from Content Hub with this query and GET method:

https://[Content Hub sandbox]/api/entities/query?query=Definition.Name=='M.Asset'

The API token generated previously must be copied on the Header on a new entry called “X-Auth-Token”.

On the results, as you see below the data required for this blog are Id, FileName, Title, and extension.

One of the assets returned on Postman is with Id 29496. It is possible to inspect that asset directly on Content Hub with that Id.

Bellow it is indicated some parts of the code in Visual Studio. The class ContentHubService contains the definition to connect to the Content Hub sandbox URL on the BaseAddress property. The API Token generated previously must be copied on the “x-auth-token” header attribute.

The method GetAssetsInner executes the initial query “/api/entities/query?query=Definition.Name==’M.Asset'”

var response = await client.GetAsync(query);
if (response.IsSuccessStatusCode)
{
     var stringResponse = await response.Content.ReadAsStringAsync(); 
     //Convert the response to an AssetModel object
     return JsonConvert.DeserializeObject<AssetModel>(stringResponse);
}
else
{
    throw new HttpRequestException(response.ReasonPhrase);
}

The results obtained by the API are paginated(25 entities). The results returned by the initial query have the next page value as bellow.

For that reason the code gets the value indicated above in Postman and its equivalence in Visual Studio on the property “assetInitial?.Next?.Href” to call recursively the method GetAssetsInner until retrieves all the results.

if (assetInitial != null && assetInitial?.Next?.Href != null)
{
    assets.AddRange(assetInitial.Items);
    await GetAssets(assetInitial.Next.Href, assets);
}

The model called AssetModel was created with inner objects to map the structure returned by REST API service/query.

public class AssetModel
{
        public List<AssetModelDetail> Items { get; set; }
        public int Total_Items { get; set; }
        public int Returned_Items { get; set; }
        public AssetModelNavigation Next { get; set; }
        public AssetModelNavigation Previous { get; set; }
        public AssetModelNavigation Self { get; set; }
}

The casting of the API Rest result with the model is in this line using the Newtonsoft.Json library.

var stringResponse = await response.Content.ReadAsStringAsync(); 
//Convert the response to an AssetModel object
return JsonConvert.DeserializeObject<AssetModel>(stringResponse);

Once it is executed the example from visual studio, the following screen will be displayed.

The process to get the Excel file is obtained by clicking on Execute button.

One example of the Excel file is the following one.

Now you can see an example to see the differences between Web Api SDK and Rest API usage. The official documentation of REST API is here. See you on the next blog.