Skip to content

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/.

Pick an example, build it, and run it:

Terminal window
cd examples/testRestApiMemory
dub run

Then open http://localhost:9090/ in your browser.

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.

REST API with in-memory storage.

Terminal window
cd examples/testRestApiMemory
dub run

What it demonstrates:

  • MemoryCrate!Book and MemoryCrate!Category for zero-setup storage
  • router.crateSetup with the default RestApi policy
  • 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", ...}]}

JSON:API with in-memory storage.

Terminal window
cd examples/testJsonApiMemory
dub run

What it demonstrates:

  • MemoryCrate with the JsonApi protocol policy
  • router.crateSetup!JsonApi to 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", ...}}]}

MCP (Model Context Protocol) with in-memory storage.

Terminal window
cd examples/testMcpMemory
dub run

What it demonstrates:

  • MemoryCrate with the Mcp protocol policy
  • router.crateSetup!Mcp to expose models as MCP tools
  • JSON-RPC 2.0 over a single /mcp endpoint
  • 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}}}

GraphQL with in-memory storage.

Terminal window
cd examples/testGraphQLMemory
dub run

What it demonstrates:

  • MemoryCrate with the GraphQL protocol policy
  • router.crateSetup!GraphQL to 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 } }"}

These examples use MongoCrate and require a MongoDB instance running on 127.0.0.1:27017. Data persists in the test database across restarts.

REST API with MongoDB storage and OpenAPI spec generation.

Terminal window
cd examples/testRestApiModel
dub run

What it demonstrates:

  • MongoCrate!Book backed by a real MongoDB collection
  • OpenAPI specification generation via crateRouter.toOpenApi
  • The same RestApi protocol as the in-memory example, but with persistent data

JSON:API with MongoDB storage and OpenAPI spec generation.

Terminal window
cd examples/testJsonApiModel
dub run

What it demonstrates:

  • MongoCrate with JsonApi protocol policy
  • OpenAPI specification generation for JSON:API endpoints
  • The same models as the REST example, but with JSON:API serialization

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.

The examples show how little code changes when switching protocols. Compare the four in-memory examples — the only differences are:

  1. The import: crate.protocols.jsonApi.policy, crate.protocols.mcp.policy, or crate.protocols.graphql.policy
  2. The setup call: router.crateSetup!JsonApi, router.crateSetup!Mcp, or router.crateSetup!GraphQL
  3. The client-side request format

The D model structs, crate creation, and server setup are identical across all four.