Generate the coordinates of an address so that you can save it to Airtable and use it for the Map Component.
MapBox API
This script uses the MapBox API → you can create a free account and generate a token here :
Create an Account
Generate a Token
Create a token with the default settings - you don’t need to change anything.
Leave the tab open as you will need to come back and copy the token in later steps.
Airtable Setup
To trigger your automation, we recommed using a checkbox initially during testing - then later
Record / Field
Setup a new field with a checkbox, then check it for one record.
Trigger
Then create an automation with the trigger → when record matches conditions → [checkbox] is checked.
Run Script
After the trigger create a Run Script step.
Copy Script
Copy the script below. This script sends an address to Mapbox and returns the latitude and longitude coordinates of the given address.
/**
* This script returns the latitude and longitude of a given address by querying Mapbox through its API.
* It accepts Mapbox API access token and the Address field
*
* To get an access token, please go to
* https://account.mapbox.com/access-tokens
*/
const {address} = input.config();
const urlParams = new URLSearchParams({
q: address,
access_token: "YOUR_MAPBOX_API_TOKEN",
limit: "1"
});
const stringifiedUrlParams = urlParams?.toString();
const APIUrl = `https://api.mapbox.com/search/geocode/v6/forward?${stringifiedUrlParams}`;
const result = await fetch(
APIUrl,
{
method: "GET",
}
)
.then(res => res.json())
.catch(err => { throw err });
const latitude = result?.features?.[0]?.properties?.coordinates?.latitude;
const longitude = result?.features?.[0]?.properties?.coordinates?.longitude;
//Set the longitude and latitude as the outputs. Use this to save the coordinates to a table/view.
output.set("latitude", latitude)
output.set("longitude", longitude)
Input Variable → Address
We will need to put the address into the script so it can be converted into coordinates.
To do this we use an input variable → make sure it has the Name as address.
Mapbox Token
Copy the mapbox token and paste it into the script.
Test your Script
Click the test button and you should see it run successfully with input as the address and output as two coordinates.
Update Record
In the next step update the record Latitude and Longitude fields → using the outputs from the script.
Result
Once the script works, you’ll have coordinates you can plug into your map component.
Run Automatically
When you have confirmed it works for your records by triggering them with checkboxes - switch to an automation flow that works for your use case.
On Demand : when [coordinates] is empty and [address] is not empty → good for systems where new records are added from external sources.
Interval : every [day] find records where [same condition as above] and repeating group [run the script] then [update record] → good for systems where the addresses are added manually.