Can anyone help me with error 401?

I am trying to use react to connect to the api, but i am getting error 401.

my function is this
try {
const response = await fetch(apiUrl, {
method: ‘GET’,
mode: ‘no-cors’,
headers: {
‘Authentication’: authHeading
}
});
if (response.ok) {
const data = await response.json();
setResults(JSON.stringify(data, null, 2));
} else {
setResults(Error: ${response.status} - ${response.statusText});
}
} catch (error) {
setResults(Error: ${error});
}
};

Are you getting a CORS error (see e.g. here)?

Note that Companies House currently (for weeks) have a particular issue or issues with CORS e.g. see:

There is lots of information on this forum on general authentication and what to work through if you get a 401. (You can search the forum - magnifying glass icon in top right). See e.g. here:

im getting this error:
create.tsx:28

   GET https://api.company-information.service.gov.uk/company/03933476%E2%80%99 net::ERR_ABORTED 401 (Unauthorized)

Ive tried following the steps of adding my javascript domain to the ch site but i think its this thats causing it.

I’m not sure why you’ve got a URL-encoded closing single quote / apostrophe on the end of the URL you report there? That probably won’t help you, if that’s what you’re trying to send…

Given that apparently Companies House do have some ongoing CORS issues when calling via AJAX (what you’re doing with typescript / react) this complicates things. Many people are making apparently valid requests which are failing currently.

The standard pattern for investigating “401 issues” is:

a) If at all possible use a basic, well-known tool like Curl to send the simplest possible request to the server to ensure all is working and give you detailed feedback on the process. Curl is good for this, it’s very verbose. This is to check that:

  • the API key is correct
  • your application details are correctly registered with Companies House - as you mention you need to register the javascript domain, if using a local server there’s a workaround for that etc.), you should choose to create a live application not test/sandbox
  • the URL you are requesting is correct (starting with the request for a company profile is a good idea as there are no parameters to worry about).

b) Ensure you’ve understood HTTP Basic and how the Companies House API key fits into that (e.g. it is the username in this scheme, the password is blank / empty).

c) Ensure you understand how your programming language / environment works with http basic (if you’re doing a fetch it’s up to you to build the header correctly, other tools / environments often work differently).

Good luck.