๐Ÿ“ฅList, Fetch, and Download Documents

This snippet mirrors the document retrieval recipe using 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

Last updated