Openstreetmap: Get ‘best’ tile for a given bounded box

I’ve been experimenting with the OSM tile server (https://tile.openstreetmap.org/) and have been converting lat/long to tile coordinates for embedding the tile into a web page.

However, what I actually need to do is find the ‘best’ tile for a given bounded box. I currently have four coords for the min and max lat and long. What I would like to get is the most ‘zoomed in’ tile that includes these coordinates.

This may look like:

  1. Calculate the average coordinate for the centre of the tile
  2. Find the right zoom level to include all bbox coordinates

Is this possible? (I’m working in JS but all answers gratefully received)