SDKs overview

Kroki provides an API for generating diagrams from textual descriptions, supporting a wide array of diagramming tools like PlantUML, Mermaid, and GraphViz. To facilitate integration for developers, Kroki offers official SDKs and a growing ecosystem of community-contributed libraries across various programming languages. These SDKs abstract the underlying HTTP API calls, allowing developers to generate diagrams by simply providing the diagram code and specifying the desired format.

The SDKs typically handle tasks such as encoding the diagram source, compressing it, making HTTP requests to the Kroki server (either the public instance or a self-hosted one), and decoding the response. This simplifies the process for developers who wish to embed dynamic diagrams into their applications, automate documentation generation, or integrate visual elements into their development workflows. For instance, a Python library might expose a function like kroki.diagram(source, type, format), streamlining diagram generation within Python scripts. The official documentation details the structure of the Kroki HTTP API endpoints for direct interaction if an SDK is not available for a specific environment.

Official SDKs by language

The Kroki project maintains official client libraries for several popular programming languages. These libraries are designed to provide stable and supported interfaces for interacting with the Kroki API. They often include features such as source compression, error handling, and direct integration with common data structures in their respective languages. Using an official SDK ensures compatibility with the latest API features and benefits from dedicated maintenance.

The table below outlines the key official SDKs, their typical package names, and common installation methods:

Language Package/Library Install Command (Example) Maturity
Python kroki-py pip install kroki-py Stable
JavaScript/TypeScript @kroki/kroki-js npm install @kroki/kroki-js or yarn add @kroki/kroki-js Stable
Java kroki-java (Maven/Gradle artifact) Add dependency to pom.xml or build.gradle Stable
Go go-kroki go get github.com/yuzutech/go-kroki Stable

For each official SDK, comprehensive documentation is available on the Kroki SDKs documentation page, detailing usage, configuration options, and specific examples for different diagram types. These resources are crucial for understanding the nuances of each library, such as how to specify the Kroki server URL if not using the public instance, or how to handle binary outputs like PNG or SVG.

Installation

Installing Kroki SDKs typically follows the standard package management practices for each programming language. The process is designed to be straightforward, allowing developers to quickly add Kroki capabilities to their projects.

Python

For Python, the kroki-py library can be installed using pip, the Python package installer. It's recommended to install libraries within a virtual environment to manage dependencies effectively, a practice commonly used in Python development environments.

pip install kroki-py

JavaScript/TypeScript

For JavaScript and TypeScript projects, the @kroki/kroki-js package is available via npm or Yarn. This makes it suitable for both Node.js backend applications and frontend web projects.

# Using npm
npm install @kroki/kroki-js

# Using Yarn
yarn add @kroki/kroki-js

Java

Java developers integrate kroki-java by adding it as a dependency in their build configuration files. This is typically done in pom.xml for Maven projects or build.gradle for Gradle projects.

Maven example (pom.xml):

<dependency>
    <groupId>io.kroki</groupId>
    <artifactId>kroki-java</artifactId>
    <version>0.x.x</version> <!-- Replace with the latest version -->
</dependency>

Gradle example (build.gradle):

implementation 'io.kroki:kroki-java:0.x.x' // Replace with the latest version

Go

Go modules are used for managing dependencies in Go projects. The go-kroki library can be added using the go get command.

go get github.com/yuzutech/go-kroki

After running the installation command, the Go module system will download and make the library available for import in your Go source files.

Quickstart example

This section provides a quickstart example demonstrating how to generate a simple Mermaid flowchart using the Kroki SDKs. The examples will illustrate the basic steps of importing the library, defining diagram source code, and requesting an image from the Kroki service.

Python Quickstart

This Python example uses kroki-py to generate a Mermaid diagram and save it as an SVG file. This provides a direct way to integrate diagram generation into Python scripts or applications, as detailed in the Kroki Python SDK documentation.

import kroki

def generate_mermaid_diagram():
    source = """
    graph TD;
        A[Start] --> B{Is it?};
        B --> C{OK};
        C --> D[End];
    """

    # Generate the diagram as SVG
    svg_output = kroki.diagram(source, 'mermaid', 'svg')

    # Save the SVG to a file
    with open('mermaid_flowchart.svg', 'wb') as f:
        f.write(svg_output)
    print("Mermaid flowchart saved to mermaid_flowchart.svg")

if __name__ == "__main__":
    generate_mermaid_diagram()

JavaScript Quickstart (Node.js)

This Node.js example uses @kroki/kroki-js to generate a PlantUML diagram and print its SVG output to the console. This snippet demonstrates how to use the SDK in a server-side JavaScript environment, aligning with practices for JavaScript module imports.

const kroki = require('@kroki/kroki-js');

async function generatePlantUMLDiagram() {
    const source = `
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
    `;

    try {
        const svgOutput = await kroki.diagram({ 
            diagram: source,
            type: 'plantuml',
            format: 'svg'
        });
        console.log("PlantUML diagram (SVG):");
        console.log(svgOutput.toString());
    } catch (error) {
        console.error("Error generating diagram:", error);
    }
}

generatePlantUMLDiagram();

Java Quickstart

This Java example uses kroki-java to generate a GraphViz diagram and save it to a file. This type of integration is common in Java enterprise applications for generating dynamic reports or documentation.

import io.kroki.client.KrokiClient;
import io.kroki.client.DiagramType;
import io.kroki.client.Format;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class KrokiGraphVizExample {

    public static void main(String[] args) {
        String source = """
        digraph G {
            A -> B;
            B -> C;
            C -> A;
        }
        """;

        KrokiClient client = new KrokiClient("https://kroki.io"); // Or your self-hosted instance

        try {
            byte[] svgBytes = client.render(DiagramType.GRAPHVIZ, Format.SVG, source);
            Path outputPath = Paths.get("graphviz_diagram.svg");
            Files.write(outputPath, svgBytes);
            System.out.println("GraphViz diagram saved to " + outputPath.toAbsolutePath());
        } catch (IOException e) {
            System.err.println("Error generating or saving diagram: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Community libraries

Beyond the officially maintained SDKs, the Kroki ecosystem benefits from various community-contributed libraries and integrations. These libraries often extend Kroki's functionality to specific frameworks, build tools, or niche environments, providing tailored solutions for particular use cases. While not officially supported by the Kroki team, many community projects are actively maintained and widely used.

Examples of community contributions include:

  • Markdown/Documentation Integrations: Plugins for static site generators (like Hugo, Jekyll, or MkDocs) that automatically convert Kroki code blocks in Markdown files into rendered diagrams during the build process. These plugins streamline the creation of technical documentation with embedded visuals.
  • Editor Extensions: Integrations for code editors (e.g., VS Code extensions) that provide live previews of Kroki diagrams as developers type their source code, enhancing the authoring experience.
  • Language-Specific Wrappers: Libraries for languages not covered by official SDKs, or alternative implementations that offer different API paradigms or additional features (e.g., caching, advanced error handling).
  • Build Tool Plugins: Integrations with build systems like Maven or Gradle that allow for diagram generation as part of a software project's build lifecycle, ensuring documentation stays up-to-date with code changes.

Developers are encouraged to explore community resources on platforms like GitHub or package repositories (e.g., PyPI, npm) by searching for "kroki" to discover these integrations. When using community libraries, it is advisable to review their documentation, issue trackers, and contribution activity to assess their maturity and ongoing support. The Kroki community integrations page also lists various projects that connect Kroki with other tools and platforms, such as AsciiDoc and Confluence, expanding its utility in diverse development and documentation workflows.