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.
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 Node.js Documentation.
Prerequisites
Node.js (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 Backoffice Users and API Keys article.
Basic familiarity with terminal commands and Node.js.
Step 1: Create Content and Media in Your Heartcore Project
Log into your Heartcore project on the Umbraco Cloud Portal.
Navigate to the Content section.
Create a new content item, such as a "Home Page." Fill in the necessary fields and publish it.
Go to the Media section.
Upload an image.
Note the content’s URL and media ID for fetching via the Node.js client.
Step 2: Initialize a Node.js Project
Open a terminal or command prompt.
Navigate to the directory where you want your project to reside.
Run the following command to create a
package.json
file:
Step 3: Install the Client Library
There are two ways to install the Umbraco Headless Client Library:
Clone or download the Umbraco.Headless.Client.NodeJs client library from GitHub, or
Run the following command in your terminal:
Step 4: Write the code to Fetch Content and Media
Create a new file called
app.js
using a text editor (such as Notepad++ or Visual Studio Code) in your project directory.Open the
app.js
file.Use the following code template:
Step 5: Run the Script
Open a terminal.
Navigate to your project folder.
Run the following command:
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:
In the above examples:
Use
root()
to fetch all content or media from the root level.Use
children()
orancestors()
to navigate the hierarchy and retrieve related content or media items. You can also fetch specific items directly by their ID or URL.
For a full list of available methods, visit the Content Delivery API sample repository on GitHub.
Content Management API
To manage content, media, and other types, use the following convention:
In the above examples:
Use
create()
to add new content.Use
all()
to fetch all available content types.
For a full list of available methods, visit the Content Management API sample repository on GitHub.
References
Last updated