Hi,
Apologies if this is a stupid question (probably first of many), I am very new to API’s.
How are you all connecting to CH? From what I’ve read you can’t use localhost ( I’ve tried to change etc/hosts file but still not getting past CORS ) and you can’t go through a browser ( I tried uploading a basic index and js file to a domain)
I am stuck! Any comments/help is much appreciated.
Do you want to connect from within a browser using javascript or from code running on a desktop/server, which programming language would you prefer to use.
I connect using C#.
Hi Tim,
I don’t know whether it’s possible to directly connect to CH API from within a browser, to solve the CORS issue Companies House would have to make a change at their end to allow your domain to make cross origin requests.
It would be possible to write code that runs on your web server that calls CH API, and them make use of that on a web page on your website.
I don’t use javascript myself for this API but other people do. Here’s a thread from back in the day (2020) which looks like it should serve:
I think it’s always worth eliminating the basics as there are several things that can trip people up:
if at all possible simply make sure that Companies House will accept your username and password (API key plus empty string as that is what Companies House expects) AND the form of query you’re using. I always use curl for this as it’s about the simplest and most widespread tool for making an http(s) request (and examining what’s happening). You’ll probably find an example on any thread I’ve posted on…
I know you’ve said you’re stuck at CORS but I find it helpful to first establish that I definitely should be able to get a response back. After that it’s just making the language / environment / tool work!
You mention writing code running on the web server. I’d tend to recommend that as a) it’s simpler (no “call and response” that you get with CORS requests) and b) You don’t have to expose your API key to users, as you would if that code is running in the browser.
ensure you’re using the correct (sandbox or live) key / environment. I don’t really understand this, before my time but the advice seems to be “just use live” - I think there are some more helpful threads about this on this site.
ensure when you register your application with Companies House you’ve entered either the corret IP address(es) you’re connecting from OR (especially for javascript) the domain(s).
I think you can use localhost + javascript as per the information in this thread - which you may already have tried of course…