Javascript: Getting Started

Hi I’m very new to this and as part of my learning process I’m starting from the ground up in order to give myself the best understanding of connecting to this API.

I’ve trawled through Google but have hit a wall.

I found this post in the forum with a really helpful snippet of JS using fetch which I feel is a good starting point:

var url = 'https://api.companieshouse.gov.uk/company/12345678';
var key = 'MyAPIKey';
var headers = new Headers ({
  'Authorization': 'Basic ' + btoa(key),
  'Content-Type': 'text/json'
});
var obj = {
  mode: 'no-cors',
  method: 'GET',
  headers: headers
};
fetch(url, obj)
.then((resp) => resp.json())
.then(data => console.log(data))
.then(function(res) {
  return res.json();
})
.then(function(res) {
  console.log(res);
})
.catch(function(err) {
  console.log(err);
});

I’m this with my API key trying both CORS and NO-CORS but returning only this error:

GET https://api.companieshouse.gov.uk/company net::ERR_ABORTED 401
  (anonymous) @ VM15:12
SyntaxError: Unexpected end of input
  at <anonymous>:13:22

I’ve also tried embedding into my page ensuring that I have registered the appropriate JS domains but get a similar 401 error.

Any assistance or advice would be appreciated.

EDIT: Added company number to code snippet