Get logged in user id in api calls
If the user is not logged into your app and if you don't let anonymous users to access the API, how can you let them to log in without accessing API?

What you can do is you can maintain a API key for your application. You should call the API using the key all the time. That should be your first check point. Then you should add that API key validation for accessing the API. Then you can give access to your login API function using that key.

Once user is successfully login to the system you can return a dynamically genarated(random) session key to your APP so that the user login to the app and he/she has a session key. From that point call the API using the session key and add validation method to validate both session key and API key.

So all API functions are accessed by logged in users only. When the user logs out from the app call a session key function to remove your key.

I think you are looking for this kind of a solution.