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

catto's avatar
Level 1

How to authenticate when consume api in Laravel?

I'm trying to integate api service into my laravel web app. From login & every operation in my web app is using the api. But I always get response 401 unauthorized whenever I hit the api endpoint.

First of all I logged in using this request to api, I can successfully got the response.

 public function login($user_id, $password)
    {
        try {
            $guzzle = new Client();
            $url = config('app.base_api_url') . 'auth/signin';
            $credentials = base64_encode($user_id.':'.$password);

            $response = $guzzle->post($url, [
                'Authorization' => ['Basic '.$credentials],
                'form_params' => [
                    'userid' => $user_id,
                    'password' => $password,
                ],

            ]);

            return json_decode($response->getBody());
        } catch (\Exception $e) {
            throw $e;
        }
    }

The response from api above is like this:

{
    "user": {
        "status": "success",
        "message": "user successfully loggedin",
        "payload": {
            "userid": "user001",
            "role": "R.5",
            "name": "User",
            "title": "",
            "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJzZXJ0aWZpa2FzaWhhbGFsaXBiQGdtYWlsLmNvbSIsIm5hbWUiOiJMUEggSVBCIiwiaWF0IjoxNjU2MzgzMTExLCJleHAiOjE2NTYzODMxNDEsImF1ZCI6InNpaGFsYWwuYnBqcGguZ28uaWQiLCJpc3MiOiJzaWhhbGFsLmJwanBoLmdvLmlkIn0.vfceMwXAAMoTlcrGZHLn72y_sg5J1Rz4_SFmhwOiGTc",
            "refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJzZXJ0aWZpa2FzaWhhbGFsaXBiQGdtYWlsLmNvbSIsIm5hbWUiOiJMUEggSVBCIiwiaWF0IjoxNjU2MzgzMTExLCJleHAiOjE2NTY5ODc5MTEsImF1ZCI6InNpaGFsYWwuYnBqcGguZ28uaWQiLCJpc3MiOiJzaWhhbGFsLmJwanBoLmdvLmlkIn0.Wa06POKEvjKoN28CPHjTiDahUSMvAc_2PRrIMpv9NBg"
        }
    }
}

And then I want to get the data from api, but I always got 401 Unauthorized, Error: You are not logged in response. (I hard coded the token only for development purpose)

public function getCost($page, $order_dir, $limit)
    {
        try {
            $guzzle = new Client();
            $url = config('app.base_api_url') . 'api/v1/costs';
            $token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJzZXJ0aWZpa2FzaWhhbGFsaXBiQGdtYWlsLmNvbSIsIm5hbWUiOiJMUEggSVBCIiwiaWF0IjoxNjU2MzgzMTExLCJleHAiOjE2NTYzODMxNDEsImF1ZCI6InNpaGFsYWwuYnBqcGguZ28uaWQiLCJpc3MiOiJzaWhhbGFsLmJwanBoLmdvLmlkIn0.vfceMwXAAMoTlcrGZHLn72y_sg5J1Rz4_SFmhwOiGTc";

            $response = $guzzle->get($url, [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
                'form_params' => [
                    'page' => $page,
                    'order_dir' => $order_dir,
                    'limit' => $limit,
                ],

            ]);

            return json_decode($response->getBody());
        } catch (\Exception $e) {
            throw $e;
        }
    }

I don't know what's wrong with my code. Did I have to logged in the user manually or something like that? I'm stuck with this since yesterday. Any help would be very helpful for me

0 likes
6 replies
Joeszeto's avatar

have you try using this api token and send the same request from postman ?

Joeszeto's avatar

@catto if you get the same result even using postman, that means this is the api provider (the guy) problem.

catto's avatar
Level 1

@Joeszeto If I'm using my local url I got 401 unauthorized, but when I'm using sandbox url from the api provider and test it in postman I can get the data. But somehow when using sandbox url, as long as I logged in I can get the data without using the token, is that possible to get the data without token? btw the api is made using node js

Joeszeto's avatar

@catto make sure the api is using token base auth not session base (cookie base)? and have you set the header is postman, something like accept application/json

iftekhs's avatar

What middlewares are you using when you try to get data.

Please or to participate in this conversation.