Usage
Kroki provides an HTTP API to convert plain text diagrams to images.
Kroki handles both GET and POST requests.
When using GET requests, your diagram must be encoded in the URL using a deflate + base64 algorithm.
Don’t worry, if you’re not familiar with deflate or base64 (or if you don’t want to use them),
you can also send your diagram as plain text using POST requests.
|
Let’s take an example with a GraphViz "Hello World":
digraph G {
Hello->World
}
Here we are using a Python one-liner to encode our diagram using deflate + base64:
cat hello.dot | python -c "import sys; import base64; import zlib; print(base64.urlsafe_b64encode(zlib.compress(sys.stdin.read().encode('utf-8'), 9)).decode('ascii'))"
The above command will return eNpLyUwvSizIUHBXqPZIzcnJ17ULzy_KSanlAgB1EAjQ.
We can then copy this value in the URL:
GET /graphviz/svg/eNpLyUwvSizIUHBXqPZIzcnJ17ULzy_KSanlAgB1EAjQ
And here’s the result:
You can also call Kroki with POST:
POST /
{
"diagram_source": "digraph G {Hello->World}",
"diagram_type": "graphviz",
"output_format": "svg"
}
In this case, you don’t need to encode your diagram.
It’s also possible to send your diagram as plain text using the Content-Type header.
The output format will be specified using the Accept header and the diagram source will be sent as the request body:
POST /graphviz
Accept: image/svg+xml
Content-Type: text/plain
digraph G {
Hello->World
}
You can also define the output format in the URL if you don’t want to add an Accept header:
POST /graphviz/svg
Content-Type: text/plain
digraph G {
Hello->World
}
The same concept applies when sending the diagram as JSON:
POST /graphviz/svg
{
"diagram_source": "digraph G {Hello->World}"
}
Options
Some diagram libraries relies on options declared outside the diagram definition.
To do pass options to the underlying diagram library, you can declare them as query parameters, as attributes in the JSON body or as HTTP headers.
Query parameters
For GET requests, you can pass diagram options as query parameters:
GET /graphviz/svg/eNpLyUwvSizIUHBXqPZIzcnJ17ULzy_KSanlAgB1EAjQ?key=value
POST requests
When using POST you can declare options in the diagram_options:
POST /
{
"diagram_source": "digraph G {Hello->World}",
"diagram_type": "graphviz",
"output_format": "svg",
"diagram_options": {
"key": "value"
}
}
HTTP headers
Finally, you can pass options using HTTP headers prefixed by Kroki-Diagram-Options:
POST /graphviz
Accept: image/svg+xml
Content-Type: text/plain
Kroki-Diagram-Options-Key: value
digraph G {
Hello->World
}
Kroki defines the precedence order as follows:
-
JSON body
-
HTTP header
-
Query parameter
To find out what options are available, please read the Diagram Options page.
Errors
When a request fails, Kroki responds with an HTTP error status and a message.
The format of the message depends on the Accept header: HTML, JSON, plain text, or an SVG/PNG image showing the message.
With Accept: application/json, the body describes the error:
{
"error": {
"code": 400,
"type": "diagram-error",
"message": "Syntax Error? (Assumed diagram type: sequence)",
"line": 2
}
}
code-
The HTTP status code.
type-
Why the request failed:
invalid-requestthe request is invalid (empty body, invalid diagram option…)
decodethe encoded diagram source cannot be decoded
unsupported-diagram-typethe diagram type is not supported
unsupported-formatthe output format is not supported by the diagram type
unsupported-mime-typenone of the MIME types of the
Acceptheader is supported by the diagram typemissing-formatthe output format is neither defined in the path nor in the
Acceptheadermethod-not-allowedthe HTTP method is not allowed on this endpoint
not-foundthe resource does not exist
uri-too-longthe request URI is too long, send the diagram in a POST request instead
diagram-errorthe diagram library rejected the diagram source
service-unavailablea companion service required to render the diagram is unavailable
timeoutthe conversion took too long
internalunexpected error
message-
The error message. For a
diagram-error, the message of the diagram library. line,column-
Where the diagram library reports the error in the diagram source (1-based), when known. Currently, PlantUML and Structurizr report a line, Mermaid reports a line and sometimes a column. When the error has a
line, the PlantUML message doesn’t repeat it.
Clients must handle responses without the type, line and column fields (older servers, proxies), and type values they don’t know.
OpenAPI specification
Kroki describes its HTTP API with an OpenAPI 3.1 document served at /openapi.json:
GET /openapi.json
The document is generated from the diagram types available on the server: each diagram type lists its own output formats, its options and the version of the diagram library. Companion services registered at runtime are included as long as they are registered.
Kroki does not ship an interactive API explorer.
To browse the API or send requests from a UI, load the document URL (for instance https://kroki.io/openapi.json) in a tool such as Swagger Editor, Scalar, Redocly or your IDE.
CORS is enabled on every route, so a browser-based tool can fetch the document directly.
Next
-
Install Kroki on your server for the first time
-
Interact with the Kroki API using: