Structurizr Authoring¶
A Structurizr Workspace owns one persistent model, views and styles.
They are properties, not factories. This page covers the workspace, the model,
relationships and deployment; Views and Styles cover
the rest.
from c4.contrib.structurizr import Rel, Workspace
workspace = Workspace("Payments", description="Payments architecture")
with workspace.model as model:
customer = model.Person("Customer", tags=["External"])
payments = model.SoftwareSystem("Payments")
api = model.Container("API", technology="FastAPI", owner=payments)
customer >> Rel("Uses", technology="HTTPS") >> api
Use the Structurizr names for factories (Person, SoftwareSystem,
Container, Component, ...). Lowercase aliases are not supported. Tags must
be list[str] (or tuple) values, never a bare string.
Workspace¶
Workspace(name, description=None, theme=None, default_renderer=None,
render_options=None) is also a context manager. workspace.set_property(key,
value) sets a workspace property. workspace.render() returns the DSL and
workspace.save(path) writes it. See Styles for theme and
render_options.
Elements¶
Every element factory is a method of workspace.model and returns the element.
Containers and components also have shortcuts on their parent:
system.Container(...) and container.Component(...).
Elements nest in two ways: pass owner= (or parent= for deployment nodes), or
use the parent as a context manager:
from c4.contrib.structurizr import Workspace
workspace = Workspace("Nested")
with workspace.model as model:
shop = model.SoftwareSystem("Shop")
with shop:
api = model.Container("API") # owner is the active context
with api:
model.Component("Controller")
| Python call | Generated DSL |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
technology is rendered for containers, components, deployment nodes and
infrastructure nodes. Structurizr DSL has no technology slot on people and
software systems, so it is ignored there. When only technology is set, the
description slot is written as an empty string.
Common keyword arguments¶
All elements accept description, technology, alias, tags, properties,
url and style. Containers and components also take owner; deployment and
infrastructure nodes take parent; instances take parent.
| Python call | Generated DSL |
|---|---|
|
|
|
|
|
|
|
|
|
|
aliasis the Python-side identity and the DSL identifier. When omitted it is derived from the name ("Web App"becomesweb_app), and a repeated alias gets a_2,_3suffix. An explicit alias must be a valid Python identifier. Words reserved by Structurizr DSL (such asmodelorviews) are renamed in the output.tagsare added to the built-in tag of the element kind (Person,Software System,Container, ...).element.tag("Critical")adds more and returns the element.propertiesbecome aproperties { ... }block;element.property(key, value)sets one.styleis an element-local style. It generates a private tag and one style rule, so it is equivalent toworkspace.styles.element(element, ...). See Styles.
Relationships¶
uses, used_by and the >> operator create model relationships between
elements of the same workspace. They accept technology, tags,
properties, url and style.
a.uses(b, "label")createsa -> b.b.used_by(a, "label")creates the samea -> b.a >> Rel("label", technology=...) >> bis the concise form, with the StructurizrRelbuilder.a >> "label" >> bworks without options.b << Rel("label") << aandb << "label" << awrite the same relationship in the other direction.- A list on one side fans out:
a >> Rel("label") >> [b, c]and[a, b] >> Rel("label") >> c, and the<<forms of both. Each list item gives one relationship, in list order, and the expression returns the list. A list on both sides raisesValueError("Either source or destination must be a single element")and creates nothing. A list can stand on the left only together withRel(...): Python has no>>between alistand astr, so[a, b] >> "label" >> craisesTypeError. Use[a, b] >> Rel("label") >> c. With one element on the left, a string works:a >> "label" >> [b, c]. a >> bcreates a relationship with no description, and the DSL has no description string for it.a >> b | "label"sets the description; the pipe fills only an empty description and raisesValueErrorif one exists.
Rel takes only the description as a positional argument:
Rel("Calls", technology="HTTPS"). Rel("Calls", "HTTPS") raises TypeError,
and there is no label= alias. A Rel is immutable, so one value can be used
for any number of relationships.
| Python call | Generated DSL |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
A relationship between a parent and one of its own descendants is rejected when rendering, because Structurizr does not allow it. See Limitations.
Relationships created with uses or >> are model relationships. A dynamic
view step is view-local unless you ask otherwise; see
Views.
Deployment¶
A deployment model has environments, nested deployment nodes, optional infrastructure nodes and instances of model elements.
from c4.contrib.structurizr import Workspace
workspace = Workspace("Deployment")
with workspace.model as model:
shop = model.SoftwareSystem("Shop")
api = model.Container("API", owner=shop)
with model.DeploymentEnvironment("Live") as live:
server = live.DeploymentNode("Server", technology="Ubuntu 24.04")
docker = server.DeploymentNode("Docker")
docker.InfrastructureNode("Load balancer", technology="nginx")
docker.ContainerInstance(api)
workspace.views.DeploymentView(shop, environment="Live", key="live")
model.DeploymentEnvironment(name)returns the existing environment of that name or creates it. Insidewith env:new deployment nodes join it.env.DeploymentNode(...)andnode.DeploymentNode(...)create root and nested nodes. Every node belongs to exactly one environment.InfrastructureNodeand the two instance factories need a deployment node parent. An instance copies the name, description and technology of its target and gets the alias<target>_instanceunless you passalias=.SoftwareSystemInstancetakes aSoftwareSystem,ContainerInstancetakes aContainer, both from the same workspace.
Validation¶
The rules checked before rendering are listed in Limitations, and the full list with the error that reports each is in the Reference.
Upstream: model, deployment model.