Monday, November 11, 2024

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally while maintaining control over access. Start by authenticating your API request using your credentials to gain access to the necessary endpoints. Once authenticated, you can use the API to create a public link for specific assets or content items by sending a POST request to the designated endpoint, typically `/api/links`. In your request body, include parameters such as the asset ID, link type, and any additional metadata you want to associate with the link. After the request is processed, the API will return a response containing the generated public link, which you can then distribute as needed. It’s important to configure the permissions and expiration settings appropriately to ensure that the link aligns with your organization's security policies. By following these steps, you can efficiently create and manage public links, facilitating easy access to content while leveraging the robust capabilities of the Sitecore Content Hub REST API.  

In this blog, we will walk through how to create public links for assets, how to retrieve the public URL, and how to set expiration dates for those links to help you maintain full control over the shared content.

Content Hub allows us to create public links and integrate them with any type of web application/page.

This blog helps you to create public links using REST API.

Step 1: Authenticating and Creating a Public Link for an Asset

The first step in creating a public link is authenticating your API request. Authentication ensures that only authorized users or applications are able to interact with the Sitecore Content Hub's API. Once authenticated, you’ll be able to create a public link for a specific asset or entity.

API Request to Create Public Link

To create a public link for your selected asset, make a POST request to the following API endpoint:

  • URL: {{ChUrl}}/api/entitydefinitions/M.PublicLink/entities
  • Method: POST
  • Headers: Content-Type: application/json
  • Body (Raw JSON format):
json:
{
	"properties": {		
		"Resource": "downloadOriginal"		
	},
	"is_root_taxonomy_item": false,
	"is_path_root": false,
	"inherits_security": true,
	"entitydefinition": {
		"href": "{{ChUrl}}/api/entitydefinitions/M.PublicLink"
	},
	"relations": {
		"AssetToPublicLink": {
			"parents": [
				{
				"href": "{{ChUrl}}/api/entities/{entityid}"
				}
			]
		}
	}
}


Explanation of Parameters:

  • Resource: Defines the specific resource, such as the original download link, that is being shared.
  • is_root_taxonomy_item: Specifies whether the asset is a root taxonomy item.
  • inherits_security: Ensures that security settings (such as user permissions) are inherited by the public link.
  • entitydefinition: Refers to the entity definition for the type of public link.
  • relations: Links the public link to the selected asset using the entity ID.

Replace {{ChUrl}} with your Content Hub URL and {entityid} with the asset's unique entity ID. After sending the POST request, the API will return a successful "201 Created" response code and provide you with an identifier for the newly created public link.

Expected API Response:

If the request is successful, you will receive a response containing the public link's identifier:

{
    "id": 89358,
    "identifier": "iAOnXWcvmk6cQw"
}


Step 2: Retrieve the Public Link URL

Once the public link is created, you can retrieve the actual public URL by using the identifier you obtained in the previous step. This public URL allows you to share the asset with external parties or integrate it into other applications.

API Request to Get the Public Link URL

  • URL: {{ChUrl}}/api/entities/identifier/{identifier}
  • Method: GET

Replace {identifier} with the identifier returned from the previous API request.

Example Response:

The API will return a response with the generated public link for your asset.

{ "id": 89358, "publicLink": "https://yourcontenthuburl.com/publiclink/iAOnXWcvmk6cQw" }

With the public link, you can now easily distribute the content to your intended users or integrate the link into web applications.

Step 3: Set an Expiration Date for the Public Link

One of the key benefits of using the Sitecore Content Hub API to create public links is the ability to control the lifespan of these links. You can set an ExpirationDate to specify when the public link should no longer be valid. This feature helps enhance security by automatically revoking access to shared assets after a set period, reducing the risk of unauthorized access.

Example Request with Expiration Date:

{ "properties": { "Resource": "downloadOriginal", "ExpirationDate": "2021-11-15T10:18:45.220Z" }, "is_root_taxonomy_item": false, "is_path_root": false, "inherits_security": true, "entitydefinition": { "href": "{{ChUrl}}/api/entitydefinitions/M.PublicLink" }, "relations": { "AssetToPublicLink": { "parents": [ { "href": "{{ChUrl}}/api/entities/{entityid}" } ] } } }

  • ExpirationDate: Set this value in ISO 8601 format (YYYY-MM-DDTHH:MM:SS.mmmZ) to define the exact time when the public link should expire.
By specifying an expiration date, you ensure that content access is temporary and controlled. After the expiration date, the link will become invalid, and users will no longer be able to access the content.                    
                        

Importance and Benefits of Creating Public Links

Using public links through the Sitecore Content Hub REST API offers several advantages:

  1. Controlled Sharing: You can share content externally without compromising internal security. The ability to set expiration dates ensures that shared links are only valid for a limited time, reducing the risk of unauthorized access.

  2. Ease of Integration: Public links can be easily integrated into external web applications, client portals, or even marketing campaigns. This allows for seamless content distribution while maintaining centralized control over assets.

  3. Security and Compliance: Setting permissions and using expiration dates for links helps ensure that your organization remains compliant with security policies and regulatory requirements, ensuring only authorized access to sensitive content.

  4. Optimized User Experience: The public link feature enables organizations to share assets with external users or teams without the need for them to log into the Content Hub. This makes content more accessible, enhancing collaboration with external stakeholders and partners.

  5. Improved Workflow Efficiency: Automating the process of generating public links through API requests minimizes manual intervention, making the content sharing process faster and more efficient.

By leveraging Sitecore Content Hub's REST API to create public links, you can efficiently share content with external parties while maintaining strict control over access and security. The ability to set expiration dates further enhances security, ensuring that links are only valid for as long as needed.

Whether you're sharing assets for marketing, collaboration, or external distribution, public links make it easier to manage access and content flow within and outside your organization.

With Sitecore Content Hub, you can combine flexibility, security, and ease of use to enhance the way you manage and distribute content.

                                            Happy learning and sharing your assets securely!

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...