Your First API
Building your first API with ZinTrust is fast and intuitive. In this guide, we'll create a simple "Task" API.
1. Create the Model and Migration
Use the CLI to generate a model and its corresponding migration:
bash
zin add model Task --migrationEdit the migration in database/migrations/ to add a title and completed status:
typescript
import { MigrationSchema, type IDatabase } from '@zintrust/core';
export interface Migration {
up(db: IDatabase): Promise\<void>;
down(db: IDatabase): Promise\<void>;
}
export const migration: Migration = {
async up(db: IDatabase): Promise\<void> {
const schema = MigrationSchema.create(db);
await schema.create('tasks', (table) => {
table.id();
table.string('title');
table.boolean('completed').default(false);
table.timestamps();
});
},
async down(db: IDatabase): Promise\<void> {
const schema = MigrationSchema.create(db);
await schema.dropIfExists('tasks');
},
};Run the migration:
bash
zin migrateIf your project uses Cloudflare D1 (DB_CONNECTION=d1 or d1-remote), use:
bash
zin migrate --local --database zintrust_db2. Create the Controller
Generate a controller for your tasks:
bash
zin add controller TaskControllerImplement the index and store methods:
typescript
import { Task } from '@app/Models/Task';
import { Controller, type IRequest, type IResponse } from '@zintrust/core';
export const TaskController = {
async index(_req: IRequest, res: IResponse): Promise\<void> {
const tasks = await Task.query().get();
Controller.json(res, { data: tasks });
},
async store(req: IRequest, res: IResponse): Promise\<void> {
const task = Task.create(req.getBody() as Record\<string, unknown>);
await task.save();
Controller.json(res, { data: task }, 201);
},
};3. Register the Routes
Add the routes to routes/api.ts:
typescript
import { Router, type IRouter } from '@zintrust/core';
import { TaskController } from '@app/Controllers/TaskController';
export function registerRoutes(router: IRouter): void {
Router.get(router, '/tasks', TaskController.index);
Router.post(router, '/tasks', TaskController.store);
}4. Test Your API
Start the development server:
bash
zin startYou can now send a POST request to http://localhost:7777/tasks to create a task, and a GET request to see all tasks.