Making RapidAPI calls in Laravel Controller using HTTP client

I’m having trouble making API calls to RapidAPI from within my Laravel controller. I tried using unirest but keep getting class not found errors. Is there a better way to handle external API requests in Laravel? Any suggestions would be helpful.

Here’s what I’m working with:

public function process(Request $request)
{
    $validatedData = $request->validate([
        'first_name' => ['required', 'string', 'max:100'],
        'second_name' => ['required', 'string', 'max:100'],
    ]);

    // Store the data
    Profile::create($validatedData);

    // Try to make API call
    $apiHeaders = ['Accept' => 'application/json'];
    
    $apiResponse = Unirest\Request::post("RAPIDAPI_ENDPOINT", [
        "X-RapidAPI-Key" => "MY_API_KEY"
    ]);
    
    var_dump($apiResponse);
    
    return view('dashboard');
}

Getting this error:

Class ‘App\Http\Controllers\Unirest\Request’ not found

What’s the correct approach for consuming RapidAPI endpoints in Laravel?

yeah, that unirest error happens cuz u haven’t installed it via composer yet. use laravel’s http client instead - way easier and no extra packages needed. don’t forget the x-rapidapi-host header tho, rapidapi needs both headers to work.

Skip third-party libraries and use Laravel’s native HTTP client. I hit similar issues when I started with RapidAPI endpoints - external packages like Unirest create more problems than they solve. The built-in client does everything you need.

Here’s how to structure it:

use Illuminate\Support\Facades\Http;

public function process(Request $request)
{
    $validatedData = $request->validate([
        'first_name' => ['required', 'string', 'max:100'],
        'second_name' => ['required', 'string', 'max:100'],
    ]);

    Profile::create($validatedData);

    $response = Http::withHeaders([
        'X-RapidAPI-Key' => env('RAPIDAPI_KEY'),
        'X-RapidAPI-Host' => 'your-endpoint-host.rapidapi.com'
    ])->post('https://your-rapidapi-endpoint.com/api', [
        'data' => $validatedData
    ]);

    if ($response->successful()) {
        $apiData = $response->json();
        // Process your response
    }

    return view('dashboard');
}

This kills dependency issues and works perfectly with Laravel’s error handling. I’ve used this pattern on multiple RapidAPI integrations - zero problems.

That error happens because Unirest isn’t installed properly or the namespace is wrong. Just use Laravel’s built-in HTTP client instead - it’s cleaner for RapidAPI calls. I’ve used it on several RapidAPI integrations and it works great.

Swap your Unirest code for this:

$response = Http::withHeaders([
    'X-RapidAPI-Key' => 'YOUR_API_KEY',
    'X-RapidAPI-Host' => 'your-api-host.rapidapi.com'
])->post('RAPIDAPI_ENDPOINT', $yourData);

$result = $response->json();

Don’t forget use Illuminate\Support\Facades\Http; at the top of your controller. The HTTP facade handles errors way better than third-party libraries. Also, put your API key in the .env file for security.