SyntaxStudy
Sign Up
REST API Designing RESTful Endpoints
REST API Beginner 1 min read

Designing RESTful Endpoints

A RESTful endpoint maps a URL + method pair to an action on a resource. Endpoints should be consistent: every resource collection follows the same pattern, and every error response has the same shape. Consistency reduces cognitive load for API consumers and simplifies client code generation. Controller organisation in frameworks like Laravel maps directly to REST conventions. A single resource controller handles index, show, store, update, and destroy actions. Route resource declarations generate all five endpoints from one line, enforcing the naming convention automatically.
Example
# Laravel resource routes (routes/api.php)
Route::apiResource('products', ProductController::class);
# Generates:
# GET    /products           -> index
# POST   /products           -> store
# GET    /products/{id}      -> show
# PUT    /products/{id}      -> update
# PATCH  /products/{id}      -> update
# DELETE /products/{id}      -> destroy

# Nested resources
Route::apiResource('users.orders', UserOrderController::class);
# GET    /users/{user}/orders          -> index
# POST   /users/{user}/orders          -> store
# GET    /users/{user}/orders/{order}  -> show

# Custom action endpoints
Route::post('/orders/{order}/cancel',  [OrderController::class, 'cancel']);
Route::post('/users/{user}/verify',    [UserController::class, 'verify']);

# app/Http/Controllers/ProductController.php (skeleton)
class ProductController extends Controller {
  public function index(Request $req): JsonResponse {
    return response()->json(Product::paginate(20));
  }
  public function store(StoreProductRequest $req): JsonResponse {
    $product = Product::create($req->validated());
    return response()->json($product, 201);
  }
  public function show(Product $product): JsonResponse {
    return response()->json($product);
  }
  public function update(UpdateProductRequest $req, Product $product): JsonResponse {
    $product->update($req->validated());
    return response()->json($product);
  }
  public function destroy(Product $product): JsonResponse {
    $product->delete();
    return response()->json(null, 204);
  }
}