Skip to main content
In this tutorial, you’ll build a complete JSON API for managing todos. You’ll learn how to create routes, controllers, and interact with the database.

What We’re Building

A REST API with these endpoints:

Prerequisites

Make sure you have a Kit project created:

Step 1: Create the Migration

First, create a migration for the todos table:
Edit the generated migration file in migrations/:
Run the migration and sync entities:

Step 2: Create the Controller

Create a controller for handling todo operations:
Edit src/controllers/todos.rs:

Step 3: Define Routes

Add the routes in src/routes.rs:
The routes! macro automatically generates a register() function that returns a configured Router.

Step 4: Test the API

Start the server:

Create a Todo

Response:

List All Todos

Response:

Get a Single Todo

Update a Todo

Delete a Todo

Adding Validation

The #[request] attribute automatically handles validation using the validator crate. When validation fails, Kit returns a 422 response with Laravel/Inertia-compatible error format:
If validation fails, the response looks like:

Summary

You’ve built a complete CRUD API with:
  • Database migrations for the todos table
  • A controller with index, show, store, update, and destroy actions
  • RESTful routes following conventions
  • JSON responses with proper error handling

Next Steps

  • Add authentication middleware to protect routes
  • Implement pagination for the index endpoint
  • Add filtering and sorting capabilities
  • Create an Inertia frontend (see Inertia Todo Tutorial)