diff --git a/export_md/codimd_export.py b/export_md/codimd_export.py
index 45e2bff62ea273f8cbd9d1cea5682e101f386174..18a931e7a2557a11ea13202d762e7e2c0727e2bc 100755
--- a/export_md/codimd_export.py
+++ b/export_md/codimd_export.py
@@ -43,6 +43,8 @@ def export_from_codimd(instance_url, session_id, export_to):
     target_dir = prepare_target_dir(export_to)
     num_ok = num_fail = 0
     print_block_heading(f"Accessing history and trying to fetch each document")
+    with open(Path(target_dir, f"history.json"), mode="w") as stream:
+        json.dump(data, stream)
     print("Hold on, this may take a while...")
     for row in data["history"]:
         document_id = row["id"]
@@ -51,8 +53,6 @@ def export_from_codimd(instance_url, session_id, export_to):
             contents = slurp(f"{document_url}/download", session_id)
             with open(Path(target_dir, f"{document_id}.md"), mode="wb") as stream:
                 stream.write(contents)
-            with open(Path(target_dir, f"history.json"), mode="w") as stream:
-                json.dump(data, stream)
             num_ok += 1
         except HTTPError as error:
             # history might reference deleted or otherwise inaccessible notes