Getting the deployment URL programmatically

I’m looking to programmatically get the deployment URL in my backend code in order to send it as a parameter to a third party service. Is there a way I can get this URL into an environment variable automatically during deployment similar to how XDN_ENVIRONMENT_NAME is set? Or is there another option for getting it?

Note: I’m trying to get the URL without access to req or window.

Can you expand on your motivation? Why not just use the host header on the request? You could also add a variable to the environment.