Bagisto Hosting
On this page

Table of Content

Building a REST API with Laravel from Scratch

If you’ve ever built a mobile app, a Vue/React frontend, or a third-party integration — you needed a REST API.

Laravel makes building one surprisingly clean. Let me walk you through it from zero.

What is a REST API in Laravel?

A REST API lets your Laravel app send and receive data over HTTP — usually JSON.

Instead of returning a view, your controller returns data. The frontend or mobile app handles the display.

ChatGPT-Image-Jul-15-2026-11_31_38-AM

Step 1: Set up a fresh Laravel project


That’s your app running at http://localhost:8000. Now let’s build the API on top of it.

Step 2: Create the model, migration, and controller

We’ll build a simple Posts API as the example.


The -mcr flag creates the model, migration, and a resource controller in one command.

Open the migration and add the fields:


Run it:

Step 3: Define your REST API routes in Laravel

Open routes/api.php — this is where all API routes live.


One line gives you all 5 routes: index, store, show, update, destroy.

Check them with:


All routes are automatically prefixed with /api/ — so /api/posts, /api/posts/{id}, etc.

Step 4: Write the controller logic

Open app/Http/Controllers/PostController.php and fill in the methods:


Also add fillable to the Post model:

Step 5: Format responses with API Resources

Raw Eloquent output exposes every column. Use API Resources to control what gets returned.



Now wrap your controller responses:


Clean, consistent output — no surprise fields leaking to the client.

Step 6: Add authentication to your REST API with Laravel Sanctum

Sanctum is Laravel’s lightweight token-based auth — perfect for REST APIs.


Add the HasApiTokens trait to your User model:


Create a login route that issues a token:


Protect your routes with the auth:sanctum middleware:


Now every request needs an Authorization: Bearer {token} header.

For a deeper look at token-based auth, see our guide on Laravel Sanctum API authentication.

Step 7: Handle errors properly

By default Laravel returns HTML for errors. For APIs you want JSON.

Add this to app/Exceptions/Handler.php:


Now 404s and validation errors return clean JSON — not an HTML error page.

 

ChatGPT-Image-Jul-15-2026-11_33_24-AM

Quick tips before you ship

  • Always version your API — use Route::prefix('v1') from day one
  • Never return raw Eloquent collections — always use API Resources
  • Use response()->noContent() (204) for delete endpoints, not null
  • Paginate list endpoints — never return all rows at once
  • Test every endpoint with Postman or Insomnia before going live

For handling background tasks in your API — like sending emails after a POST — check out our guide on Laravel Queues from zero to production.

Quick recap

  • Use routes/api.php and Route::apiResource() for clean route setup
  • API Resources control exactly what data gets returned
  • Sanctum handles token auth with minimal setup
  • Always return JSON errors — not HTML
  • Version from day one — you’ll thank yourself later

That’s a fully working REST API in Laravel — routes, resources, auth, and error handling.

Start with the Posts example, then replace it with whatever your app actually needs.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home