How to get current user id in Api_Action

Hi,
It seems CurrentUserID() method does not work in an api_action.
How to use it?
Thanks

It depends on how you use your API action. If you did not enable the JWT middleware for your action, the JWT token is not checked and the user is not logged in. You may search “JWTMiddleware” in this forum.

Hi mansour,

Do let me know whether you manage to solve this?

I added the below middleware but CurrentUserID() also returns empty. I wonder if there is additional steps that I need to do after adding the middleware?

$app->get('/hello/{name}', function ($request, $response, $args) {
//...
})->add(JwtMiddleware::class);

By the way, it works in v2025 without adding the JwtMiddleware when I login as admin. The CurrentUserID() will return -1 as expected but it doesn’t work in v2024 even after I added the JwtMiddleware.

if (IsLoggedIn()) {
    $userID = CurrentUserID();
}

Hi dear yinsw,
Unfortunately not for now. I’m searching for a quick fix.
Thanks