Test your API connection and verify PSC data
For local testing, you need to use a CORS proxy because browsers block direct API calls from local files.
# If you have Python installed: python -m http.server 8000 # Or with Node.js: npx http-server # Then open: http://localhost:8000
# Windows: chrome.exe --user-data-dir="C:/temp" --disable-web-security # Mac: open -n -a /Applications/Google\ Chrome.app --args --user-data-dir="/tmp/chrome" --disable-web-security # Linux: google-chrome --disable-web-security --user-data-dir="/tmp"
For production use, create a simple backend proxy:
// server.js
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.static('public'));
app.get('/api/company/:number', async (req, res) => {
try {
const response = await axios.get(
`https://api.company-information.service.gov.uk/company/${req.params.number}`,
{
headers: {
'Authorization': 'Basic ' + Buffer.from(process.env.API_KEY + ':').toString('base64')
}
}
);
res.json(response.data);
} catch (error) {
res.status(error.response?.status || 500).json({ error: error.message });
}
});
app.get('/api/company/:number/persons-with-significant-control', async (req, res) => {
// Similar implementation for PSC endpoint
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
API_KEY=your-companies-house-api-key-here
Add this to your main application:
// Companies House API Integration
class CompaniesHouseAPI {
constructor(apiKey, useProxy = false) {
this.apiKey = apiKey;
this.useProxy = useProxy;
this.baseURL = useProxy
? 'https://api.allorigins.win/raw?url=' + encodeURIComponent('https://api.company-information.service.gov.uk')
: 'https://api.company-information.service.gov.uk';
}
async getCompany(companyNumber) {
const url = `${this.baseURL}/company/${companyNumber}`;
return this.makeRequest(url);
}
async getPSCs(companyNumber) {
const url = `${this.baseURL}/company/${companyNumber}/persons-with-significant-control`;
return this.makeRequest(url);
}
async getOfficers(companyNumber) {
const url = `${this.baseURL}/company/${companyNumber}/officers`;
return this.makeRequest(url);
}
async makeRequest(url) {
try {
const response = await fetch(url, {
headers: {
'Authorization': 'Basic ' + btoa(this.apiKey + ':')
}
});
if (!response.ok) {
throw new Error(`API Error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('API Request failed:', error);
throw error;
}
}
}
// Usage in your app:
const api = new CompaniesHouseAPI('your-api-key', true); // true for local testing
async function verifyPSC(companyNumber, expectedName, expectedAddress) {
try {
const pscs = await api.getPSCs(companyNumber);
for (const psc of pscs.items) {
if (psc.name && psc.name.includes(expectedName)) {
const match = {
nameMatch: psc.name === expectedName,
addressMatch: compareAddresses(psc.address, expectedAddress),
actualData: psc
};
return match;
}
}
return { found: false };
} catch (error) {
console.error('Verification failed:', error);
return { error: error.message };
}
}