HTTP methods
- GET – fetch/read data saja
- POST – create resource (data object) utk request
- PUT – utk update. biasa utk upload file
- PATCH – utk update. update some from all resources
- DELETE
sample of API end point.
verb / main resource (normally plural) / id
GET /products/12
headers, requests and response
- request
- conist of. headers and body
- all allowed http methods (GET, POST..)
- can contain parameters (data, body)
- response
- consist of headers and body
- uniformed output (json)
- http status code (success/ok 200, 400 not found, failed etc)
- headers
- store/host all metadata including authentication key
Authentication
- basic auth – username and password, encoded with base64
- login page, user based auth
- api token – bearer token per user
- use case: login page, user based auth with abilities, mobile app
- api key – server generated token/key to auth
- in configuration file.
- app to api, api to api, backend comm, controlled env
- oauth 2.0 – combination of username/pass and token
- use case – login page, user-based auth with scopes, mobile app
- bearer auth – using bearer token under authorization header
authentication and authorization
- authentication – identify user legit or not
- authorization – apa scope yang dia boleh buat
laravel
- laravel sanctum – cover api token
- laravel passport – cover oauth 2.0
best practice
- atomic/singularity principle
- specific objective for every function
- follow standard and consistent naming conventions
- adheres to http method
- endpoint using nouns instead of verb
- response using json formatted structure
- uniformed response structure with appropriate status code
- using versioning to mitigate backward incompatibility
- always validate input and property handle errors
- capture errors in logs for easier troubleshoot/debug
- provide good support documentation (open api?)
laravel routes
- api – specific utk api
- web – utk web app
php artisan
- php artisan make:controller invoiceController –api
- php artisan route:list
- php artisan make:model Invoice –migration
- php artisan migrate
invoice
- InvoiceController, InvoiceCommand, InvoiceGateway
untuk postman, kena add header Accept application/json

GET hantar guna params
POST define dalam body
middleware = app/http/Kernel.php
X-API-KEY = if start with X, consider as custom param
CRUD, middleware
ways to consume API
php artisan make:controller InvoiceControler –resource