Skip to content

Introduction

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.

Building a REST API with vibe.d typically means writing repetitive handler functions:

// Without Crate: manual route registration
router.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.

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.

Define a struct with an _id field. Crate introspects the struct at compile time and generates GET, POST, PUT, PATCH, and DELETE endpoints.

Add cross-cutting concerns with a fluent API:

crateRouter
.prepare(productCrate)
.and(authMiddleware)
.and(validationFilter)
.and(paginationFilter);

The same model can serve multiple API protocols simultaneously:

// REST + MCP from the same crate
auto crateRouter = router.crateSetup!(RestApi, Mcp);
crateRouter.add(productCrate);

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.

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

Ready to build something? Head to Getting Started to set up your first Crate API.