Tutorial
We model a small online bookshop, one C4 level at a time. Each step is a complete, valid file; the diagrams below are what render produces from it.
Install the runtime first (see Command line), then create a file bookshop.json. Add "$schema" with the path from your file to schema/c4.schema.json to get completion and validation in your editor. The sources below say ../../schema/c4.schema.json because they live in site/models/ of the repository.
1. System context
Start with the big picture: who uses the system, and which other systems it depends on. Nothing technical yet.
{
"$schema": "../../schema/c4.schema.json",
"version": 1,
"name": "Bookshop",
"description": "Online shop for an independent bookstore",
"model": {
"people": [
{ "id": "customer", "name": "Customer", "description": "Buys books online", "external": true }
],
"softwareSystems": [
{ "id": "shop", "name": "Bookshop", "description": "Catalogue, cart and orders" },
{ "id": "payments", "name": "Payment provider", "description": "Card payments", "external": true }
],
"relationships": [
{ "from": "customer", "to": "shop", "description": "Browses and buys books using" },
{ "from": "shop", "to": "payments", "description": "Charges cards using" }
]
},
"views": [
{ "type": "systemContext", "key": "context", "scope": "shop", "title": "Bookshop - system context" }
]
}versionis required and always1.- Every element has an
id, unique in the whole file, and aname. Ids are what relationships and views reference. external: truemarks what you do not own. It is drawn in grey.- Relationships live in one place,
model.relationships. Thedescriptionreads as a sentence: Customer "browses and buys books using" Bookshop. - A
systemContextview needs ascope: the software system at the centre.
Check and render it:
node src/cli.ts validate bookshop.json
node src/cli.ts render bookshop.json bookshop.html
2. Containers
Zoom into the bookshop: what runs and is deployed separately. Containers are nested in their software system, and get a technology.
JSON source: site/models/tutorial-2-containers.json
{
"$schema": "../../schema/c4.schema.json",
"version": 1,
"name": "Bookshop",
"description": "Online shop for an independent bookstore",
"model": {
"people": [
{ "id": "customer", "name": "Customer", "description": "Buys books online", "external": true }
],
"softwareSystems": [
{
"id": "shop",
"name": "Bookshop",
"description": "Catalogue, cart and orders",
"containers": [
{ "id": "web", "name": "Web shop", "description": "Storefront and cart", "technology": "Next.js", "tags": ["Browser"] },
{ "id": "api", "name": "Order API", "description": "Takes and tracks orders", "technology": "Go" },
{ "id": "db", "name": "Order database", "description": "Orders and stock", "technology": "PostgreSQL", "tags": ["Database"] }
]
},
{ "id": "payments", "name": "Payment provider", "description": "Card payments", "external": true }
],
"relationships": [
{ "from": "customer", "to": "web", "description": "Browses and buys books using", "technology": "HTTPS" },
{ "from": "web", "to": "api", "description": "Places orders using", "technology": "HTTPS/JSON" },
{ "from": "api", "to": "db", "description": "Reads and writes orders in", "technology": "SQL" },
{ "from": "api", "to": "payments", "description": "Charges cards using", "technology": "HTTPS/JSON" }
]
},
"views": [
{ "type": "systemContext", "key": "context", "scope": "shop", "title": "Bookshop - system context" },
{ "type": "container", "key": "containers", "scope": "shop", "title": "Bookshop - containers" }
],
"styles": {
"elements": [
{ "tag": "Database", "shape": "cylinder" },
{ "tag": "Browser", "shape": "webBrowser" }
]
}
}What changed:
shopnow has threecontainers. The database is a container of the shop, not a system of its own.- Relationships now go to and from containers:
customer → web,api → payments. You do not need to keepcustomer → shop: the context view still shows it, because relationships between parents are implied from those between their children. - Relationships between containers name their protocol in
technology. - The
DatabaseandBrowsertags are styled with acylinderand awebBrowsershape. Tags are free text; the styles give them meaning.
The context view of the same file is unchanged, even though no relationship targets shop any more:
3. Components
Zoom into one container only where it helps. Here, the Order API is split into three components.
JSON source: site/models/tutorial-3-components.json
{
"$schema": "../../schema/c4.schema.json",
"version": 1,
"name": "Bookshop",
"description": "Online shop for an independent bookstore",
"model": {
"people": [
{ "id": "customer", "name": "Customer", "description": "Buys books online", "external": true }
],
"softwareSystems": [
{
"id": "shop",
"name": "Bookshop",
"description": "Catalogue, cart and orders",
"containers": [
{ "id": "web", "name": "Web shop", "description": "Storefront and cart", "technology": "Next.js", "tags": ["Browser"] },
{
"id": "api",
"name": "Order API",
"description": "Takes and tracks orders",
"technology": "Go",
"components": [
{ "id": "orderHandler", "name": "Order handler", "description": "HTTP endpoints for orders", "technology": "net/http" },
{ "id": "checkout", "name": "Checkout", "description": "Prices the cart and takes the payment", "technology": "Go package" },
{ "id": "orderStore", "name": "Order store", "description": "Persists orders", "technology": "pgx" }
]
},
{ "id": "db", "name": "Order database", "description": "Orders and stock", "technology": "PostgreSQL", "tags": ["Database"] }
]
},
{ "id": "payments", "name": "Payment provider", "description": "Card payments", "external": true }
],
"relationships": [
{ "from": "customer", "to": "web", "description": "Browses and buys books using", "technology": "HTTPS" },
{ "from": "web", "to": "orderHandler", "description": "Places orders using", "technology": "HTTPS/JSON" },
{ "from": "orderHandler", "to": "checkout", "description": "Checks out the cart with" },
{ "from": "checkout", "to": "orderStore", "description": "Saves the paid order with" },
{ "from": "checkout", "to": "payments", "description": "Charges cards using", "technology": "HTTPS/JSON" },
{ "from": "orderStore", "to": "db", "description": "Reads and writes orders in", "technology": "SQL" }
]
},
"views": [
{ "type": "systemContext", "key": "context", "scope": "shop", "title": "Bookshop - system context" },
{ "type": "container", "key": "containers", "scope": "shop", "title": "Bookshop - containers" },
{ "type": "component", "key": "components", "scope": "api", "title": "Order API - components" }
],
"styles": {
"elements": [
{ "tag": "Database", "shape": "cylinder" },
{ "tag": "Browser", "shape": "webBrowser" }
]
}
}componentsare nested in theapicontainer.web → apibecameweb → orderHandler: declare relationships at the most detailed level you model.- A
componentview is scoped to a container.
4. A dynamic view
Static views show what can talk to what. A dynamic view tells one story: the numbered steps of a scenario.
{
"type": "dynamic",
"key": "placeOrder",
"scope": "shop",
"title": "Place an order",
"description": "What happens when a customer checks out.",
"steps": [
{ "from": "customer", "to": "web", "description": "Submits the cart" },
{ "from": "web", "to": "api", "description": "Creates the order" },
{ "from": "api", "to": "payments", "description": "Charges the card" },
{ "from": "api", "to": "db", "description": "Stores the paid order" }
]
}
- Every step must follow a relationship of the model, in the same direction.
web → apiis not declared, but it is implied byweb → orderHandler, so it is valid. - With a software system as
scope, steps run between containers, drawn inside the boundary. - The step
descriptionreplaces the relationship's on the drawing; the technology comes from the relationship.
5. Review before you share
node src/cli.ts inspect bookshop.json
inspect lists what each view shows and warns about modelling gaps: missing descriptions or technologies, elements in no view, crowded views. All the files of this tutorial pass without warnings.
The final file, with all four views:
JSON source: site/models/tutorial-4-dynamic.json
{
"$schema": "../../schema/c4.schema.json",
"version": 1,
"name": "Bookshop",
"description": "Online shop for an independent bookstore",
"model": {
"people": [
{
"id": "customer",
"name": "Customer",
"description": "Buys books online",
"external": true
}
],
"softwareSystems": [
{
"id": "shop",
"name": "Bookshop",
"description": "Catalogue, cart and orders",
"containers": [
{
"id": "web",
"name": "Web shop",
"description": "Storefront and cart",
"technology": "Next.js",
"tags": [
"Browser"
]
},
{
"id": "api",
"name": "Order API",
"description": "Takes and tracks orders",
"technology": "Go",
"components": [
{
"id": "orderHandler",
"name": "Order handler",
"description": "HTTP endpoints for orders",
"technology": "net/http"
},
{
"id": "checkout",
"name": "Checkout",
"description": "Prices the cart and takes the payment",
"technology": "Go package"
},
{
"id": "orderStore",
"name": "Order store",
"description": "Persists orders",
"technology": "pgx"
}
]
},
{
"id": "db",
"name": "Order database",
"description": "Orders and stock",
"technology": "PostgreSQL",
"tags": [
"Database"
]
}
]
},
{
"id": "payments",
"name": "Payment provider",
"description": "Card payments",
"external": true
}
],
"relationships": [
{
"from": "customer",
"to": "web",
"description": "Browses and buys books using",
"technology": "HTTPS"
},
{
"from": "web",
"to": "orderHandler",
"description": "Places orders using",
"technology": "HTTPS/JSON"
},
{
"from": "orderHandler",
"to": "checkout",
"description": "Checks out the cart with"
},
{
"from": "checkout",
"to": "orderStore",
"description": "Saves the paid order with"
},
{
"from": "checkout",
"to": "payments",
"description": "Charges cards using",
"technology": "HTTPS/JSON"
},
{
"from": "orderStore",
"to": "db",
"description": "Reads and writes orders in",
"technology": "SQL"
}
]
},
"views": [
{
"type": "systemContext",
"key": "context",
"scope": "shop",
"title": "Bookshop - system context"
},
{
"type": "container",
"key": "containers",
"scope": "shop",
"title": "Bookshop - containers"
},
{
"type": "component",
"key": "components",
"scope": "api",
"title": "Order API - components"
},
{
"type": "dynamic",
"key": "placeOrder",
"scope": "shop",
"title": "Place an order",
"description": "What happens when a customer checks out.",
"steps": [
{
"from": "customer",
"to": "web",
"description": "Submits the cart"
},
{
"from": "web",
"to": "api",
"description": "Creates the order"
},
{
"from": "api",
"to": "payments",
"description": "Charges the card"
},
{
"from": "api",
"to": "db",
"description": "Stores the paid order"
}
]
}
],
"styles": {
"elements": [
{
"tag": "Database",
"shape": "cylinder"
},
{
"tag": "Browser",
"shape": "webBrowser"
}
]
}
}Next steps
- Choose and filter what each view shows: Views.
- Change shapes and colours: Tags and styles.
- Split a large model into several files: Splitting a model.
- Every field, with its type: JSON schema.