Build Your First Maat Application
In this guide, you will create a Maat application with a small JSON endpoint, validate its input, and test the complete request flow. You only need the Dart SDK—Maat does not require Node.js, reflection, or code generation.
Create the Application
Install Ptah, Maat’s project installer, and create an application named blog:
dart pub global activate ptahmaat new blogcd blogPtah installs the application’s Dart packages, creates .env, and generates
an application key. The generated project already includes a health route and
a passing feature test.
Add a Validated Route
Replace routes/api.dart with the following code:
import 'package:maat/maat.dart';
/// Routes served under `/api`.void apiRoutes() { Route.get('/health', (Request request) => {'status': 'ok'});
Route.post('/posts', (Request request) async { final data = await request.validate({'title': 'required|string|max:120'});
return Response.json({'data': data}, status: 201); });}The route is available at /api/posts because the generated
RouteServiceProvider applies the /api prefix. request.validate() returns
only validated fields and automatically produces a 422 JSON response when
the title is missing or longer than 120 characters.
Test the Endpoint
Create test/feature/posts_test.dart:
import 'package:maat/testing.dart';import 'package:test/test.dart';
import '../../bootstrap/app.dart';
void main() { test('creates a validated post', () async { final client = TestClient(await createApp());
(await client.postJson('/api/posts', { 'title': 'First Maat post', })).assertCreated().assertJson({ 'data': {'title': 'First Maat post'}, });
(await client.postJson('/api/posts', { 'title': '', })).assertUnprocessable().assertJsonValidationErrors(['title']); });}Run the test:
dart test test/feature/posts_test.dartTestClient sends requests directly through Maat’s HTTP kernel, so the test
does not open a port or depend on an external server.
Run the Application
Start the development server:
maat serveIn another terminal, send a valid request:
curl -X POST http://127.0.0.1:8000/api/posts \ -H 'content-type: application/json' \ -d '{"title":"First Maat post"}'Maat returns a 201 Created response:
{"data":{"title":"First Maat post"}}Where to Go Next
- Learn how Maat maps requests in Routing.
- Move route logic into Controllers.
- Explore the complete rule set in Validation.
- Persist posts with Seshat ORM.