Getting OctoPrint Server Port via Plugin

Can anyone point me in the direction of an injected property or other method/util function to determine the server's port (or api url) using python. I'm developing a plugin to proxy REST API commands via MQTT subscriptions and have it working correctly with a forced url of http://localhost:5000/api/... but I know that the port can change and want to program that in to my url builder.

HAPROXY is where the port will change to the rest of the network Octoprint directly uses 5000

I understand that, but the plugin is running on the same device so there is no need to go through ha proxy, so using localhost is perfect, just need a way to pull the active running port. I figured out I can run self._settings.global_get(["server","port"]) but I'm not sure if that gives me the actual port being used or just what's saved in config.yaml.

That only gives you what's in the config. Since the port can be influenced through startup parameters, you'll need to go the route of implementing the StartupPlugin mixin and then having the port handed to you by implementing its on_startup method (which gets bound address + port as parameters).

Thanks Gina, that seems to work with my testing. Another quick question related to the same plugin. Is there a setting anywhere that would allow the user to change the API url? I just want to make sure that the rest of the path won't change and the pattern http://localhost:<port>/api/ would be all I need to account for.

They could put things behind a reverse proxy, but within OctoPrint itself there's no way to change the endpoints.