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

synergy's avatar

API to download file from another laravel project

I have 2 laravel projects on different server.

I'm building an API on Project1 to download pdf file and feed to Project2.

So users on Project2 will be able to download files from Project1 via API calls from application.

// Project1 (API)
public function downloadPDF($id)
    {
        $model = Record::find($id);
        $filename = $model->file_number . '.pdf';
        $pdf = storage_path('files/' . $filename);
        return response()->download($pdf);
    }
// Project2
public function preview($path)
    {
        $client = new \GuzzleHttp\Client;
        $response = $client->request('GET', 'api/record/download/' . $path);
        dd($response->getBody());
    }

The response status is 200 (ok). However, when I attempt to read the getBody() or getBody()->getContents(), it returns empty string.

0 likes
3 replies
davidifranco's avatar

As a suggestion, instead of trying to return the file from the api. return a temporary signed url to the file on your api server. First, create a web route on your api server to download the file. Then return the signed url like so:

URL::temporarySignedRoute(
    'download', now()->addMinutes(30), ['id' => 1]
);

Make sure to either attach the “signed” middleware to the web route or manually check that the request has a valid Signature.

https://laravel.com/docs/7.x/urls#signed-urls

synergy's avatar

Never thought of that! Will try out your suggestion. Thanks!

Please or to participate in this conversation.