Umbraco Heartcore
CMSCloudDXP
  • What is Umbraco Heartcore?
    • Compare with Umbraco CMS
  • Versions and updates
  • Getting Started
    • API Browser
    • Backoffice Users and API Keys
    • Building a project from scratch
    • Environments
    • GraphQL Playground
    • Preview
    • The Umbraco Cloud Portal
    • Tour of the Backoffice
    • Using the Forms API
    • Webhooks
    • Deployment workflow
      • Content and media transfer / restore
      • Structure deployments
  • API Documentation
    • API Documentation
      • Rate Limits
    • Content Delivery
      • Content
      • Media
      • Redirect API
    • Content Management
      • Umbraco Forms
      • Languages
      • Content
        • Content Types
      • Media
        • Media Types
      • Members
        • Member Groups
        • Member Types
      • Relations
        • Relation Types
    • GraphQL API
      • Filtering and Ordering
      • Property Editors
      • Schema Generation
      • Persisted Queries
  • Backoffice
    • Custom Grid Editors
  • Client Libraries
    • Client Libraries
    • .NET Core Console Application
    • Node.js Client library
    • .NET Client library
      • MVC Sample
      • Content Management Sample
  • Tutorials
    • Custom Grid Editors
    • Querying with GraphQL
    • Creating content with media
  • Release Notes
    • February 2024
    • April 2024
    • August 2024
    • September 2024
    • April 2025
Powered by GitBook
On this page
  • Prerequisites
  • Step 1: Create Content and Media in Your Heartcore Project
  • Step 2: Initialize a Node.js Project
  • Step 3: Install the Client Library
  • Step 4: Write the code to Fetch Content and Media
  • Step 5: Run the Script
  • Exploring API Methods
  • Content Delivery API
  • Content Management API
  • References

Was this helpful?

Edit on GitHub
Export as PDF
  1. Client Libraries

Node.js Client library

Learn how to fetch content and media from your Umbraco Heartcore project using Node.js and the Umbraco.Headless.Client.NodeJs Library.

Previous.NET Core Console ApplicationNext.NET Client library

Last updated 5 months ago

Was this helpful?

This article showcases how to fetch content and media from your Umbraco Heartcore project using the official Node.js Client Library. If you are not familiar with Node.js you can take a look at the official .

Prerequisites

  • (version 10 or above) installed on your machine. You can verify the version by running node -v in your terminal.

  • Access to an Umbraco Heartcore project.

  • An API key generated from your Heartcore project. For more information, see the article.

  • Basic familiarity with terminal commands and Node.js.

Step 1: Create Content and Media in Your Heartcore Project

  1. Log into your Heartcore project on the Umbraco Cloud Portal.

  2. Navigate to the Content section.

  3. Create a new content item, such as a "Home Page." Fill in the necessary fields and publish it.

  4. Go to the Media section.

  5. Upload an image.

  6. Note the content’s URL and media ID for fetching via the Node.js client.

Step 2: Initialize a Node.js Project

  1. Open a terminal or command prompt.

  2. Navigate to the directory where you want your project to reside.

  3. Run the following command to create a package.json file:

npm init -y

Step 3: Install the Client Library

There are two ways to install the Umbraco Headless Client Library:

  • Run the following command in your terminal:

npm install @umbraco/headless-client

Step 4: Write the code to Fetch Content and Media

  1. Create a new file called app.js using a text editor (such as Notepad++ or Visual Studio Code) in your project directory.

  2. Open the app.js file.

  3. Use the following code template:

 //Importing the Client Library
const { Client } = require('@umbraco/headless-client')

//Connecting to the Heartcore project on Cloud
const client = new Client({ 
    projectAlias: 'your-project-alias',
    apiKey: 'your-api-key'
})

//Create an async function to fetch content and media
async function run() {
    try {
        //Fetch all content from the root
        const contentResult = await client.delivery.content.root()

        //Fetch a media item by its ID
        const mediaResult = await client.delivery.media.byId('your-media-id')

        //Log results
        console.log('Content:', JSON.stringify(contentResult, null, 2))

        console.log('Media:', JSON.stringify(mediaResult, null, 2))

    } catch (error) {
        console.error('Error fetching data:', error.response ? error.response.data : error.message)
    }
}

run()

Step 5: Run the Script

  1. Open a terminal.

  2. Navigate to your project folder.

  3. Run the following command:

node app.js

Exploring API Methods

The Node.js library provides a variety of methods to fetch and manage data from your Umbraco Heartcore project. These methods allow you to interact with both the Content Delivery API and the Content Management API, depending on your requirements.

Content Delivery API

To fetch content or media, use the following convention:

client.delivery.content.root();

client.delivery.media.root();

client.delivery.content.ancestors(contentId);

client.delivery.content.children(parentId);

client.delivery.content.byId(contentId);

In the above examples:

  • Use root() to fetch all content or media from the root level.

  • Use children() or ancestors() to navigate the hierarchy and retrieve related content or media items. You can also fetch specific items directly by their ID or URL.

Content Management API

To manage content, media, and other types, use the following convention:

client.management.content.create()

client.management.contentType.all()

In the above examples:

  • Use create() to add new content.

  • Use all() to fetch all available content types.

References

Clone or download the client library from GitHub, or

For a full list of available methods, visit the .

For a full list of available methods, visit the .

Node.js Documentation
Node.js
Backoffice Users and API Keys
Umbraco.Headless.Client.NodeJs
Content Delivery API sample repository on GitHub
Content Management API sample repository on GitHub
Node.js Documentation
Umbraco Heartcore API Documentation
Create an Umbraco Heartcore project