Laravel lets you send HTTP requests to external APIs using a simple built-in client. It supports clean syntax for passing data, headers and handling responses.
You can connect to any third-party service in just a few lines of code fast, secure and reliable. Ready to see it in action?
Why External API Requests Matter in Laravel
Laravel apps often rely on external APIs to extend functionality and connect with third-party platforms.
Common use cases include:
- Sending messages through services like Twilio or WhatsApp
- Retrieving weather, location or currency data
- Processing payments via Stripe or Razorpay
- Syncing data with CRMs or SaaS tools like HubSpot or Zoom
Setting Up Laravel’s HTTP Client
Laravel’s HTTP client is available out of the box from version 7 onwards and uses Guzzle under the hood.
To get started, import the facade at the top of your controller
use Illuminate\Support\Facades\Http;
Sending a Basic GET Request
A basic GET request in Laravel requires just one line. Here’s how to fetch data from an external API
$response = Http::get('https://api.example.com/users');
To include query parameters, pass them as an array:
$response = Http::get('https://api.example.com/users', [ 'limit' => 10,
'sort' => 'name'
]);
For structured data, use Laravel’s built-in response parser:
$data = $response->json(); // returns array
Making a POST Request with Data
To send data to an external API, use the Http::asJson()->send() approach. Here's how you can pass a payload:
$response = Http::asJson()->post('https://api.example.com/create-user', [ 'name' => 'John Doe',
'email' => '[email protected]'
]);
You can also include authentication headers if the API requires it:
$response = Http::withHeaders([ 'Authorization' => 'Bearer ' . config('services.api.token'),
])->asJson()->post('https://api.example.com/create-user', [
'name' => 'Jane Doe',
'email' => '[email protected]'
]);
Using baseUrl() for Cleaner API Code
When calling the same API multiple times, baseUrl() helps clean up repeated URLs:
$client = Http::baseUrl('https://api.example.com/v1');
$response1 = $client->get('users');
$response2 = $client->post('users/create', [
'name' => 'Alex',
'email' => '[email protected]'
]);
Also Read: Custom Validation Rules in Laravel 5.5
Handling Responses & Errors in Laravel
Laravel provides several built-in methods to check if an API call succeeded or failed:
$response = Http::get('https://api.example.com/data');
if ($response->successful()) {
$data = $response->json();
} elseif ($response->clientError()) {
// 4xx error
} elseif ($response->serverError()) {
// 5xx error
}
For added safety, wrap requests in a try-catch block to handle connection issues:
try { $response = Http::timeout(5)->get('https://api.example.com/data');
$data = $response->json();
} catch (\Exception $e) {
// Handle timeout or network errors
}
Example: Calling a Weather API in Laravel
Here’s a simple example using a public weather API with Laravel’s HTTP client:
use Illuminate\Support\Facades\Http;
$response = Http::get('https://api.weatherapi.com/v1/current.json', [
'key' => 'your_api_key',
'q' => 'New York'
]);
$data = $response->json();
$temp = $data['current']['temp_c'];
$condition = $data['current']['condition']['text'];
Conclusion
Laravel’s HTTP client makes working with third-party APIs fast, clean and developer-friendly. You can fetch, send and decode data in just a few readable lines.
Stick to best practices like clear endpoints, structured data handling and proper error checks to keep your integration smooth.