Scripting API - Diagrams (JavaScript)

The Structurizr web application provides a small JavaScript API that can be used to script and automate certain tasks. This is available under a JavaScript variable called structurizr.scripting when viewing the diagram viewer/editor page.

Free and paid plans

The following functions are provided for all plans (free and paid), and for Structurizr Express (where applicable).

  • isDiagramRendered(): Returns true when the diagram has been rendered, false otherwise.
  • setAutoLayout(bool): Sets whether an auto-layout algorithm should be used when rendering diagrams.
  • exportCurrentDiagramToPNG(options): Exports the current diagram to PNG, and returns the resulting base64 data URI. The optional options parameter is an object, which allows you to crop the image to show just the diagram content, { crop: true }.
  • exportCurrentDiagramKeyToPNG: Exports the current diagram key to PNG, and returns the resulting base64 data URI.
  • exportCurrentDiagramToSVG(options): Exports the current diagram to SVG, and returns the resulting SVG markup. The optional options parameter is an object, which allows you to include the double-click handlers for zooming in and opening an element's URL, { interactive: true }.
  • exportCurrentDiagramKeyToSVG: Exports the current diagram key to SVG, and returns the resulting SVG markup.
  • getViews(): Gets an array of objects representing the views in the software architecture model.
  • changeView(viewKey): Displays the specified view.
  • getViewKey(): Gets the key of the current view.
  • onWorkspaceSaved(callback): Registers a function that is called when the workspace is saved.

Scripting

Paid plans only

And the following functions are provided for paid plans only.

  • exportDiagramsToOfflineHTMLPage(callback): Exports all diagrams to a single HTML page for offline use. The callback function is called when generation of the HTML page has been finished, and the first function argument is the resulting HTML markup.

Paid subscriptions only

Puppeteer

The diagram scripting API can be used in conjunction with tools such as Puppeteer (headless Chrome/Chromium), to automate the export of diagrams from the command line, perhaps as a part of a build process. The structurizr-puppeteer repo on GitHub has some code samples of how to do this.