Model API
The Model API gives you your architecture model in code: load a workspace (or a DSL string) in Node.js, then query and traverse elements, relationships, deployments and views.
On these pages:
- Load and query (this page): create a
LikeC4instance and get the model from it. - Model reference: methods of
LikeC4Model, elements, element metadata, the deployment model and views. - Model Builder: build a model in TypeScript, without DSL files.
- Change the model: add elements to a loaded workspace, write DSL back, apply model diffs and delete elements safely.
Add likec4 to your dependencies:
npm i likec4pnpm add likec4yarn add likec4bun add likec4Create a LikeC4 instance from a directory with source files or from a string with DSL source.
From workspace
Section titled “From workspace”Recursively search and parse source files:
import { LikeC4 } from 'likec4'
const likec4 = await LikeC4.fromWorkspace('/path/to/workspace')The method also accepts options:
| Property | Description |
|---|---|
printErrors |
If the model is invalid, report errors to the logger (default true) |
throwIfInvalid |
Return a rejected promise if the model is invalid (default false) |
logger |
Where to write logs: 'console' (default) or 'stderr'.false leaves the logger unconfigured |
logLevel |
Log level: 'trace', 'debug', 'info', 'warning' or 'error' |
graphviz |
Layout engine: 'wasm' (default) uses the bundled Graphviz WebAssembly, 'binary' uses the local Graphviz dot binary |
From source
Section titled “From source”Parse from a string:
import { LikeC4 } from 'likec4'
const likec4 = await LikeC4.fromSource(` specification { element system element user } model { customer = user 'Customer' cloud = system 'System' } views { view index { include * } }`)fromSource accepts the same options as fromWorkspace.
Dispose
Section titled “Dispose”A LikeC4 instance holds language services. Dispose it when you no longer need it:
import { LikeC4 } from 'likec4'
const likec4 = await LikeC4.fromWorkspace('/path/to/workspace')
// ...
// Cleanup resourcesawait likec4.dispose()With an await using declaration, the instance is disposed automatically:
import { LikeC4 } from 'likec4'
async function main() { await using likec4 = await LikeC4.fromWorkspace('/path/to/workspace')
// ... // likec4 is disposed when main() returns}Get the model
Section titled “Get the model”Once the workspace is loaded, get the model in one of two stages (both have the same query API):
- Computed (
LikeC4Model.Computed): views are computed from predicates. Fast and enough to traverse, but not ready for rendering. - Layouted (
LikeC4Model.Layouted): extends the computed model with layout data (dimensions, positions) needed for rendering.
import { LikeC4 } from 'likec4'
const likec4 = await LikeC4.fromWorkspace('/path/to/workspace')
// Validation errors and warningsif (likec4.hasErrors()) { console.log(likec4.getErrors())}console.log(likec4.getWarnings())
// Computed modelconst model = await likec4.computedModel()
// Layouted model, ready for renderingconst layouted = await likec4.layoutedModel()const diagram = layouted.view('index').$layoutedlikec4.syncComputedModel() returns the computed model synchronously, but does not read manual layouts.
In a workspace with multiple projects, pass the project name: likec4.computedModel('my-project'). likec4.projects() lists the project IDs.
Query the model
Section titled “Query the model”import { LikeC4 } from 'likec4'
const likec4 = await LikeC4.fromWorkspace('/path/to/workspace')const model = await likec4.computedModel()
// Elements of some kindconst containers = model.elementsOfKind('container')
// Filter elements with a where operator:// kind is 'container' and (tag is 'deprecated' or tag is not 'internal')const matching = model.elementsWhere({ and: [ { kind: 'container' }, { or: [ { tag: 'deprecated' }, { tag: { neq: 'internal', }, }, ], }, ],})
// Views that include the elementconst views = model .element('cloud.backend.api') .views()
// Source elements of incoming relationships, filtered by tagconst callers = model .element('cloud.backend.api') .incoming() // relationships incoming to the element .filter(r => r.isTagged('http')) // filter by tag .map(r => r.source) // source elements .toArray()Methods that return several items, such as elements(), elementsWhere() or incoming(), return iterators.
Use iterator helpers (.filter(), .map(), .toArray()) or spread them into an array.
See the Model reference for all methods, element metadata, the deployment model and views.