Secure API Automation with Cloudflare Zero Trust (Part 2)

Part 2: Automating BookStack Content with Secure API Calls

Building on Part 1 where we discussed secure API access via Cloudflare Zero Trust and generic automation scripts, this part dives into technical automation for BookStack.

The goal: programmatically create books and assign them to shelves while keeping operations idempotent — running the script multiple times won’t create duplicates or overwrite existing content.

Setup Overview:

  • Configuration File (complete_books.json): Defines book names, descriptions, and target shelf IDs.
  • Bash Script: Uses curl for API calls, jq for JSON parsing, Cloudflare Access headers, and BookStack API tokens for authentication.
  • Temporary Storage: Two temp files store book IDs and shelf assignments to ensure accurate updates.

Workflow Steps:

  1. Validation:
    • Fetch all shelves and books from BookStack.
    • Exit with error if API calls fail, ensuring safe execution.
  2. Book Creation:
    • For each book in the JSON file:
      • Check if it already exists.
      • If missing, create the book and capture its ID.
    • Log success or errors for transparency.
  3. Shelf Assignment:
    • Group books by shelf ID.
    • Merge new books with existing shelf content, removing duplicates.
    • Update each shelf via the API with the consolidated list.
  4. Cleanup:
    • Temporary files are removed to keep the environment tidy.

Execution:

  • Make the script executable: chmod +x automate_books.sh
  • Ensure JSON and credentials are correctly set.
  • Run: ./automate_books.sh

Key Benefits:

  • Secure: Uses Cloudflare Zero Trust + API tokens for safe automation.
  • Idempotent: No duplicates, safe for repeated runs.
  • Scalable: Easily add new books in the JSON file without changing the script.
  • Auditable: Logs creation and update operations for traceability.

This approach complements the concepts from Part 1 and provides a practical, technical framework to automate and manage BookStack content efficiently.