How to encode polylines in Google Sheets?

I’m using OSM and Mapbox API to draw static images of custom maps. To draw lines on the map requires them to be encoded as polylines, can this be done within Google Sheets?

Background: My users create ordered travel itineraries by selecting a number of items. I’m using Mapbox’ Static Image API to create a custom map showing their choices but cannot figure out how to encode the coordinates into polylines to connect these dots.

  1. I first get the lat/lon of an address from OSM API:
    =ImportXML("https://nominatim.openstreetmap.org/search?q="&D2&"&format=xml&addressdetails=1","//place[1]/@lon"),""))

  2. And then draw the map using Mapbox Static Image API:
    ="https://api.mapbox.com/styles/v1/mapbox/outdoors-v11/static/"&Markers&"/"&Center&","&Zoom&",0/"&Width&"x"&Height&"?access_token=X"

  3. I’m stuck adding the line overlay as it requires polylines.