Overview
The API uses middleware to handle cross-cutting concerns like authentication, tenant validation, and request processing. Understanding these middleware components is essential for proper API integration.Authentication Middleware
Sanctum Authentication
All protected endpoints require authentication using Laravel Sanctum tokens.- User logs in via
/api/v2/admin/login - Server returns a Bearer token
- Include token in
Authorizationheader for subsequent requests - Token is validated on each request
Tenant Middleware
X-Tenant-ID Header Validation
Thetenant middleware ensures multi-tenancy isolation by validating the X-Tenant-ID header on protected routes.
How It Works
- Header Check: Validates that
X-Tenant-IDheader is present - Request Injection: Adds
tenant_idto the request object - Controller Access: Controllers can access via
$request->tenant_id
Protected Routes
The following route groups require theX-Tenant-ID header:
/api/v2/admin/products/*- Product management/api/v2/admin/orders/*- Order management- Any route with
->middleware('tenant')
Example Request
Error Response (400 Bad Request)
If theX-Tenant-ID header is missing:
Benefits of Tenant Middleware
- Data Isolation: Ensures users only access data from their tenant
- Security: Prevents cross-tenant data leakage
- Simplicity: Controllers automatically receive validated tenant_id
- Consistency: Centralized validation logic
Middleware Stack
Typical Request Flow
Applying Middleware to Routes
In Route Definitions
Best Practices
1. Always Include Required Headers
2. Handle Middleware Errors
3. Store Tenant ID Securely
4. Create API Client Helper
Troubleshooting
Missing X-Tenant-ID Header
Problem: Getting 400 error with “X-Tenant-ID header is required” Solution: Ensure the header is included in every request to tenant-protected routesInvalid Tenant ID
Problem: Getting empty results or 404 errors Solution: Verify the tenant ID is correct and the user has access to that tenantToken Expiration
Problem: Getting 401 errors after some time Solution: Implement token refresh or re-authentication flowRelated Documentation
- Multi-Tenancy - Understanding tenant architecture
- Authentication - Login and token management
- Getting Started - Initial setup guide