Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Last active August 7, 2023 16:34
Show Gist options
  • Save ultim8k/7f30ebdaac0b95cdef4da8d2070b0ab4 to your computer and use it in GitHub Desktop.
Save ultim8k/7f30ebdaac0b95cdef4da8d2070b0ab4 to your computer and use it in GitHub Desktop.
COMPANIES_HOUSE_API_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
COMPANIES_HOUSE_API_URL=https://api.companieshouse.gov.uk

Testing Companies House API

ATTENTION

The API keys do not work if you create an application in a development status. Make sure you create an app for production and then use that to generate API keys. It will save you a few hours tinkering with failed authentication responses.

import express from "express";
import cors from "cors";
import dotenv from "dotenv";
dotenv.config({ path: ".env" });
const companiesHouseApiUrl = process.env.COMPANIES_HOUSE_API_URL || "";
const companiesHouseApiKey = process.env.COMPANIES_HOUSE_API_KEY || "";
const searchNameInCompaniesHouse = async () => {
const headers = new Headers();
headers.set(
"Authorization",
`Basic ${Buffer.from(companiesHouseApiKey + ":").toString("base64")}`
);
return fetch(`${companiesHouseApiUrl}/search/companies?q=${name}`, {
headers,
}).then((response) => response.json());
};
const app = express();
app.use(cors());
app.get("/", (req, res) => {
try {
const companiesHouseResponse = await searchNameInCompaniesHouse(
"Blucher Horses Limited"
);
res.status(200).json({
companies: companiesHouseResponse,
});
} catch (err) {
res.status(500).json({
message: err?.message
});
}
});
app.listen(serverPort, () =>
console.log('Server running on http://localhost:3000')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment