Umbraco-Headless-Client-NodeJs

NodeJs version 8.11.2 LTS or later is recommended but it might work fine with previous releases as well.

Getting started

Install the umbraco-headless package through npm.
Create a new empty folder and type the following npm commands

npm init
npm install umbraco-headless

Get access to the api by including it in your project and supplying site and
credentials:

var UmbracoHeadless = require('umbraco-headless');
// this will change when proper token authentication is addedvar config = {
url: "https://domain.s1.umbraco.io",
username: "email@umbraco.com",
password: "secret",
imageBaseUrl: "https://domain.s1.umbraco.io"
};
// to use async / await we must run all code inside a async function
async function run(){
// create a new instance of the clientvar headlessService = new UmbracoHeadless.HeadlessService(config);
// the client will implicitly authenticate if you don't do it manually
await headlessService.authenticate();
// client is connected and ready// get the site// NOTE: this currently will not work without a content item as it needs to be implemented.// getSite will only work when called WITH a content item argument (getting the ancestor site of that content item).// var site = await headlessService.getSite(content);// console.log("site name: " + site.name);// until implemented - get the site using a query or a Id.// get a specific item by idvar site = await headlessService.getById(1000);
// the returned node contains all properties
console.log("my custom property:", site.myPropertyAlias);
// it can also be used to navigate byvar firstChild = await headlessService.getFirstChild(site);
// multiple results will be wrapped in a paged resultvar children = await headlessService.getChildren(site);
console.log("total results: ", children.totalResults);
// results on the page can be iterated in the .results array
console.log("first result of this paged result: ", children.results[0]);
// get a content item by url// NOTE: not implemented yet!// var contentByUrl = await headlessService.getByUrl("/my-content");
}
// run the async function
run();

Copy the entire code above into a file called index.js and run the file with node:

node .

Using VS Code?

If you are using VSCode as your editor then you can easily set up the editor to debug and easily inspect the values returned from the Umbraco Headless site by configuring the .vscode/launch.json file like so

Our.umbraco.com is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community.
This site is running
Umbraco version 7.13.1