Using webhook to trigger API get request

  • 0
  • 1
  • Question
  • Updated 1 month ago
  • Answered
Quickbase uses stupid mapbox - It is currently not supporting our country, so I have a workaround in terms of getting addresses sorted:  www.what3words.com

All our customers will have a unique 3 word address that is simply copied and pasted into a single field, I then want to trigger an API request to get the 3 word address translated into GPS co ordinates and entered into "Street 1, Street 2" of the Location type field.

Its pretty simple logic, but I just need help to get the format for the webhook and how to get the info into my table.

The API get request documentation: (https://docs.what3words.com/api/v2/#forward)

Here is the format of the request:

https://api.what3words.com/v2/forward?addr=[myfieldhere]&display=full&format=xml&key=[myapikeyhere]


So in the URL, I can easily use the required markers, however I don't know how to receive the payload back into a field in my record on Quickbase.

Any help here would be appreciated.  On or off world.
Photo of Geoffrey

Geoffrey

  • 1,980 Points 1k badge 2x thumb

Posted 1 year ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
This is trivial to do although I would suggest you don't use a webhook - just use script from your form. There is no point in making it more complicated by implementing it through a webhook.

Here is the code that takes QuickBase HQ's (150 Cambridgepark Dr, Cambridge, MA 02140) three word identifier (puns.chew.snow) and gets the JSON response and in particular the lat, lon and map url:
var apikey = "YOUR API KEY";
var addr = "puns.chew.snow";
var url = `https://api.what3words.com/v2/forward?addr=${addr}&display=full&format=json&key=${apikey...;
$.get(url)
  .then(function(json) {
    console.log(json.geometry.lat);
    console.log(json.geometry.lng);
    console.log(json.map);
    console.log(JSON.stringify(json, null, "  "));
  });

Here is the console output:
42.394445
-71.146089
http://w3w.co/puns.chew.snow
{
  "thanks": "Thanks from all of us at index.home.raft for using a what3words API",
  "crs": {
    "type": "link",
    "properties": {
      "href": "http://spatialreference.org/ref/epsg/4326/ogcwkt/";,
      "type": "ogcwkt"
    }
  },
  "words": "puns.chew.snow",
  "bounds": {
    "southwest": {
      "lng": -71.146107,
      "lat": 42.394432
    },
    "northeast": {
      "lng": -71.146071,
      "lat": 42.394459
    }
  },
  "geometry": {
    "lng": -71.146089,
    "lat": 42.394445
  },
  "language": "en",
  "map": "http://w3w.co/puns.chew.snow";,
  "status": {
    "reason": "OK",
    "status": 200
  }
}

And if you don't want to use jQuery here is the equivalent code using the Fetch API:

fetch(url)
  .then(res => res.json())
  .then((json) => {
    console.log(json.geometry.lat);
    console.log(json.geometry.lng);
    console.log(json.map);
    console.log(JSON.stringify(json, null, "  "));
  });
You can test this code from the console and then implement it using the IOL technique. Instead of logging the lat/lon you would just fill in a form value using code similar to this:
_fid_7.value = json.geometry.lat;
_fid_8.value = json.geometry.lng;
_fid_9.value = json.map;
(Edited)
Photo of Geoffrey

Geoffrey

  • 1,980 Points 1k badge 2x thumb
Thanks for this Dan.  I'll give it a bash and see how it comes out.
Photo of Geoffrey

Geoffrey

  • 1,980 Points 1k badge 2x thumb
So im stuck on this, is there anyone that can assist here? this code of Dans is not working.  

Any help is appreciated.






Photo of AustinK

AustinK

  • 1,176 Points 1k badge 2x thumb
Can you be more descriptive than it "is not working"? Which part of the code does not work? Did you try both jquery and the other option? Did you test it in the console first to make sure it worked before trying to implement it via IOL? Is the part that isn't working the part of the code that is not fully included, the part that fills the fields to the form?

If you can say which section of the code is failing then someone can possibly help.
(Edited)
Photo of Geoffrey

Geoffrey

  • 1,980 Points 1k badge 2x thumb
Hi Austin.  Thanks, my post was just to check if someone is able to assist on this since Dan is MIA.


So the first line of code (Jquery) doesn't seem to work, i'm not sure if there is a syntax error.  I tested in the console first.

Secondly, i'm not sure how to combine the 2 pieces of code (below) To pull the information and add into the quickbase native address type field.:

First one to fetch the info:
var apikey = "YOUR API KEY";
var addr = "puns.chew.snow";
var url = `https://api.what3words.com/v2/forward?addr=${addr}&display=full&format=json&key=${apikey...;
$.get(url)
  .then(function(json) {
    console.log(json.geometry.lat);
    console.log(json.geometry.lng);
    console.log(json.map);
    console.log(JSON.stringify(json, null, "  ")); });

And second to populate the field forms:

_fid_7.value = json.geometry.lat;
_fid_8.value = json.geometry.lng;  _fid_9.value = json.map;

I am thinking I should run the code in a formula url button, and making the script as follows:


var apikey = "YOUR API KEY";
var addr = [what3words address field];
var url = `https://api.what3words.com/v2/forward?addr=${addr}&display=full&format=json&key=${apikey...;
$.get(url)
  .then(function(json) {
    _fid_7.value = json.geometry.lat; _fid_8.value = json.geometry.lng; _fid_9.value = json.map; console.log(JSON.stringify(json, null, "  ")); });


But im messing the code up somewhere (not a developer here).  I would like for the script to run automatically using iol, but im not entirely sure how the script page would need to look



(Edited)
Photo of AustinK

AustinK

  • 1,176 Points 1k badge 2x thumb
Lets start with the easy stuff. You have gone to what3words and created an api key correct? You have to register an account and possibly more, it says it is free. Then you replace the "Your API Key" part with your API key.

You also will need to edit that URL it is using. This code is using the v2 API but they are on v3 now.

https://api.what3words.com/v3/convert-to-coordinates?words=filled.count.soap&key=[API-KEY]

You can try this code here and see if it works with your API key. It should console.log the data which you can see by hitting f12 if in Chrome and going to the console tab.

var apikey = "YOUR API KEY";
var addr = "puns.chew.snow";
var url = `https://api.what3words.com/v3/convert-to-coordinates?words=${addr}&format=json&key=${apikey....;
$.get(url)
  .then(function(json) {
    console.log(json.geometry.lat);
    console.log(json.geometry.lng);
    console.log(json.map);
    console.log(JSON.stringify(json, null, "  "));
});