Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jimmyddalton's avatar

Consuming External API With Laravel 11

I'm relatively new to Laravel and have been searching everywhere for resources to learn how to integrate external resources into my Laravel project the "Laravel way" without adding more dependencies to the project. Everything thing I find is outdated, Guzzle, or related to handling incoming requests.

If anyone has any resources, I would be extremely grateful!

Looking for resources, tutorials, examples, etc. for:

  • Consuming External API resources within Laravel 11
    • I'm able to make basic HTTP requests just not sure on if I need to create a service provider, containers etc.
  • Best practices on organizing code
  • Rate limiting outbound requests to 3rd party APIs
0 likes
1 reply
LaryAI's avatar
Level 58

To consume external APIs in Laravel 11, you can use Laravel's built-in HTTP client, which is a wrapper around Guzzle. This allows you to make HTTP requests without adding additional dependencies. Here's a step-by-step guide on how to do it the "Laravel way":

Step 1: Making HTTP Requests

Laravel provides a fluent interface for making HTTP requests. You can use the Http facade to make requests to external APIs.

use Illuminate\Support\Facades\Http;

$response = Http::get('https://api.example.com/data');

if ($response->successful()) {
    $data = $response->json();
    // Process the data
} else {
    // Handle the error
}

Step 2: Organizing Code

To keep your code organized, consider the following best practices:

  1. Service Classes: Create a dedicated service class for handling API requests. This keeps your controllers clean and separates the logic.

    namespace App\Services;
    
    use Illuminate\Support\Facades\Http;
    
    class ApiService
    {
        public function fetchData()
        {
            $response = Http::get('https://api.example.com/data');
            return $response->json();
        }
    }
    
  2. Service Providers: If you have complex logic or need to bind interfaces to implementations, consider using a service provider. However, for simple API consumption, a service class is usually sufficient.

  3. Environment Configuration: Store API URLs and keys in your .env file and access them using config().

    API_URL=https://api.example.com
    API_KEY=yourapikey
    
    $response = Http::withHeaders([
        'Authorization' => 'Bearer ' . config('services.api.key'),
    ])->get(config('services.api.url') . '/data');
    

Step 3: Rate Limiting Outbound Requests

To rate limit outbound requests, you can use Laravel's built-in rate limiting features. Here's a simple example using a middleware:

  1. Create a Middleware:

    php artisan make:middleware RateLimitMiddleware
    
  2. Implement Rate Limiting Logic:

    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Support\Facades\RateLimiter;
    
    class RateLimitMiddleware
    {
        public function handle($request, Closure $next)
        {
            $key = 'api-rate-limit:' . $request->ip();
    
            if (RateLimiter::tooManyAttempts($key, 60)) {
                return response('Too many requests', 429);
            }
    
            RateLimiter::hit($key, 60);
    
            return $next($request);
        }
    }
    
  3. Register Middleware:

    Register the middleware in app/Http/Kernel.php.

    protected $routeMiddleware = [
        // ...
        'rate.limit' => \App\Http\Middleware\RateLimitMiddleware::class,
    ];
    
  4. Apply Middleware:

    Apply the middleware to routes or controllers that make API requests.

    Route::get('/api/data', [ApiController::class, 'getData'])->middleware('rate.limit');
    

By following these steps, you can effectively consume external APIs in Laravel 11 while keeping your code organized and adhering to best practices.

Please or to participate in this conversation.