Man… how silly would you feel if I told you Batocera already has a ready-to-use API that can be controlled via HTTP requests and even has its own webUI which you can visit on your browser for management?
https://wiki.batocera.org/es-webserver
It can be used to navigate to certain games, retrieve your current game list and of course launch/close games.
However, this webUI is not intended to actually be playing the games itself from. You’ll need your controller plugged into the device and to use that.