Need help setting up my Node API

I created a headless starter project. I’m following these directions and I have…

  1. Add the following into your build.gradle file:
    dependencies {
    include “com.enonic.xp:lib-node:${xpVersion}”

  2. JavaScript controller, add a require statement: const nodeLib = require(’/lib/xp/node’);

      Which is my graphql.js located here:\src\main\resources\controllers
  3. Setup my event library

      Added the following to your `build.gradle` file:
         dependencies {
                 include "com.enonic.xp:lib-event:${xpVersion}"
      JavaScript controller, add a require statement:
                   const eventLib = require('/lib/xp/event');
        Which is my graphql.js located here:\src\main\resources\controllers
  4. Now I’m trying to setup connection to single repo. Where do I place this?

        var repo = nodeLib.connect({
             repoId: "com.enonic.cms.default",
            branch: "master"
  5. My goal is… after I setup my repo connection, to create 1,000 nodes with configuring this sample:
    var result1 = repo.create({ _name: "nerd", displayName: "Carpenter and IT expert", likes: "Plywood", numberOfUselessGadgets: 123 });

Enonic XP 7.6.0

Windows 10 10.0

  1. Where do I place this?

The nodelib.connect gives a connection to the main storrage.
If you want to create things then you can do that in the controller.

Just remember that the controller is executed everytime you visit a page.
So you might want to check if the content is created allready before adding a 1000 nodes.
Node apis create function is probably what you want to use.

Second addition:
Creating a 1000 nodes might take some time, and the controller renders the page so it might take a while. After you manage to get it working, you might want to take a look at services, then you could async load/create data.


So skip step 4?

If I move on to step 5. Where do I place

var result1 = repo.create({ _name: "nerd", displayName: "Carpenter and IT expert", likes: "Plywood", numberOfUselessGadgets: 123 });

Where do i place?

You can place it anywhere. I suggest (like above), to create a service that runs the controller code. Then you access it with anything that calls the service url. If you access the service url the service controller will run.

Another option is the main.js file. You can create a repo there, but you do need extra tests to make sure its not created multiple times. So check if the repo allready exists. Since main.js is run every time you deploy an app.

Thank for your quick responses. I should have specified that is the first time working in Enonic system API setup so the more specific the better.

When you say “I suggest (like above)” are you saying

1)create file " myservice.js"
2) inside that file " myservice.js" place this:

var result1 = repo.create({ _name: "nerd", displayName: "Carpenter and IT expert", likes: "Plywood", numberOfUselessGadgets: 123 });

  1. place, " myservice.js" here: src/main/resources/services/myservice/

  2. then deploy project
    5)check if the repo already exists. Do I do this with GraphiQL?

Thanks again for your help

Hi there @thrdcircle - To better help you out, it would be nice to understand what your objective is? Are you planning to create content via API, or are you looking to store user-generated data or other types of non-editorial data?

If you intend to create content, you should not use the node API, but the content API. The node API is a low level API that can store any type of data, and is completely schema-less. The content API however adds the CMS logic including content types to the process.

Also, the headless GraphQL API only works on top of the content API/CMS - it does not expose node-level data.

1 Like

Thanks. Sounds like we want Content API then.

My objective is to import 1,000 “records” with some fielded IDs (4-5 non-editorial) and some blank user-generated data fields (2html areas) that may be populate once in content studio. Content type would be a general article.

The purpose is exploring the ease of the API instead of typing 1,000 pieces of content manually. Once in the repo, exploring the workflow with that large volume.

Also, looking to scope out the runtime on creating 1,000 on how much a lift for the server timing.

Ok, so you have a content type and want to crate 1000 instances of it.
A quick approach is to use an app called contenthive: (I have not tested it lately, but it will generate content for you)

If you primarily want to explore creating content programmatically, there are essentially two approaches, via API, or via import. Imports are most commonly used after exporting from XP first.

The content API is documented here:
To practially have some code do this, you could create a service (as proposed by @pkw ) and trigger it by visiting the service endpoint.

Hope this helps?

Yes, this helps, I’m using the Content API but not seeing item after deploying project.

Here is my simple content-type “biotest.xml” placed here:src\main\resources\site\content-types\biotest

<display-name>Bio Test API</display-name>
<description>Bio template</description>
    <input name="education" type="TextLine" >

Here is my “local.js” placed here: src\main\resources\lib

  var contentLib = require('/lib/xp/content');
  var result1 = contentLib.create({
    name: 'mycontent',
    parentPath: '/proppub',
    displayName: 'My Content',
    contentType: 'contentapienonic:biotest',
    language: 'en',
    data: {
        education: Harvard,


    "attachments": {},
    "publish": {}
    });'Content created with id ' + result1._id);

Any thoughts? Not seeing any “ContentCreate” error in server log.

It is probably because the controller is never executed. You need to be able to trigger your “local.js” controller to run. There are many ways of doing this. You could for instance trigger it when the application starts, or by mapping it to an endpoint. The simplest way of doing this is probably to convert your controller to a service (just move it to a special services folder)

Here is a description of how services work: