import ipaddress

def build_api_url(service_host, tcp_port):
    try:
        ip = ipaddress.ip_address(service_host)
        if isinstance(ip, ipaddress.IPv6Address):
            api_url = 'https://[%s]:%s/api/v1/' % (service_host, tcp_port)
        else:
            api_url = 'https://%s:%s/api/v1/' % (service_host, tcp_port)
    except ValueError:
        # Not a valid IP address, assume it's a hostname
        api_url = 'https://%s:%s/api/v1/' % (service_host, tcp_port)
    return api_url
