What is a REST API?
A REST API (Representational State Transfer Application Programming Interface) is a set of rules and conventions for building web services that allow communication between clients (frontend, mobile apps) and servers. It follows REST (a software architectural style) to ensure scalability, performance, and maintainability.
Key Principles of REST API
- 
Client-Server Architecture - The frontend (client) and backend (server) communicate independently over HTTP.
 
- 
Statelessness - Each request from a client contains all the necessary information; the server does not store session state.
 
- 
Cacheable - Responses can be cached to improve performance.
 
- 
Uniform Interface - Uses standard HTTP methods (GET,POST,PUT,DELETE) and formats (like JSON).
 
- Uses standard HTTP methods (
- 
Resource-Based - Everything is treated as a resource (e.g., Users, Products, Orders), and each resource has a unique URL.
 
REST API Methods & Examples
| HTTP Method | Action | Example URL | Description | 
|---|---|---|---|
| GET | Read | /api/properties | Fetch all properties | 
| GET | Read | /api/properties/1 | Fetch a single property (id = 1) | 
| POST | Create | /api/properties | Add a new property | 
| PUT | Update | /api/properties/1 | Update property (id = 1) | 
| DELETE | Delete | /api/properties/1 | Remove property (id = 1) | 
REST API Example in Laravel
Here’s a simple API endpoint for a Property Listing System:
Route::apiResource('properties', PropertyController::class);
- GET /api/properties→ Returns all properties
- POST /api/properties→ Adds a new property
- GET /api/properties/1→ Returns property with id 1
- PUT /api/properties/1→ Updates property with id 1
- DELETE /api/properties/1→ Deletes property with id 1
Why Use REST API?
✅ Platform Independent (Works with React, Vue, Android, iOS, etc.)
✅ Scalable & Lightweight
✅ Follows Web Standards (HTTP, JSON, etc.)
✅ Easier to Integrate & Maintain
Create REST APIs in laravel
Building REST APIs in Laravel involves setting up routes, controllers, models, and middleware. Below is a step-by-step guide to creating a RESTful API in Laravel 11. Lets first understand what is REST API.
1. Install Laravel 11 (if not already installed)
composer create-project --prefer-dist laravel/laravel myapi cd myapi
2. Configure Database
Update your .env file with database credentials:
DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=myapi_db 
DB_USERNAME=root 
DB_PASSWORD=secret
Run migration:
php artisan migrate
3. Create a Model & Migration
php artisan make:model Property -m
Modify the generated migration file in database/migrations/xxxx_xx_xx_xxxxxx_create_properties_table.php:
public function up() {
    Schema::create('properties', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('description');
        $table->decimal('price', 10, 2);
        $table->string('location');
        $table->timestamps();
    }); 
}
Run the migration:
php artisan migrate
4. Create Controller
php artisan make:controller PropertyController --api
Modify app/Http/Controllers/PropertyController.php:
namespace App\Http\Controllers;
use App\Models\Property; 
use Illuminate\Http\Request;
class PropertyController extends Controller {     
    
    // Get all properties     
    public function index()
    {
        return response()->json(Property::all(), 200);
    }
    // Store a new property
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'required|string',
            'price' => 'required|numeric',
            'location' => 'required|string',
        ]);
        $property = Property::create($validated);
        return response()->json($property, 201);
    }
    // Get a single property
    public function show(Property $property)
    {
        return response()->json($property, 200);
    }
    // Update a property
    public function update(Request $request, Property $property)
    {
        $property->update($request->all());
        return response()->json($property, 200);
    }
    // Delete a property
    public function destroy(Property $property)
    {
        $property->delete();
        return response()->json(null, 204);
    } 
}
5. Define API Routes
Modify routes/api.php:
use App\Http\Controllers\PropertyController;
Route::apiResource('properties', PropertyController::class);
6. Test API using Postman or cURL
Start Laravel’s built-in server:
php artisan serve
Now test your API endpoints:
- 
GET all properties curl -X GET http://127.0.0.1:8000/api/properties
- 
POST a new property curl -X POST http://127.0.0.1:8000/api/properties \ -H "Content-Type: application/json" \ -d '{"title": "Luxury Apartment", "description": "3BHK Sea Facing", "price": 1500000, "location": "Miami"}'
- 
GET a single property curl -X GET http://127.0.0.1:8000/api/properties/1
- 
PUT update property curl -X PUT http://127.0.0.1:8000/api/properties/1 \ -H "Content-Type: application/json" \ -d '{"title": "Updated Title"}'
- 
DELETE a property curl -X DELETE http://127.0.0.1:8000/api/properties/1
7. Enable API Authentication (Optional)
If authentication is required, use Laravel Sanctum:
composer require laravel/sanctum 
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" 
php artisan migrate
Then, add use HasApiTokens; in the User model and register Sanctum middleware in app/Http/Kernel.php.
Next Steps
- Implement API pagination using paginate()
- Add error handling with custom responses
- Implement authentication with Laravel Sanctum or Passport
- Connect API to frontend (React, Vue, or mobile apps)