Skip to content

LikeC4 Rendering Backend

The LikeC4 backend turns a Project into LikeC4 .c4 source. Like Structurizr, LikeC4 models a whole architecture (a specification vocabulary, a model, a deployment and a set of views) rather than one diagram, so the backend has its own authoring API in c4.contrib.likec4. It does not accept the portable Diagram classes.

Rendering to .c4 source needs no external tools. Image export delegates to the likec4 CLI, which needs Node.js and Playwright Chromium, and only the local backend exists: LikeC4 is not available through Kroki. See Renderers and Installation.

Supported version

The backend is tested against LikeC4 1.60.0. Other versions may work, but the generated syntax is only validated against this one. Whatever this project does not check is left to likec4 validate and the LikeC4 documentation.

Capability table

Capability Status
.c4 source rendering Supported, deterministic, no external tools
Image export PNG and JPG through the local likec4 CLI (no SVG: LikeC4 has none)
Specification Element, relationship and deployment node kinds, tags, custom colors
Model Nested elements, relationships (kinds, <->), metadata, links, icons
Deployment Nested nodes, instances, deployment relationships
Views Element, deployment and dynamic views
View predicates *, .*, .**, where, relationship predicates, raw text
Dynamic views Flat steps, back, variant; parallel/alternative blocks not yet
JSON and Python conversion "backend": "likec4" JSON, c4 convert --json-to-py

Quickstart

Save this as project.py:

from c4.contrib.likec4 import Project, Rel, Wildcard

project = Project("Shop")

project.specification.element("actor", shape="person").element("system")

customer = project.model.element("actor", "Customer", alias="customer")
shop = project.model.element("system", "Online Shop", alias="shop")
customer >> Rel("buys goods") >> shop

project.views.element_view("index", "Landscape").include(Wildcard())

Render the source, then export the index view as an image:

c4 render project.py --likec4 -o architecture.c4

c4 export project.py --likec4 --likec4-view-key index -f png -o index.png

The rendered .c4:

specification {
  element actor { style { shape person } }
  element system
}

model {
  customer = actor "Customer"
  shop = system "Online Shop"
  customer -> shop "buys goods"
}

views {
  view index {
    title "Landscape"
    include *
  }
}

Every kind, tag and custom color must be declared in project.specification before it is used; the renderer reports a missing declaration by name.

A larger project
from c4.contrib.likec4 import (
    Between,
    Children,
    Project,
    RawPredicate,
    Rel,
    Where,
    Wildcard,
)


project = Project('Shop', view_key='index')

spec = project.specification
spec.color('brand', '#0b5fff')
spec.element('actor', notation='Person', color='brand', shape='person')
spec.element('system', color='primary')
spec.element('service', technology='Python', shape='component')
spec.element('database', color='amber', shape='storage')
spec.relationship('async', line='dotted', head='diamond')
spec.tag('critical', color='red')
spec.tag('deprecated')
spec.deployment_node('environment', notation='Environment', color='gray')
spec.deployment_node('vm')

model = project.model
customer = model.element(
    'actor',
    'Customer',
    alias='customer',
    description='Buys goods in the **shop**.',
)
shop = model.element(
    'system',
    'Online Shop',
    alias='shop',
    summary='Sells goods online',
    metadata={'owner': 'team-shop', 'tiers': ['web', 'api']},
    links=[('https://example.com/shop', 'Docs')],
)
web = model.element(
    'service',
    'Web',
    parent=shop,
    alias='web',
    icon='tech:react',
)
api = model.element(
    'service',
    'API',
    parent=shop,
    alias='api',
    description='Handles [[orders]] and payments.',
    tags=['critical'],
)
db = model.element(
    'database',
    'Database',
    parent=shop,
    alias='db',
    tags=['deprecated'],
)
queue = model.element('service', 'Order Queue', alias='queue')

customer >> Rel('opens', technology='HTTPS') >> web
web >> Rel('calls', technology='REST', tags=['critical']) >> api
api >> Rel('reads and writes', technology='SQL') >> db
api >> Rel('publishes orders', kind='async', navigate_to='checkout') >> queue

