A long-lived project that still receives updates
Client for the Companies House REST API. Provides company profiles and officer lists.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

CompaniesHouse::Client

GH Actions

This Gem implements an API client for the Companies House REST API. It can be used to look up information about companies registered in the United Kingdom. More information about this free API can be found on the Companies House API website.

To interact the older CompaniesHouse XML-based API, see the gem companies-house-gateway. (Monthly subscription fees, and other fees, may apply.)

Quick start:

export COMPANIES_HOUSE_API_KEY=YOUR_API_KEY_HERE
  • Install companies-house-rest through RubyGems
  • Create and use a client:
require 'companies_house/client'
client = CompaniesHouse::Client.new(api_key: ENV['COMPANIES_HOUSE_API_KEY'])
profile = client.company('07495895')

Overview

This gem is meant to provide a simple synchronous API to look up company profile information and company officers. The data returned is parsed JSON.

This gem provides information on companies by their Companies House company number. This "company number" is actually a string and should be treated as such. The string may consist solely of digits (including leading 0s) or begin with alphabetic characters such as NI or SC.

Authentication

Using the Companies House REST API requires you to register an account on the CompaniesHouse Developers website and configure an API key. Developers should read the Companies House developer guidelines before using this API, and will note that these guidelines contain several instructions regarding API keys:

  • Do not embed API keys in your code
  • Do not store API keys in your source tree
  • Restrict API key use by IP address and domain
  • Regenerate your API keys regularly
  • Delete API keys when no longer required

Client Initialization

All requests to the API are made through a client object:

require 'companies_house/client'
client = CompaniesHouse::Client.new(config)

The client is configured by passing a hash to the constructor. The supported keys for this hash are:

Key Description
:api_key Required. The API key received after registration.
:endpoint Optional. Specifies the base URI for the API (e.g. if using a self-hosted version)
:instrumentation Optional. Instruments the request/response (see Instrumentation for details)

Instrumentation

By default, no instrumentation is being applied. If you are using Rails or the ActiveSupport gem, instrumentation will happen automatically via ActiveSupport::Notifications

Requests

Once a client has been initialised, requests can be made to the API. Details of the available fields in the response are in the Companies House documentation. The endpoints currently implemented by the gem are:

