REST API Tutorials

This tutorial builds a code-snippet API with Reinhardt's REST template. You will create a snippets app, expose function-based JSON endpoints with HTTP method macros, connect those handlers to the database through dependency injection, and finish by comparing the explicit handler style with a compact ModelViewSet.

The finished project is the reference crate at examples/examples-tutorial-rest. Treat that example as the answer key: every chapter below points back to files in that crate and ends with a command you can run.

If you want a browser-rendered full-stack application with server functions and a WASM client, follow the basis tutorial instead. This track is REST-only: it has no SPA, no client router, and no templates.

What You'll Build

By the end of the tutorial, the server exposes these API paths under /api/:

GET    /api/snippets/
POST   /api/snippets/
GET    /api/snippets/config/
GET    /api/snippets/{id}/
PUT    /api/snippets/{id}/
DELETE /api/snippets/{id}/

GET    /api/snippets-viewset/
POST   /api/snippets-viewset/
GET    /api/snippets-viewset/{id}/
PUT    /api/snippets-viewset/{id}/
PATCH  /api/snippets-viewset/{id}/
DELETE /api/snippets-viewset/{id}/

The first group is written by hand so you can see the request extractors, response construction, validation, and dependency injection. The second group is generated by ModelViewSet so you can decide when the convention-over-configuration layer is worth using.

Prerequisites

You should have:

  • Rust 1.94.0 or later
  • Docker Desktop running, because the example's cargo make migrate and cargo make runserver tasks start disposable PostgreSQL and Redis containers
  • Basic familiarity with Rust modules, async fn, and JSON serialization
  • The cargo-make subcommand available as cargo make

Install the Reinhardt project generator before starting Part 1:

cargo install reinhardt-admin-cli --version "0.2.0"

After installation, the command is reinhardt-admin.

Tutorial Parts

PartTitleWhat you build
1Project SetupGenerate a REST project, inspect the settings layout, and run the empty server
2Your First EndpointsAdd temporary static JSON endpoints with #[get], #[post], #[put], and #[delete]
3Models and the DatabaseDefine the Snippet model, create the migration, and apply the schema
4Dependency InjectionReplace the temporary handlers with real ORM access through Depends<DatabaseConnection>
5Serializers and ValidationValidate request bodies, choose status codes, and return structured error responses
6Bonus: ViewSets and RoutersExpose the same CRUD surface through ModelViewSet and router registration

TL;DR: The API in Five Commands

The tutorial explains each file as you go. If you only want the shape of the workflow, it is this:

reinhardt-admin startproject tutorial --template rest
cd tutorial
reinhardt-admin startapp snippets --template rest
cargo make migrate
cargo make runserver

Then, from another terminal:

curl http://127.0.0.1:8000/api/snippets/

At first you will see an empty list because the database has no snippets yet:

{"snippets":[]}

The rest of this series fills in the code behind that response.