Making RapidAPI calls in Laravel Controller - Class not found issue

I’m having trouble making API calls to RapidAPI from my Laravel controller. I keep getting a class not found error when trying to use the HTTP client library. Can someone help me figure out what’s wrong with my setup?

Here’s what I’m trying to do in my controller:

public function handleSubmission(Request $request)
{
    Profile::create(
        request()->validate([
            'first_name' => ['required','max:255'],
            'last_name' => ['required','max:255'],
        ],
        [
            'first_name.required' => 'First name is required',
            'last_name.required' => 'Last name is required'
        ]));

        $requestHeaders = array('Accept' => 'application/json');

        $apiResponse = Unirest\Request::post("RAPIDAPI_ENDPOINT",
          array(
            "X-RapidAPI-Key" => "MY_API_KEY"
          )
        );
            dd($apiResponse);

        return view("success");
}

The error I’m getting:

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

Is there a better way to make RapidAPI calls in Laravel? Maybe using Guzzle or the built-in HTTP client? Any suggestions would be appreciated!

i think u forgot to import Unirest. try adding use Unirest\Request; at the top of your controller. but honestly, using Laravel’s Http facade is better! Http::post() is much more straightforward and cleaner.

That error means Unirest isn’t installed or autoloaded properly. But honestly? Skip Unirest entirely and use Laravel’s built-in HTTP client instead - it’s way more reliable. Just swap your API call for something like Http::withHeaders(['X-RapidAPI-Key' => 'YOUR_KEY'])->post('RAPIDAPI_ENDPOINT', $data). Don’t forget to add use Illuminate\Support\Facades\Http; at the top of your controller. I’ve been hitting RapidAPI with this method for over a year now - zero issues. Way better error handling than third-party stuff.

Your namespace resolution is broken. You’re calling Unirest\Request, but Laravel’s looking for it under your controller’s namespace. Just drop Unirest - it’s basically dead anyway. Use Laravel’s built-in HTTP client instead (it’s been solid since v7). Here’s how: $response = Http::withHeaders(['X-RapidAPI-Key' => env('RAPIDAPI_KEY')])->post($endpoint, $payload);. Don’t forget to import the Http facade and throw your API key in the .env file. I’ve switched tons of projects off Unirest and Laravel’s HTTP client handles errors and responses way better.