Node.js Client library
In this article you will be able to read about our Node.js Client library. You will be able to see a code sample showing you how to create a client and fetch some data from an Umbraco Heartcore project.
You will have to have Node.js version 10 or above to be able to work with this client library.
You can either clone or download the Client library from Github or you can install it with npm.
npm install @umbraco/headless-client
Once you have installed the client library you can create a Client and start fetching data from a project. In the following sample we are fetching content from the root of a project and a media item targeted by its Id, and then logging the results in the console.
//Importing the Client Library
const { Client } = require('@umbraco/headless-client')
//Connecting to the Heartcore project on Cloud
const client = new Client({ projectAlias: 'demo-headless' })
//If protection is turned on you will have to add the API Key that has been asigned to your user
client.setAPIKey('')
//Create an async function to fetch all content from the root of the project
async function run() {
//Getting the content from the method where root is targeted
const contentResult = await client.delivery.content.root()
//Getting the content from the method where a media item is targeted by its Id
const mediaResult = await client.delivery.media.byId('b21f3fc4-7d8e-47f7-885b-65b770cb5374')
//Printing to the console as JSON with indentation for readability
console.log(JSON.stringify(contentResult, null, 2))
console.log(JSON.stringify(mediaResult, null, 2))
}
run()
The Node.js library consists of a long list of different methods you can use in order to fetch and manage data from your Umbraco Heartcore project.
The methods to use when fetching content or media from the Content Delivery API uses the following convention:
content.delivery.content.root()
content.delivery.media.root()
In the examples above all content / media from the
root
is called. You can also call specific content items by ID or URL, and you can even get related content items by calling e.g. children()
or ancestors()
. Find a full list of the available methods for the Content Delivery API on the sample repository on Github.When using the Content Management API to manage content, media and the various types in your Umbraco Heartcore project, you need to use the following convention:
content.management.content.create()
content.management.contentType.all()
In the examples above, the first methods shows how the Content Management API is called in order to create new content and the second methods gives an example of how to get a list of all available content types. Find a full list of the available methods for the Content Management API on the sample repository on Github.