Build Your First API in Under 1 Hour with SiroPHP

February 19, 20268 min readTutorial

How long does it take you to build a simple CRUD API? If you're like most developers, the answer is probably half a day or more. You spend hours writing boilerplate code: models, controllers, migrations, routes, validation, tests...

What if you could generate all of that in 30 seconds? Not a prototype. Not a skeleton. A fully functional, production-ready API with proper structure, validation, and tests.

With SiroPHP's intelligent scaffolding system, you can. Let me show you how to go from zero to a complete API in under an hour.

1. The Boilerplate Problem

Every CRUD API follows the same pattern. For a simple "products" resource, you need:

Traditional Workflow:

  • ✏️ Create migration file (5 min)
  • ✏️ Define schema with columns and indexes (5 min)
  • ✏️ Create model class (5 min)
  • ✏️ Add fillable fields, relationships, casts (5 min)
  • ✏️ Create controller (5 min)
  • ✏️ Write index, show, store, update, destroy methods (20 min)
  • ✏️ Add validation rules (10 min)
  • ✏️ Define routes (5 min)
  • ✏️ Write API tests (30 min)
  • ✏️ Test manually with Postman (15 min)

⏱️ Total time: ~1.5 - 2 hours for ONE resource
😫 Reality: Most projects have 5-10 resources = 7.5-20 hours

This is wasted time. You're not solving unique problems—you're writing repetitive code that follows predictable patterns. There's a better way.

2. Quick Start: Install & Setup (5 minutes)

Let's start from scratch and build a complete API project.

Step 1: Install SiroPHP

# Create new project
composer create-project sirosoft/api my-api

# Navigate to project
cd my-api

# Generate app key
php siro key:generate

Step 2: Configure Database

# Edit .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_api_db
DB_USERNAME=root
DB_PASSWORD=

# Run migrations
php siro migrate

Step 3: Start Development Server

php siro serve

# Server running at http://localhost:8080

Time elapsed: 5 minutes
🎯 Status: Project ready, server running

3. One-Command CRUD Generation (30 seconds)

Here's where the magic happens. Instead of manually creating files, use the scaffolding command:

# Generate complete CRUD for "products" resource
php siro make:crud products

# Output:
✓ Created: database/migrations/2026_02_19_create_products_table.php
✓ Created: app/Models/Product.php
✓ Created: app/Controllers/ProductController.php
✓ Updated: routes/api.php
✓ Created: tests/ProductApiTest.php

Generated 5 files in 0.3 seconds! 🚀

That's it. In less than a second, you have a fully functional CRUD API. Let's see what was generated.

4. What Gets Generated?

The scaffolding command creates everything you need for a production-ready API:

1. Database Migration

// database/migrations/create_products_table.php
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->text('description')->nullable();
    $table->decimal('price', 10, 2);
    $table->integer('stock')->default(0);
    $table->boolean('is_active')->default(true);
    $table->timestamps();
    $table->softDeletes();
});

2. Model with Relationships

// app/Models/Product.php
class Product extends Model
{
    protected $fillable = [
        'name',
        'description',
        'price',
        'stock',
        'is_active',
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'stock' => 'integer',
        'is_active' => 'boolean',
    ];

    // Soft deletes enabled by default
    use SoftDeletes;
}

3. Controller with Full CRUD

// app/Controllers/ProductController.php
class ProductController extends Controller
{
    // GET /api/products - List all products
    public function index(Request $request)
    {
        $products = Product::paginate($request->get('per_page', 20));
        return Response::json($products);
    }

    // GET /api/products/{id} - Get single product
    public function show(int $id)
    {
        $product = Product::findOrFail($id);
        return Response::json($product);
    }

    // POST /api/products - Create product
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'price' => 'required|numeric|min:0',
            'stock' => 'required|integer|min:0',
        ]);

        $product = Product::create($validated);
        return Response::json($product, 201);
    }

    // PUT /api/products/{id} - Update product
    public function update(Request $request, int $id)
    {
        $product = Product::findOrFail($id);
        
        $validated = $request->validate([
            'name' => 'sometimes|string|max:255',
            'price' => 'sometimes|numeric|min:0',
        ]);

        $product->update($validated);
        return Response::json($product);
    }

    // DELETE /api/products/{id} - Delete product
    public function destroy(int $id)
    {
        $product = Product::findOrFail($id);
        $product->delete();
        return Response::json(['message' => 'Deleted']);
    }
}

4. Routes Auto-Registered

// routes/api.php (automatically updated)
Route::apiResource('products', ProductController::class);

// Generates:
// GET    /api/products           → index
// GET    /api/products/{id}      → show
// POST   /api/products           → store
// PUT    /api/products/{id}      → update
// DELETE /api/products/{id}      → destroy

5. API Tests Included

// tests/ProductApiTest.php
class ProductApiTest extends TestCase
{
    public function test_can_list_products()
    {
        $response = $this->get('/api/products');
        $response->assertStatus(200);
    }

    public function test_can_create_product()
    {
        $response = $this->post('/api/products', [
            'name' => 'Test Product',
            'price' => 99.99,
            'stock' => 10,
        ]);
        $response->assertStatus(201);
    }

    // More tests included...
}

5. Customize & Extend

The generated code is just a starting point. Customize it to fit your needs:

Add Custom Fields

# Regenerate with custom fields
php siro make:crud products   --fields="name:string,description:text,price:decimal,sku:string,category_id:foreign"

# Or edit migration manually and re-run
php siro migrate:fresh

Add Relationships

// app/Models/Product.php
public function category(): BelongsTo
{
    return $this->belongsTo(Category::class);
}

public function reviews(): HasMany
{
    return $this->hasMany(Review::class);
}

// Use in controller
$products = Product::with('category', 'reviews')->paginate(20);

Add Custom Validation

// app/Controllers/ProductController.php
$validated = $request->validate([
    'name' => 'required|string|max:255|unique:products,name',
    'price' => 'required|numeric|min:0|max:999999.99',
    'sku' => 'required|string|unique:products,sku|regex:/^[A-Z]{3}-[0-9]{4}$/',
    'stock' => 'required|integer|min:0|max:99999',
]);

6. Building a Complete Project in 1 Hour

Let's build a complete e-commerce API with multiple resources. Here's the timeline:

0-5m

Project Setup

Install, configure database, run initial migration

5-10m

Generate Core Resources

Products, Categories, Users (3 commands, 30 seconds each)

10-20m

Add Relationships

Product belongsTo Category, User hasMany Orders, etc.

20-35m

Customize Business Logic

Add custom validation, filters, search, pagination

35-50m

Testing & Debugging

Run generated tests, fix issues, test with CLI tools

50-60m

Documentation & Deploy Prep

Generate API docs, review code, prepare for deployment

🎉 Result: Complete e-commerce API with 5+ resources, relationships, authentication, tests, and documentation—in under 1 hour.

Conclusion

Building APIs shouldn't be about writing boilerplate code. It should be about solving real business problems. SiroPHP's scaffolding system eliminates the repetitive work, letting you focus on what matters: your application's unique features.

With one command, you get:

  • Database migrations with proper schema
  • Models with relationships and soft deletes
  • Controllers with full CRUD operations
  • Auto-registered routes
  • API tests ready to run
  • Validation rules included

Stop wasting time on boilerplate. Start shipping APIs faster.

Ship APIs Faster with Scaffolding

Generate production-ready APIs in seconds, not hours