Laravel APIs & JSON Responses (Beginner Friendly)

riazul-islam
Riazul Islam
Published on Feb, 21 2026 2 min read 0 comments
image

📌 What is an API?

API (Application Programming Interface) allows different applications to communicate with each other.
In Laravel, APIs are usually JSON-based and follow RESTful principles.

Use Cases:

  • Mobile apps consuming Laravel backend
  • SPA (Single Page Applications) like Vue.js or React
  • Third-party integrations

⚙️ Step 1: Configure API Routes

API routes are defined in:

routes/api.php

Example:

Route::get('/posts', [App\Http\Controllers\Api\PostController::class, 'index']);
Route::get('/posts/{id}', [App\Http\Controllers\Api\PostController::class, 'show']);
  • /api/posts → GET all posts
  • /api/posts/{id} → GET single post by ID

Note: API routes are stateless (no sessions by default).

🔹 Step 2: Create API Controller

php artisan make:controller Api/PostController

app/Http/Controllers/Api/PostController.php:

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function index() {
        $posts = Post::all();
        return response()->json($posts, 200);
    }

    public function show($id) {
        $post = Post::find($id);
        if($post) {
            return response()->json($post, 200);
        } else {
            return response()->json(['message' => 'Post not found'], 404);
        }
    }
}
  • response()->json() → returns JSON response
  • Status codes: 200 → OK, 404 → Not Found

🔹 Step 3: Test API with Postman

  1. Open Postman
  2. GET request: http://127.0.0.1:8000/api/posts
  3. GET request: http://127.0.0.1:8000/api/posts/1

You should see JSON output like:

[
  {
    "id": 1,
    "title": "Laravel CRUD Tutorial",
    "body": "Learn Laravel CRUD step by step",
    "created_at": "2025-12-27T00:00:00",
    "updated_at": "2025-12-27T00:00:00"
  }
]

🔹 Step 4: Create New Post via API

POST request to /api/posts:

{
    "title": "New API Post",
    "body": "This post is created via API"
}

Controller method:

public function store(Request $request) {
    $request->validate([
        'title' => 'required',
        'body' => 'required'
    ]);

    $post = Post::create($request->all());
    return response()->json($post, 201);
}
  • 201 → Created status

🔹 Step 5: Update Post via API

PUT request to /api/posts/{id}:

public function update(Request $request, $id) {
    $post = Post::find($id);
    if(!$post) {
        return response()->json(['message'=>'Post not found'], 404);
    }
    $post->update($request->all());
    return response()->json($post, 200);
}

🔹 Step 6: Delete Post via API

DELETE request to /api/posts/{id}:

public function destroy($id) {
    $post = Post::find($id);
    if(!$post){
        return response()->json(['message'=>'Post not found'], 404);
    }
    $post->delete();
    return response()->json(['message'=>'Post deleted'], 200);
}

🔹 Step 7: API Resource Classes (Optional, Clean Output)

Generate resource:

php artisan make:resource PostResource

app/Http/Resources/PostResource.php:

public function toArray($request) {
    return [
        'id' => $this->id,
        'title' => $this->title,
        'body' => $this->body,
        'created_at' => $this->created_at->format('Y-m-d H:i'),
    ];
}

Controller:

use App\Http\Resources\PostResource;

public function index() {
    $posts = Post::all();
    return PostResource::collection($posts);
}
  • Ensures consistent API response format

📌 Key Takeaways

This week you learned:

  • How to create API routes in Laravel
  • How to return JSON responses
  • How to CRUD via API (GET, POST, PUT, DELETE)
  • Optional: API Resources for clean JSON responses
  • How to test APIs using Postman

 

0 Comments