# How to get document\_id?

**URL:** <http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706>\
**Category:** API Question\
**Created:** [November 1, 2019, 12:26pm UTC](http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706 "2019-11-01T12:26:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex\_g](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@alex\_g](http://forum.companieshouse.gov.uk/u/alex_g)\
**Post date:** [November 1, 2019, 12:26pm UTC](http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706/1 "2019-11-01T12:26:12Z")

</div>

I want to get documents from a company using the API, I can find them on the filing history API however they don’t have document IDs therefore making it impossible to use the document API to download them.

I tried both the transaction ID and the XXX of the document metadata url “[https://frontend-doc-api.companieshouse.gov.uk/document/XXX](https://frontend-doc-api.companieshouse.gov.uk/document/XXX)” but these gave 500 internal server error.

Thus leading me to my question:

How do I obtain the document ID in order to find it in the document API

or (assuming the XXX in the above is correct)  
Why am I receiving 500 errors, is this an internal problem? (I’ve tried it over a number of days as initially I thought it was a problem with the API however i’ve seen no mention of internal issues and I would have thought if there were issues they would have been fixed after a few days).

Thanks

---

<div class="post-metadata">

**Author:** ![mariya\_garkavenko](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mariya\_garkavenko](http://forum.companieshouse.gov.uk/u/mariya_garkavenko)\
**Post date:** [January 23, 2020, 1:22pm UTC](http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706/2 "2020-01-23T13:22:46Z")

</div>

Hello! I am having the same problem. Have you found any solution?

---

<div class="post-metadata">

**Author:** ![alex\_g](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@alex\_g](http://forum.companieshouse.gov.uk/u/alex_g)\
**Post date:** [January 23, 2020, 1:47pm UTC](http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706/3 "2020-01-23T13:47:45Z")

</div>

Yeah, what they don’t tell you is the authentication for the document API works in a completely different way than the rest for no apparent reason. This is what you need (in python):

```
DOCUMENT_API_URL= https://%s:@document-api.companieshouse.gov.uk/document/%s/content

REQUEST_PARAMS_FOR_DOC_API = {'Accept': 'application/xhtml+xml'}

DOCUMENT_API_URL = DOCUMENT_API_URL % (api_key, document_id)

raw_response = requests.get(DOCUMENT_API_URL, params=REQUEST_PARAMS_FOR_DOC_API)

```

Note this assumes you actually want xhtml-xml documents, if you want pdf you give it something else in the params, I don’t know what because I didn’t want pdfs. Also fun fact, even when you ask for xhtml+xml documents the API sometimes ignores you completely and gives you PDFs, so I just put a while loop to try 4 times to make sure the output in the format I want then abandon the attempt if they keep sending PDFs and try again later.

Good luck

---

<div class="post-metadata">

**Author:** ![mariya\_garkavenko](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@mariya\_garkavenko](http://forum.companieshouse.gov.uk/u/mariya_garkavenko)\
**Post date:** [January 23, 2020, 2:19pm UTC](http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706/4 "2020-01-23T14:19:43Z")

</div>

Thanks a lot!

> [@alex\_g](#):
>
> Also fun fact, even when you ask for xhtml+xml documents the API sometimes ignores you completely and gives you PDFs, so I just put a while loop to try 4 times to make sure the output in the format I want then abandon the attempt if they keep sending PDFs and try again later.

LOL 😂

---

<div class="post-metadata">

**Author:** ![voracityemail](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/v/b77776/32.png) [@voracityemail](http://forum.companieshouse.gov.uk/u/voracityemail)\
**Post date:** [January 24, 2020, 6:05pm UTC](http://forum.companieshouse.gov.uk/t/how-to-get-document-id/2706/5 "2020-01-24T18:05:01Z")

</div>

> [@alex\_g](#):
>
> raw\_response = requests.get(DOCUMENT\_API\_URL, params=REQUEST\_PARAMS\_FOR\_DOC\_API)

> [@alex\_g](#):
>
> Also fun fact, even when you ask for xhtml+xml documents the API sometimes ignores you completely and gives you PDFs, so I just put a while loop to try 4 times to make sure the output in the format I want

Hmm…I’ve not experienced this - also what’s written above may be a typo but I think what you need is to specify the type you want in the _http headers_, not _URI parameters_. Python request.get seems to use “headers” for http headers and not “params” as written above as per [https://2.python-requests.org/en/master/user/quickstart/#custom-headers](https://2.python-requests.org/en/master/user/quickstart/#custom-headers)  
At least using the http header works for me - and [CH say it in the document library documentation](https://developer.companieshouse.gov.uk/document/docs/document/id/content/fetchDocument.html). (note “If the Content-Type is unsupported, a 406 error will be generated.”)

I think this should be (note - I’m not a pythonista):

DOCUMENT\_API\_URL= (exactly what you get from the [filing history response](https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/filingHistoryList-resource.html) links → document metatdata )

REQUEST\_HEADERS\_FOR\_DOC\_API = {‘Accept’: ‘application/xhtml+xml’} (or whatever type you want which is listed in the document metadata resource section)

raw\_response = requests.get(DOCUMENT\_API\_URL, auth=(‘YOUR\_API\_KEY’, ‘’) headers=REQUEST\_HEADERS\_FOR\_DOC\_API)

Note I’ve altered this so you’re not using the [https://user](https://user):password@… syntax - as mentioned e.g. in the [URL  
wikipedia article](https://en.wikipedia.org/wiki/URL) this is deprecated (“Use of the format username:password in the userinfo subcomponent is deprecated for security reasons.”). The “auth” part is using a blank password as that’s how CH have organised things.

So - after you’ve checked what formats are actually provided by checking the “resources” section returned by the [document metadata endpoint](https://developer.companieshouse.gov.uk/document/docs/document/id/fetchDocumentMeta.html) then set your [http “Accept” header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) accordingly. Note - the vast majority of documents are only filed as PDF format (“application/pdf”).

There’s an overview of some of the pitfalls of getting documents here in my comments (I’d rather CH made theirs a bit clearer themselves but hope this helps).

> [@Document API Implementation](https://forum.aws.chdev.org/t/document-api-implementation/1927/5):
>
> Good that you managed to answer your own question e.g. how to download documents for a given company number e.g.: [Get the filing history](https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/getFilingHistoryList.html) For a given filing in the list, locate the [“links” object, “document\_metadata” member](https://developer.companieshouse.gov.uk/api/docs/company/company_number/filing-history/filingHistoryList-resource.html) to get the metadata link [Request the metadata link](https://developer.companieshouse.gov.uk/document/docs/document/id/fetchDocumentMeta.html) to get the [document metadata object](https://developer.companieshouse.gov.uk/document/docs/document/id/documentMetaData-resource.html). Select your chosen data format (pdf, xml etc.), set the appropriate “Accept” http header, add “/content” on the end of the metadata link and request this to get the actual file. So you’r…
