Help

Re: update longitude and latitude

1111 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

hello everyone, sorry if my english is not very good, i will try to explain my problem to you.
I would like to retrieve the longitude and latitude through a script on airtable using an api but i dont know how to do it, can you help me please i am really stuck.
thank you

12 Replies 12

Hi @Tarik_TALEB, and welcome to the community!

Well, start with a function that makes the API call like this:

//
// post data
//
async function postData(url = '', data = {})
{
    const response = await fetch(url, {
        method: 'GET'
    });
    return response.json();
}

Call it with something like this:

await postData(apiEndpoint)
    .then(data => {
        ...
    }
    .err {
        ...
   });

Make sense?

Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

my api returns this :

{
“ip”:“109.9.171.148”,
“type”:“ipv4”,
“continent_code”:“EU”,
“continent_name”:“Europe”,
“country_code”:“FR”,
“country_name”:“France”,
“region_code”:“IDF”,
“region_name”:"\u00cele-de-France",
“city”:“Champs-sur-Marne”,
“zip”:“77420”,
“latitude”:48.852149963378906,
“longitude”:2.6002299785614014,
“location”:{
“geoname_id”:3027014,
“capital”:“Paris”,
“languages”:[
{
“code”:“fr”,
“name”:“French”,
“native”:“Fran\u00e7ais”
}
],
“country_flag”:“http://assets.ipapi.com/flags/fr.svg”,
“country_flag_emoji”:"\ud83c\uddeb\ud83c\uddf7",
“country_flag_emoji_unicode”:“U+1F1EB U+1F1F7”,
“calling_code”:“33”,
“is_eu”:true
}
}

if i understood i need to use the api to retrieve the data, then i put it in my form, knowing that i need the latitude and longitude.

example :

url_api : http://api.ipapi.com/109.9.171.148?access_key=76454ca6de838ca36eb63f1c72130&format=1

the code :

//
// post data
//
async function postData(url = url_api , data = {})
{
const response = await fetch(url, {
method: ‘GET’
});
return response.json();
}

afterwards I don’t know what to put in data, what do I put the values ​​I want to retrieve or all the values ​​that the API returns.

Thank you for your reply

Recap – the script that calls the API looks like this…

await postData(apiEndpoint)
    .then(data => {
        ...
    }
    .err {
        ...
   });

The variable “data” will contain the JSON payload returned by the API. So, accessing lat/lng from your sample response payload would look something like this:

await postData(apiEndpoint)
    .then(data => {
        let lat = data.latitude;
        let lng = data.longitude;
    }
    .err {
        ...
   });
Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

I just tested the code, I have an error:

ERROR

TypeError: Failed to fetch

at Failed to fetch

I saw that you have already answered this problem, I have the impression that it is a recurring problem.

How so? What problem, exactly, are you referring?

A failed fetch means that your code is simply not working. You have a bug somewhere, but unless you can share it, I cannot be of much assistance.

Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

Hello, excuse me for the delay in responding.
I give you the screens of a very simple code for the recovery of the json from the API.
This is what the API returns to me:
api

This is what the airtble script returns to me (in the script I only display the json)

retourApi

thank you

And how [exactly] are you able to see that response? Performing a GET from a browser using this URL?

http://api.ipapi.com/109.9.181.118?access_key=76454ca6de838ca36eb4c963f1c72130&format=1

If so, you cannot expect this to run in a script in Airtable (or any system for that matter) because the service level you’ve signed up for at ipapi.com doesn’t provide HTTPS, a key security requirement for anyone engaged in API integration via Airtable.

Until such time as you enable HTTPS on that account, you will spin your wheels attempting to make this work.

Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

Thank you for your help, i just understood i will try to find a more secure api.

It’s not a question of finding a more secure API - you have found one. You simply need to upgrade the service.

Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

Hello, I have tried with another API.
here is the code:

fetch(“https://ip-geolocation-ipwhois-io.p.rapidapi.com/json/”, {

"method": "GET",

"headers": {

    "x-rapidapi-key": "c7bd31779emshe2f681be6a371a4p111f02jsn3cbcfa506bae",

    "x-rapidapi-host": "ip-geolocation-ipwhois-io.p.rapidapi.com"

}

})

.then(response => {

console.log(response);

})

.catch(err => {

console.error(err);

});

the problem is that airtable returns nothing to me

Works fine for me. The problem is not Airtable - it’s your code. You need to do something to show the response from the API call.

image

output.markdown('# Rapid API Example');

let host      = "ip-geolocation-ipwhois-io.p.rapidapi.com"; 
let accessKey = "c7bd31779emshe2f681be6a371a4p111f02jsn3cbcfa506bae";
let url       = "https://ip-geolocation-ipwhois-io.p.rapidapi.com/json/";

let options   = {
    "method": "GET",
    "headers": {
        "x-rapidapi-key": accessKey,
        "x-rapidapi-host":host
    }
}

await fetch(url, options)
    .then((resp) => resp.json())
    .then(function(data) {
        output.inspect(data);
    })
    .catch(function(e) {
        output.markdown(e.message);
    });
Tarik_TALEB
4 - Data Explorer
4 - Data Explorer

Thanks for your help