Back up published documents

Save the text of your published documents to files with a short script, and see which ones have changes since they were published.

Guide
About 15 minutes

What you'll do

Save each published document's text as a Markdown file. The script also tells you which documents have edits since they were published, so you know the file holds those edits too.

Before you start

  • An API key with documents:search, documents:text:read and registers:changes:read. See Create and manage API keys.
  • Python 3 on the computer that runs the script.

Steps

  1. Find each document's id by searching for its name or code.
bash
curl -X POST https://app.alchex.com/api/v1/tools/read_document/search \  -H "Authorization: Bearer EXAMPLE_API_KEY" \  -H "Content-Type: application/json" \  -d '{ "query": "Supplier exit plan" }'
  1. Copy the id from documents.candidates into a file named ids.txt, one id per line.
  2. Save this script as backup.py next to ids.txt.
python
import json, os, sys, urllib.request
KEY = os.environ["ALCHEX_API_KEY"]BASE = "https://app.alchex.com/api/v1/tools/read_document/"
def call(action, body):    request = urllib.request.Request(BASE + action, data=json.dumps(body).encode(), method="POST",        headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"})    with urllib.request.urlopen(request) as answer:        return json.load(answer)
os.makedirs("backup", exist_ok=True)for doc_id in open("ids.txt").read().split():    doc = call("document", {"documentId": doc_id})    if not doc.get("published"):        print("never published, skipped:", doc["title"])        continue    text = doc["content"]    # A long document comes in parts: ask for the next one until there is none.    while doc.get("continueFrom"):        doc = call("document", {"documentId": doc_id, "fromSection": doc["continueFrom"]})        text += "\n" + doc["content"]    with open(os.path.join("backup", doc_id + ".md"), "w", encoding="utf-8") as f:        f.write(text)    changes = call("changes", {"documentId": doc_id, "since": "published"})    if changes.get("changeCount"):        print("saved, with edits since publishing:", doc["title"])    else:        print("saved:", doc["title"])
  1. In a terminal, run the script with your key.
bash
ALCHEX_API_KEY=EXAMPLE_API_KEY python3 backup.py
  1. Open the backup folder and check one file per document.

What happens next

Each file holds the document's current text. Where the script says with edits since publishing, the file includes edits nobody has approved yet. Read them with read_document/changes, or wait until the next version is published and run the script again.

Each document costs a few requests. With many documents, the key may meet the limit of 60 a minute; see Rate limits, retries and cost.

If something goes wrong

404 not_found. The id is wrong, or the document is in no team the key reaches. Search for it again.

403 missing_permission. The key lacks the permission the message names. Create a key with all three.

A document is skipped as never published. It has no published version yet. Publish it in Alchex first.