Only displaying 20 entries

I am trying to search for companies in a location and just printing to the screen at this stage suing the following code, However I only get 20 responses. Why is that?

import requests

Define API endpoint and query parameters

endpoint = “https://api.company-information.service.gov.uk/advanced-search/companies
params = {
“location”: “Suffolk”,
“company_status”: “Active”,
“items_per_page”: “100”, # Specify the number of results per page
“start_index”: “0”, # Specify the starting index for results
}

Make API request

headers = {
“Authorization”: “API_KEY”
}

response = requests.get(endpoint, params=params, headers=headers)

Check if the request was successful

if response.status_code == 200:
# Extract company numbers from the response
companies = response.json().get(“items”, [])
company_numbers = [company[“company_number”] for company in companies]
print(“Status code:”, response.status_code)
print(“Company Numbers in Suffolk:”)
print(company_numbers)
else:
print(“Failed to retrieve data. Status code:”, response.status_code)

Short: per the documentation for the Advanced Search endpoint you need the size parameter, not the items_per_page one for Advanced Search.

The API has a few places where there are inconsistencies including things like names of parameters or fields which “seem to be the same” being different, and this is one of them. The items_per_page parameter isn’t used in Advanced Search like it is in Search or other “paged” enpoints. So effectively you’ve been calling the endpoint without giving a size at all so Companies House API then just gives you some default data size.

I don’t know why things are like this but the Advanced Search was added some years after Search.

I’m not sure default data sizes are specified anywhere. It may not be important for you but if you were expecting a certain number of results I wouldn’t depend on this remaining the same. Always specify this and check the number of results you get back.

Hi @voracityemail, many thanks for taking the time to reply and explain, I really appreciate it. Is there a way to return all the results in the search or do I just specify a size larger than the expected number of results?

Well - yes and no.

Yes - if you look at the documentation it mentions paging - so as in other parts of the API you can pass e.g. start_index=0 and size=1000 to get a “page” of results, then increase start_index in multiples of size to get the next page(s). This is assuming Companies House returns 1000 results at a time (always count what you actually got back). Of course there may not be as many as you requested in total. I think it’s the hits parameter (appears to be a number within a string) that gives the total.

Also - no, in that it seems there’s a limit (10000 results?) above which Companies House currently return an error (http code 500). This may be deliberate in that Companies House have always said that the purpose of the API isn’t to download large chunks of the entire dataset - it’s for smaller more focussed queries. (See the Companies House Bulk data products for what you can get in bulk - note also that you don’t get quite the same data set that way.)

I haven’t checked this myself recently but see e.g. the thread below. (Also search the forum).

@voracityemail many thanks for taking the trouble to help me. It seems my situation will be resolved by a bulk download, followed by queries for changes over an intervening period of time.