# Companies house streaming API not working?

**URL:** <http://forum.companieshouse.gov.uk/t/companies-house-streaming-api-not-working/8405>\
**Category:** API Issues\
**Created:** [September 5, 2024, 9:53am UTC](http://forum.companieshouse.gov.uk/t/companies-house-streaming-api-not-working/8405 "2024-09-05T09:53:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfonseca](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dfonseca](http://forum.companieshouse.gov.uk/u/dfonseca)\
**Post date:** [September 5, 2024, 9:53am UTC](http://forum.companieshouse.gov.uk/t/companies-house-streaming-api-not-working/8405/1 "2024-09-05T09:53:57Z")

</div>

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 🙂

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](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 !

---

<div class="post-metadata">

**Author:** ![ebrian101](http://forum.companieshouse.gov.uk/user_avatar/forum.companieshouse.gov.uk/ebrian101/32/1145_2.png) [@ebrian101](http://forum.companieshouse.gov.uk/u/ebrian101)\
**Post date:** [September 9, 2024, 9:19am UTC](http://forum.companieshouse.gov.uk/t/companies-house-streaming-api-not-working/8405/2 "2024-09-09T09:19:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MArkWilliams](http://forum.companieshouse.gov.uk/user_avatar/forum.companieshouse.gov.uk/markwilliams/32/194_2.png) [@MArkWilliams](http://forum.companieshouse.gov.uk/u/MArkWilliams)\
**Post date:** [September 9, 2024, 2:49pm UTC](http://forum.companieshouse.gov.uk/t/companies-house-streaming-api-not-working/8405/3 "2024-09-09T14:49:21Z")

</div>

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](https://stream.companieshouse.gov.uk/filings)
