Doing HTTP requests FROM Laravel to an external API
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Making HTTP Requests from Laravel to External APIs
Body:
Introduction
When developing web applications using Laravel, it is often necessary to interact with external APIs for data retrieval or integration purposes. In this comprehensive blog post, we will explore how to make efficient HTTP requests from a Laravel application to an external API. We will also discuss various techniques and best practices, including the use of packages and built-in functionalities in Laravel to improve efficiency and maintainability.Making Basic Requests
To begin with, let's consider making a simple GET request using Laravel. You can create a Controller method that handles the request and returns the desired JSON response. For example:<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
class ExternalApiController extends Controller
{
public function getDataFromExternalAPI(Request $request)
{
// Make an HTTP request to the external API using Guzzle (or another library)
$response = \GuzzleHttp\Client::get('https://example-api.com/v1/data');
// Process and return the response
if ($response->getStatusCode() === 200) {
$jsonResponse = json_decode($response->getBody(), true);
return response()->json($jsonResponse, 200);
} else {
return response()->json(['error' => 'Failed to fetch data'], 400);
}
}
}In this example, we are using Guzzle, a popular HTTP client library for PHP. It offers easy-to-use methods for making simple requests, such as get(), which can be called directly or within a try/catch block to handle errors gracefully. Additionally, you can utilize Laravel's built-in helper functions like response()->json() to easily return JSON responses.
Using Facades and Packages
While the previous approach might suffice for simple requests, it isn't the most efficient or maintainable way when dealing with more complex scenarios or multiple APIs. One possible solution is to use Laravel's HTTP facade. This provides a cleaner interface and better organization of code.First, create a new class (for example, ExternalApiClient) that extends Illuminate\Support\Facades:
<?php
namespace App\Services;
use GuzzleHttp\Client as GuzzleClient;
class ExternalApiClient extends \Illuminate\Support\Facades
{
/**
* Get the fully qualified class name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'external-api'; }
public function __callStatic($method, $parameters)
{
// If you are using Guzzle, set up a client instance here and pass it to the method call
$client = app(GuzzleClient::class);
return (new self)->$method($client, ...$parameters);
}
}This class allows you to create static methods that will be accessible through the facade. Now we can define our request methods within this class:
<?php
namespace App\Services;
use GuzzleHttp\Client as GuzzleClient;
class ExternalApiClient extends \Illuminate\Support\Facades
{
public function getDataFromExternalAPI($url)
{
// Make an HTTP request to the external API using Guzzle (or another library)
$response = \GuzzleHttp\Client::get($url);
// Process and return the response
if ($response->getStatusCode() === 200) {
$jsonResponse = json_decode($response->getBody(), true);
return response()->json($jsonResponse, 200);
} else {
return response()->json(['error' => 'Failed to fetch data'], 400);
}
}
}In this improved example, we have implemented a class that encapsulates the request logic and provides consistent methods. You can create custom facades for each external API you need to interact with, making it easier to manage and maintain your codebase. Additionally, by extending Illuminate\Support\Facades, your class will be automatically registered in the Laravel container, allowing you to access its methods without having to instantiate a new object.