Client Method Endpoint Description
.company(company_number) GET /company/:company_number Retrieves a company profile.
.officers(company_number) GET /company/:company_number/officers Retrieves a list of company officers.
.company_search(query, items_per_page: nil, start_index: nil, restrictions: nil) GET /search/companies Retrieves a list of companies that match the given query.
.persons_with_significant_control(company_number, register_view: false) GET /company/:company_number/persons-with-significant-control Retrieves a list of persons with significant control.
.persons_with_significant_control_corporate_entity_beneficial_owner(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/corporate-entity-beneficial-owner/:psc_id Retrieves a corporate entity beneficial owner.
.persons_with_significant_control_corporate_entity(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/corporate-entity/:psc_id Retrieves a corporate entity.
.persons_with_significant_control_individual_beneficial_owner(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/individual-beneficial-owner/:psc_id Retrieves an individual beneficial owner.
.persons_with_significant_control_individual(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/individual/:psc_id Retrieves an individual.
.persons_with_significant_control_individual_with_verification_state(company_number, psc_id). GET /company/:company_number/persons-with-significant-control/individual/:psc_id/verification-state Retrieves an individual with verification state.
.persons_with_significant_control_individual_full_record(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/individual/:psc_id/full_record Retrieves the full record of an individual.
.persons_with_significant_control_legal_person_beneficial_owner(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/legal-person-beneficial-owner/:psc_id Retrieves a legal person beneficial owner.
.persons_with_significant_control_legal_person(company_number, psc_id) GET /company/:company_number/persons-with-significant-control/legal-person/:psc_id Retrieves a legal person.
.persons_with_significant_control_super_secure_beneficial_owner(company_number, super_secure_id) GET /company/:company_number/persons-with-significant-control/super-secure-beneficial-owner/:super_secure_id Retrieves a super secure beneficial owner.
.persons_with_significant_control_super_secure_person(company_number, super_secure_id) GET /company/:company_number/persons-with-significant-control/super-secure/:super_secure_id Retrieves a super secure person.
.persons_with_significant_control_statements(company_number, register_view: false) GET /company/:company_number/persons-with-significant-control-statements Retrieves a list of persons with significant control statements.
.persons_with_significant_control_statement(company_number, statement_id) GET /company/:company_number/persons-with-significant-control-statements/:statement_id Retrieves a persons with significant control statement.
.filing_history_list(company_number) GET /company/:company_number/filing-history Retrieves a list of filing history items.
.filing_history_item(company_number, transaction_id) GET /company/:company_number/filing-history/:transaction_id Retrieves a specific filing history item.

.company

This method implements the readCompanyProfile API and returns the full companyProfile resource.

.officers

This method implements the officersList API. It will make one or more requests against this API, as necessary, to obtain the full list of company officers. It returns only the values under the items key from the officerList resource(s) which it reads.

.company_search

This method implements the searchCompanies API and returns the list of companySearch resources that match the given query. The items_per_page, start_index and restrictions parameters are optional.

.persons_with_significant_control

This method implements the listPersonsWithSignificantControl API and returns the list of personsWithSignificantControl resources.

.persons_with_significant_control_corporate_entity_beneficial_owner

This method implements the getCorporateEntityBeneficialOwner API and returns the corporateEntityBeneficialOwner resource.

.persons_with_significant_control_corporate_entity

This method implements the getCorporateEntities API and returns the corporateEntity resource.

.persons_with_significant_control_individual_beneficial_owner

This method implements the getIndividualBeneficialOwner API and returns the individualBeneficialOwner resource.

.persons_with_significant_control_individual

This method implements the getIndividual API and returns the individual resource.

.persons_with_significant_control_individual_with_verification_state

This method implements the getIndividualWithVerificationState API and returns the individual resource.

.persons_with_significant_control_individual_full_record

This method implements the getIndividualFullRecord API and returns the individual resource.

.persons_with_significant_control_legal_person_beneficial_owner

This method implements the getLegalPersonBeneficialOwner API and returns the legalPersonBeneficialOwner resource.

.persons_with_significant_control_legal_person

This method implements the getLegalPersons API and returns the legalPerson resource.

.persons_with_significant_control_super_secure_beneficial_owner

This method implements the getSuperSecureBeneficialOwner API and returns the superSecureBeneficialOwner resource.

.persons_with_significant_control_super_secure_person

This method implements the getSuperSecurePerson API and returns the superSecure resource.

.persons_with_significant_control_statements

This method implements the listPersonsWithSignificantControlStatements API and returns the list of statementList resources.

.persons_with_significant_control_statement

This method implements the getStatement API and returns the statement resource.

.filing_history_list

This method implements the filingHistoryList API and returns the full filingHistoryList resource.

.filing_history_item

This method implements the filingHistoryItem API and returns the full filingHistoryItem resource.

Other API Methods

While there are other resources exposed by the Companies House API, this gem does not implement access to these resources at this time.

Error Handling

If a request to the Companies House API encounters an HTTP status other than 200 OK, it will raise an instance of CompaniesHouse::APIError instead of returning response data. The error will have the following fields:

Field Description
response The Net::HTTP response object from the failed API call.
status A string containing the response status code.

Certain API responses will raise an instance of a more specific subclass of CompaniesHouse::APIError:

Status Error Description
401 CompaniesHouse::AuthenticationError Authentication error (invalid API key)
404 CompaniesHouse::NotFoundError Not Found. (No record of the company is available.)
429 CompaniesHouse::RateLimitError Application is being rate limited

The client will not catch any other errors which may occur, such as errors involving network connections (e.g. Errno::ECONNRESET).

Development

This gem is configured for development using a bundler workflow. Tests are written using RSpec, and Rubocop is used to provide linting. Bug reports and pull requests are welcome on this project's GitHub repository.

To get started:

bundle install --path vendor

To run all tests and Rubocop:

bundle exec rake