Companies house streaming API not working?

Hello, I am new to the companies house streaming API and I can’t seem to get it working. Here is my code, please let me know if I have made some obvious mistakes :slight_smile:

import requests
import base64

api_key = ‘my api key’

api_key_with_colon = f"{api_key}:"
print(api_key_with_colon)

encoded_credentials = base64.b64encode(api_key_with_colon.encode(‘ascii’)).decode(‘ascii’)
print(encoded_credentials)

headers = {
‘Authorization’: f’Basic {encoded_credentials}’
}
print(headers)

The streaming endpoint URL

url = ‘https://stream.companieshouse.gov.uk/filings

stream_timeout = 600

response = requests.get(url, headers=headers, stream=True, timeout=stream_timeout)

if response.status_code == 200:
print(‘Connection successful, starting stream…’)
for json_line in response.iter_lines():
if json_line:
print(json_line.decode(‘utf-8’)) # Decoding from bytes to string
else:
print(‘Empty pulse’)
else:
print(f"Connection failed with status code {response.status_code}. Response: {response.text}")

Every time I run this code i get this error and I am not sure why.

Connection failed with status code 401. Response: {“error”:“Invalid Authorization”,“type”:“ch:service”}

Any help would be greatly appreciated, Thankyou !

Your header construction code looks correct. Is your API key for the live environment? Don’t try with a test key

1 Like

We always advise to use a simple command line ‘curl’ from the same server that you are running your code. e.g.
curl -vv -k -u <API_key>: https://stream.companieshouse.gov.uk/filings