Accessing a dockerized api from both server and client side running on another container

So here is the situation: I have a docker compose file which starts a rest api and a next.js app. From the next.js app I want to access the api on both server side and client side. The problem: On the server side I have access to the api using the service name as the base url (ex: http://api-service) cause it’s calling from another container inside the same docker network. And the client side I have to access to the api using localhost (ex: http://localhost) as the base url cause it’s calling from the host. So every time i make a call I have to check if I’m on server side or on client side to set the right base url which is not ideal. To overcome that problem I added 172.0.0.1 api-service to the hosts file on host but still seems not ideal enough to me.

Is there a better way to deal with this type of situation?
Thanks