How can I retvieve the block-id for the location of an xblock

I’m currently working on an endpoint that should return the location of an XBlock. I know that the location can be viewed from the staff debug info, but I need to automate this process. Given a user from the request and a course ID from the URL, how can I programmatically obtain the block-id for an XBlock?

I’ve been searching for an appropriate function or method to retrieve this block-id, but haven’t had any luck so far. Any guidance or suggestions on how to achieve this would be greatly appreciated.

Thank you!