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.
| COMPANIES_HOUSE_API_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
| COMPANIES_HOUSE_API_URL=https://api.companieshouse.gov.uk |
| 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') | |
| ); |