Skip to content

Repository files navigation

REST API (Laravel)

This is a simple Laravel-based REST API project that supports token-based authentication (via Laravel Sanctum) along with full CRUD operations for products. It's built to demonstrate how to structure a clean, secure REST API in Laravel.

What's included

  • Authentication — Register, login, logout, and fetch the currently logged-in user's details (powered by Sanctum tokens).
  • Products CRUD — Create, list, view, update, and delete products.
  • Protected routes — Product and user-related routes are only accessible to authenticated users.

Getting it running

  1. Clone the repo

    git clone <repo-url>
    cd rest-api
  2. Install dependencies

    composer install
  3. Set up your environment file

    cp .env.example .env

    Open .env and update your database credentials (DB_DATABASE, DB_USERNAME, DB_PASSWORD).

  4. Generate the app key

    php artisan key:generate
  5. Run the migrations (this creates the users, products, and personal_access_tokens tables, among others)

    php artisan migrate
  6. Start the server

    php artisan serve

    The API will now be available at http://127.0.0.1:8000/api.

API Endpoints (quick overview)

Method Endpoint Description Auth required?
POST /api/register Register a new user No
POST /api/login Log in and get a token No
POST /api/logout Log out Yes
POST /api/user Get logged-in user's details Yes
GET /api/products List all products Yes
POST /api/products Create a new product Yes
GET /api/products/{id} View a single product Yes
PUT /api/products/{id} Update a product Yes
DELETE /api/products/{id} Delete a product Yes

For every authenticated request, pass the token in the header like this:

Authorization: Bearer <your-token>

You'll get this token back in the response after logging in.

About

Laravel REST API with Sanctum token authentication and full CRUD operations for products.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages