Examples
Crate ships with six example applications in the examples/ directory. Each one is a self-contained vibe.d server that serves both an API and a browser-based client at http://localhost:9090/.
Quick Start
Section titled “Quick Start”Pick an example, build it, and run it:
cd examples/testRestApiMemorydub runThen open http://localhost:9090/ in your browser.
In-Memory Examples
Section titled “In-Memory Examples”These examples use MemoryCrate and have no external dependencies — no database required. Data lives in memory and resets when the server restarts. This is the fastest way to try Crate.
testRestApiMemory
Section titled “testRestApiMemory”REST API with in-memory storage.
cd examples/testRestApiMemorydub runWhat it demonstrates:
MemoryCrate!BookandMemoryCrate!Categoryfor zero-setup storagerouter.crateSetupwith the defaultRestApipolicy- Serving a static HTML client alongside the API via
serveStaticFiles
API format — standard JSON with model-name wrapper:
// POST /books{"book": {"name": "Dune", "author": "Frank Herbert", "price": 12.99, "inStock": true}}
// GET /books{"books": [{"_id": "1", "name": "Dune", ...}]}testJsonApiMemory
Section titled “testJsonApiMemory”JSON:API with in-memory storage.
cd examples/testJsonApiMemorydub runWhat it demonstrates:
MemoryCratewith theJsonApiprotocol policyrouter.crateSetup!JsonApito use JSON:API serialization- The difference in request/response format compared to REST
API format — JSON:API data/attributes envelope:
// POST /books (Content-Type: application/vnd.api+json){"data": {"type": "books", "attributes": {"name": "Dune", "author": "Frank Herbert", "price": 12.99, "inStock": true}}}
// GET /books{"data": [{"type": "books", "id": "1", "attributes": {"name": "Dune", ...}}]}testMcpMemory
Section titled “testMcpMemory”MCP (Model Context Protocol) with in-memory storage.
cd examples/testMcpMemorydub runWhat it demonstrates:
MemoryCratewith theMcpprotocol policyrouter.crateSetup!Mcpto expose models as MCP tools- JSON-RPC 2.0 over a single
/mcpendpoint - Tool discovery via
tools/list
API format — JSON-RPC 2.0 with tool calls:
// POST /mcp — list available tools{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}
// POST /mcp — create a book{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "create_book", "arguments": {"name": "Dune", "author": "Frank Herbert", "price": 12.99, "inStock": true}}}testGraphQLMemory
Section titled “testGraphQLMemory”GraphQL with in-memory storage.
cd examples/testGraphQLMemorydub runWhat it demonstrates:
MemoryCratewith theGraphQLprotocol policyrouter.crateSetup!GraphQLto expose models via GraphQL queries and mutations- Auto-generated schema from model definitions
- Field selection — only requested fields are returned
API format — GraphQL queries and mutations:
// POST /graphql — list books{"query": "{ books { _id name author price inStock } }"}
// POST /graphql — create a book{"query": "mutation { createBook(input: { name: \"Dune\", author: \"Frank Herbert\", price: 12.99, inStock: true }) { _id } }"}MongoDB Examples
Section titled “MongoDB Examples”These examples use MongoCrate and require a MongoDB instance running on 127.0.0.1:27017. Data persists in the test database across restarts.
testRestApiModel
Section titled “testRestApiModel”REST API with MongoDB storage and OpenAPI spec generation.
cd examples/testRestApiModeldub runWhat it demonstrates:
MongoCrate!Bookbacked by a real MongoDB collection- OpenAPI specification generation via
crateRouter.toOpenApi - The same
RestApiprotocol as the in-memory example, but with persistent data
testJsonApiModel
Section titled “testJsonApiModel”JSON:API with MongoDB storage and OpenAPI spec generation.
cd examples/testJsonApiModeldub runWhat it demonstrates:
MongoCratewithJsonApiprotocol policy- OpenAPI specification generation for JSON:API endpoints
- The same models as the REST example, but with JSON:API serialization
Project Structure
Section titled “Project Structure”Every example follows the same layout:
examples/testRestApiMemory/ dub.json # Build configuration dub.selections.json # Pinned dependency versions source/ app.d # Server entry point public/ index.html # Browser client (HTML + inline JS)The server entry point is typically under 40 lines of D code. The HTML client is a single file with no build step and no external dependencies.
Switching Between Protocols
Section titled “Switching Between Protocols”The examples show how little code changes when switching protocols. Compare the four in-memory examples — the only differences are:
- The import:
crate.protocols.jsonApi.policy,crate.protocols.mcp.policy, orcrate.protocols.graphql.policy - The setup call:
router.crateSetup!JsonApi,router.crateSetup!Mcp, orrouter.crateSetup!GraphQL - The client-side request format
The D model structs, crate creation, and server setup are identical across all four.