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:

  2. And then draw the map using Mapbox Static Image API:

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