Dart Frog Tutorial Part 2: Building Your First Real REST API (Full CRUD with Todos) 🐸

Published: (January 13, 2026 at 04:07 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

Planning & Best Practices

Quick plan – we’ll create a Todo model with id, title, and completed status, stored in‑memory (a Map for fast lookup). This keeps the example simple while being easy to replace with a real database later.

Best practices

  • Use dynamic routes with [id].dart
  • Generate unique IDs with the uuid package
  • Validate JSON request bodies
  • Return appropriate HTTP status codes: 200, 201, 204, 400, 404, 405

Setup

Add the UUID dependency

# pubspec.yaml
dependencies:
  uuid: ^4.5.0

Run the package manager:

dart pub get   # or flutter pub get

Todo Model

Create lib/src/todo.dart:

/// Represents a Todo item.
class Todo {
  /// Creates a new Todo.
  Todo({
    required this.id,
    required this.title,
    this.isCompleted = false,
  });

  /// Creates a Todo from a JSON map.
  factory Todo.fromJson(Map json) => Todo(
        id: json['id'] as String,
        title: json['title'] as String,
        isCompleted: json['isCompleted'] as bool? ?? false,
      );

  /// Unique identifier.
  final String id;

  /// Title of the todo.
  final String title;

  /// Completion status.
  bool isCompleted;

  /// Converts the Todo to JSON.
  Map toJson() => {
        'id': id,
        'title': title,
        'isCompleted': isCompleted,
      };
}

In‑Memory Store

Create lib/src/todo_repository.dart:

import 'package:my_project/src/todo.dart';
import 'package:uuid/uuid.dart';

const _uuid = Uuid();
final _todos = {};

/// Returns all todos.
List getAllTodos() => _todos.values.toList();

/// Returns a todo by its [id] or `null` if not found.
Todo? getTodoById(String id) => _todos[id];

/// Creates a new todo with the given [title].
void createTodo(String title) {
  final id = _uuid.v4();
  _todos[id] = Todo(id: id, title: title);
}

/// Updates an existing todo.
void updateTodo(String id, {String? title, bool? isCompleted}) {
  final todo = _todos[id];
  if (todo == null) return;
  _todos[id] = Todo(
    id: id,
    title: title ?? todo.title,
    isCompleted: isCompleted ?? todo.isCompleted,
  );
}

/// Deletes a todo.
void deleteTodo(String id) => _todos.remove(id);

Routes

Collection route – routes/todos/index.dart

import 'package:dart_frog/dart_frog.dart';
import 'package:my_project/src/todo_repository.dart';

Future onRequest(RequestContext context) async {
  switch (context.request.method) {
    case HttpMethod.get:
      final todos = getAllTodos();
      return Response.json(
        body: todos.map((e) => e.toJson()).toList(),
      );

    case HttpMethod.post:
      final body = await context.request.json() as Map;
      final title = body['title'] as String?;
      if (title == null || title.isEmpty) {
        return Response(statusCode: 400, body: 'Title is required');
      }
      createTodo(title);
      return Response(statusCode: 201, body: 'Todo created');

    default:
      return Response(statusCode: 405);
  }
}

Dynamic item route – routes/todos/[id].dart

import 'package:dart_frog/dart_frog.dart';
import 'package:my_project/src/todo_repository.dart';

Future onRequest(RequestContext context, String id) async {
  final todo = getTodoById(id);
  if (todo == null) return Response(statusCode: 404);

  switch (context.request.method) {
    case HttpMethod.get:
      return Response.json(body: todo.toJson());

    case HttpMethod.put:
      final body = await context.request.json() as Map;
      final title = body['title'] as String?;
      final isCompleted = body['isCompleted'] as bool?;
      updateTodo(id, title: title, isCompleted: isCompleted);
      return Response.json(body: getTodoById(id)!.toJson());

    case HttpMethod.delete:
      deleteTodo(id);
      return Response(statusCode: 204);

    default:
      return Response(statusCode: 405);
  }
}

Testing

Quick curl examples:

# Get all todos
curl http://localhost:8080/todos

# Create a new todo
curl -X POST http://localhost:8080/todos \
  -H "Content-Type: application/json" \
  -d '{"title": "Learn Dart Frog"}'

# Get a specific todo (replace <id> with the actual id)
curl http://localhost:8080/todos/<id>

The API handles errors gracefully and provides a solid production‑ready foundation.

Source Code

Show some ❤️ by starring the repo and following the author:
https://github.com/techwithsam/dart_frog_full_course_tutorial

This is your first real Dart Frog REST API — congrats! Next up: connect a Flutter app to it.

Back to Blog

Related posts

Read more »

Todo App

Introduction After completing my first logic‑focused project Counters, I wanted to take the next natural step in complexity — not by improving the UI, but by c...

ALL ABOUT REST API

Forem Feed !Forem Logohttps://media2.dev.to/dynamic/image/width=65,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.co...