# List, Fetch, and Download Documents

This snippet mirrors the [document retrieval recipe](/quick-start/documents/retrieve-and-download.md) using Python.

```python
import os
import requests


token = os.getenv("AUTOCONTENT_TOKEN", "YOUR_API_TOKEN")
document_id = os.getenv("DOCUMENT_ID", "YOUR_REQUEST_ID")
base_url = "https://api.autocontentapi.com"
headers = {"Authorization": f"Bearer {token}"}


def extension_from_content_type(content_type: str) -> str:
    if "pdf" in content_type:
        return ".pdf"
    if "html" in content_type:
        return ".html"
    return ".txt"


list_response = requests.get(f"{base_url}/documents/get", headers=headers, params={"page": 1, "pageSize": 10}, timeout=30)
list_response.raise_for_status()
print("Documents page:")
print(list_response.json())

single_response = requests.get(f"{base_url}/documents/{document_id}", headers=headers, timeout=30)
single_response.raise_for_status()
print("Single document:")
print(single_response.json())

download_response = requests.get(f"{base_url}/documents/{document_id}/download", headers=headers, timeout=60)
download_response.raise_for_status()

extension = extension_from_content_type(download_response.headers.get("content-type", "text/plain"))
output_path = f"briefing-document-{document_id}{extension}"
with open(output_path, "wb") as file_handle:
    file_handle.write(download_response.content)

print(f"Saved {output_path}")
```

Set `YOUR_API_TOKEN` (or `AUTOCONTENT_TOKEN`) and `DOCUMENT_ID` before running.

**See also**

* [Scenario overview](/quick-start/documents/retrieve-and-download.md)
* [Python guide](/code-samples/language-quickstarts/python.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.autocontentapi.com/code-samples/documents/python/retrieve-and-download.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
