Introduction
What is Crate?
Section titled “What is Crate?”Crate is a D language framework that sits on top of vibe.d and turns struct definitions into fully functional REST APIs. Instead of manually registering route handlers for every endpoint, you define your data model as a D struct and let Crate generate the CRUD operations.
The Problem
Section titled “The Problem”Building a REST API with vibe.d typically means writing repetitive handler functions:
// Without Crate: manual route registrationrouter.get("/products", &handleGetProducts);router.get("/products/:id", &handleGetProduct);router.post("/products", &handleCreateProduct);router.put("/products/:id", &handleReplaceProduct);router.patch("/products/:id", &handleUpdateProduct);router.delete_("/products/:id", &handleDeleteProduct);
// ... and then implementing each handler with JSON serialization,// error handling, database queries, etc.For every model in your API, you write the same boilerplate. Authentication, filtering, pagination, and validation get copy-pasted across handlers.
The Crate Approach
Section titled “The Crate Approach”With Crate, the same API becomes:
struct Product { string _id; string name; int price;}
auto products = new MongoCrate!Product("products");router.crateSetup!RestApi.add(products);Six endpoints, JSON serialization, error handling, and database integration in three lines.
Key Features
Section titled “Key Features”Auto-Generated CRUD
Section titled “Auto-Generated CRUD”Define a struct with an _id field. Crate introspects the struct at compile time and generates GET, POST, PUT, PATCH, and DELETE endpoints.
Middleware Pipeline
Section titled “Middleware Pipeline”Add cross-cutting concerns with a fluent API:
crateRouter .prepare(productCrate) .and(authMiddleware) .and(validationFilter) .and(paginationFilter);Multiple Protocols
Section titled “Multiple Protocols”The same model can serve multiple API protocols simultaneously:
// REST + MCP from the same crateauto crateRouter = router.crateSetup!(RestApi, Mcp);crateRouter.add(productCrate);Compile-Time Safety
Section titled “Compile-Time Safety”D’s template system validates your configuration at compile time. Missing required fields, invalid middleware annotations, and policy misconfiguration are caught before your code even runs.
When to Use Crate
Section titled “When to Use Crate”Crate is designed for:
- CRUD-heavy APIs where most endpoints follow standard patterns
- Multi-protocol services that need REST, MCP, GraphQL, or JSON:API
- D/vibe.d projects that want to reduce boilerplate
Crate is less suited for:
- APIs with mostly custom, non-CRUD endpoints (though Crate supports custom operations too)
- Projects not using vibe.d as the HTTP server
Next Steps
Section titled “Next Steps”Ready to build something? Head to Getting Started to set up your first Crate API.