# API Timeout – Backend (Laravel) Notes

The mobile app uses a **15-second request timeout** for all API calls. To avoid timeouts and improve UX:

## Recommendations

1. **Keep responses under ~10 seconds**  
   If an endpoint often takes longer, consider:
   - Optimizing queries (indexes, eager loading, reducing payload).
   - Moving heavy work to queues (e.g. `php artisan queue:work`).

2. **PHP / web server limits**  
   Ensure limits are at least as high as the app timeout (e.g. 20s) so the server doesn’t cut the request before the client:
   - **PHP:** `max_execution_time` in `php.ini` (e.g. 30).
   - **Nginx:** `fastcgi_read_timeout` (e.g. 30s).
   - **Apache:** `Timeout` directive if needed.

3. **Logging slow requests (optional)**  
   Add middleware to log requests that exceed a threshold, e.g. 10s:

   ```php
   // app/Http/Middleware/LogSlowRequests.php
   public function handle($request, Closure $next)
   {
       $start = microtime(true);
       $response = $next($request);
       $duration = (microtime(true) - $start) * 1000;
       if ($duration > 10000) {
           \Log::warning('Slow API request', [
               'url' => $request->fullUrl(),
               'method' => $request->method(),
               'duration_ms' => round($duration),
           ]);
       }
       return $response;
   }
   ```

   Register in `bootstrap/app.php` or `Kernel.php` and add to the `api` middleware group if you use it.

4. **Health check**  
   The app can call `GET /api/v1/health` with a short timeout to check connectivity. Keep this endpoint fast (no heavy DB or external calls).

## Axios timeout (mobile app)

- **Default:** 15 seconds for all requests (`API_TIMEOUT_MS` in `services/api.ts`).
- **On timeout:** User sees “Connection issue” with message and a **Retry** option where applicable (e.g. Home, Cart).
- **Logging:** Timeouts are logged as `[API Timeout] METHOD url` for debugging.
