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.
- 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.
-
Clone the repo
git clone <repo-url> cd rest-api
-
Install dependencies
composer install
-
Set up your environment file
cp .env.example .env
Open
.envand update your database credentials (DB_DATABASE,DB_USERNAME,DB_PASSWORD). -
Generate the app key
php artisan key:generate
-
Run the migrations (this creates the
users,products, andpersonal_access_tokenstables, among others)php artisan migrate
-
Start the server
php artisan serve
The API will now be available at
http://127.0.0.1:8000/api.
| 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.