deployment = project.deployment
prod = deployment.node('environment', 'Production', alias='prod')
app = deployment.node('vm', 'App VM', parent=prod, alias='app')
data = deployment.node('vm', 'Data VM', parent=prod, alias='data')

app.instance(api, 'primary')
app.instance(api, 'secondary')
app.instance(web)
data.instance(db)

app >> Rel('queries', technology='TCP/5432') >> data

views = project.views

index = views.element_view('index', 'Landscape')
index.include(Wildcard())
index.style(customer, color='brand')
index.auto_layout('LeftRight', 80, 100)

shop_detail = views.element_view(
    'shop-detail',
    'Shop',
    of=shop,
    tags=['critical'],
)
shop_detail.include(
    Wildcard(),
    Where(Children(shop), tag='critical'),
    Between(web, api),
)
shop_detail.exclude(db)
shop_detail.include(api, navigate_to='checkout')

critical = views.element_view('critical')
critical.include(RawPredicate('shop.** where tag is #critical'))

production = views.deployment_view('production', 'Production')
production.include(prod, Children(prod))
production.style(prod, color='green')

checkout = views.dynamic_view('checkout', 'Checkout', variant='sequence')
checkout.step(customer, web, 'opens checkout', technology='HTTPS')
checkout.step(web, api, 'places order', notes='Validates the **cart**.')
checkout.back(web, api, 'order id')
checkout.step(api, queue, 'enqueues', color='amber')
specification {
  color brand #0b5fff
  element actor {
    notation "Person"
    style {
      color brand
      shape person
    }
  }
  element system { style { color primary } }
  element service {
    technology "Python"
    style { shape component }
  }
  element database {
    style {
      color amber
      shape storage
    }
  }
  relationship async {
    line dotted
    head diamond
  }
  tag critical { color red }
  tag deprecated
  deploymentNode environment {
    notation "Environment"
    style { color gray }
  }
  deploymentNode vm
}

model {
  customer = actor "Customer" { description '''Buys goods in the **shop**.''' }
  shop = system "Online Shop" {
    summary "Sells goods online"
    metadata {
      owner "team-shop"
      tiers ["web", "api"]
    }
    link https://example.com/shop "Docs"
    web = service "Web" { icon tech:react }
    api = service "API" {
      #critical
      description '''Handles \\[\\[orders]] and payments.'''
    }
    db = database "Database" { #deprecated }
  }
  queue = service "Order Queue"
  customer -> shop.web "opens" { technology "HTTPS" }
  shop.web -> shop.api "calls" {
    #critical
    technology "REST"
  }
  shop.api -> shop.db "reads and writes" { technology "SQL" }
  shop.api -[async]-> queue "publishes orders" { navigateTo checkout }
}

deployment {
  prod = environment "Production" {
    app = vm "App VM" {
      primary = instanceOf shop.api
      secondary = instanceOf shop.api
      instanceOf shop.web
    }
    data = vm "Data VM" { instanceOf shop.db }
  }
  prod.app -> prod.data "queries" { technology "TCP/5432" }
}

views {
  view index {
    title "Landscape"
    include *
    style customer { color brand }
    autoLayout LeftRight 80 100
  }
  view shop-detail of shop {
    #critical
    title "Shop"
    include *
    include shop.* where tag is #critical
    include shop.web -> shop.api
    exclude shop.db
    include shop.api with { navigateTo checkout }
  }
  view critical { include shop.** where tag is #critical }
  deployment view production {
    title "Production"
    include prod, prod.*
    style prod { color green }
  }
  dynamic view checkout {
    title "Checkout"
    variant sequence
    customer -> shop.web "opens checkout" { technology "HTTPS" }
    shop.web -> shop.api "places order" { notes '''Validates the **cart**.''' }
    shop.web <- shop.api "order id"
    shop.api -> queue "enqueues" { color amber }
  }
}

Where to go next

  • Authoring: specification, model, relationships, deployment.
  • Views: element, deployment and dynamic views, predicates.
  • Styles: colors, shapes, borders, sizes, icons, line and arrow styles.
  • Reference: allowed values and validation rules (generated).
  • Renderers: the renderer, the local backend and the CLI.
  • Limitations: what is not supported and what only likec4 validate checks.

Upstream documentation: LikeC4 DSL and LikeC4 CLI.