Interacting with a CMS API

I want to use the Management API of my CMS to create entries when users fill in a Netcall form.
I have tried using a Fragment Callback and my code is like this:

return {
  main: function (myObj) {

// Secrets redacted!
const PROJECT = 'website';
const id = '';
const shared =  '';

// Get an authorization token.
async function getAuth() {
  let auth = fetch(
    'https://cms-chesheast.cloud.contensis.com/authenticate/connect/token',
    {
      method: 'post',
      body: `grant_type=client_credentials&client_id=${id}&client_secret=${shared}&scope=Entry_Read ContentType_Read Project_Read Entry_Write`,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Accept: 'application/json',
      },
    }
  )
    .then((response) => response.json())
    .then((data) => {
      return data.access_token;
    });
  return auth;
}

async function sendComment(entry) {
  let response = getAuth().then((auth) => {
    fetch(
      'https://cms-chesheast.cloud.contensis.com/api/management/projects/website/entries',
      {
        method: 'post',
        headers: {
          Authorization: 'bearer ' + auth,
          'Content-Type': 'application/json; charset=utf-8',
        },
        body: JSON.stringify(entry),
      }
    );
  });
}

// Create an entry.
const values = myObj.get_values();
let date = new Date();
let msg = values[cs.ref('TestEntryCreation')];
let newEntry = {
  myComment: msg,
  dateAndTime: date,
  sys: {
    contentTypeId: 'testComment',
    projectId: PROJECT,
    language: 'en-GB',
    dataFormat: 'entry',
  },
};
sendComment(newEntry);

  }
}

It isn’t working although the same code works in a local environment.
Any help appreciated.
Mark

1 Like

Morning Mark,

I would favour an event action for this, as these can operate in the background and would not re-run for each page refresh or field change.

Check the detective as it may be complaining about restricted access to the contensis domain, be sure that the specific sub-domain is added to the firewall rules, or *.contensis.com is specified.

Let me know if you continue to have issues.

1 Like