Server to Server Endpoint request unauthorised

Theres your problem.

You locked the access token to that function, it’s not available to other functions to use. So it’s undefined.