Populate Knack Object with External source via External Source API

Hi,

is it possible to populate a Knack object from an external source API?

I would like to use the https://coinmarketcap.com/api/ to auto populate a knack object, both on IDE and runtime when user submits a button to refresh information.

Any help would be extremely grateful since I'm lost on this one.

Thanks

If you required a professional help then please contact me on Skype : sunny_singla04 or email me on ssingla1985@gmail.com

Hello Bruno,

 

No need to Set limit and delay and instead of  'POST' First we can get all object data and check one by one if exist then call 'PUT' if not exist then call 'POST' . 

To overcome setTimeout functionality you just need to call method one by one, after one method success then call next record below this method .

 

Regards,

Sunny Singla 

 

Sunny thanks for the reply

I did it :)

But to bypass the API transaction limits I had to put in a delay, which turns the routine a little slow ( I'm refreshing 1000 records )

Also, don't quite know how to auto delete all records prior to refresh the information, since this code will add another set of records to the existing ones, and what I need is to replace them all.

Another step would be to run this every hour, don't think Knack can handle this level of customization yet

//CODE#1 Button to get coinmarketcap values via Json
$(document).on('knack-scene-render.scene_1', function(event, scene) {

$('#view_1 .view-header').after('<div style="padding:5px"><a href="#" id="refreshcmc" class="kn-button">Update From Coinmarketcap</a></div>');

$('#refreshcmc').click(function(event) {
event.preventDefault();
// get data from coinmarketcap (Json)

function loadJSON(callback) {

var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'https://api.coinmarketcap.com/v1/ticker/?limit=1000', true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {

// .open will NOT return a value but simply returns undefined in async mode so use a callback
callback(xobj.responseText);

}
}
xobj.send(null);

}

loadJSON(function(response) {
// Do Something with the response e.g.
jsonresponse = JSON.parse(response);

var data = {};

// set the delay to prevent hitting Knack API rate limit (milliseconds)
var myDelay = 100;

//Knack.showSpinner();
for (var i = 0, len = jsonresponse.length; i < len; i++) {


data.field_6 = jsonresponse[i].id;
data.field_7 = jsonresponse[i].name;
data.field_8 = jsonresponse[i].symbol;
data.field_9 = jsonresponse[i].rank;
data.field_10 = jsonresponse[i].price_usd;
data.field_11 = jsonresponse[i].price_btc;
//data.field_12 = jsonresponse[0].24h_volume_usd;
data.field_13 = jsonresponse[i].market_cap_usd;
data.field_14 = jsonresponse[i].available_supply;
data.field_18 = jsonresponse[i].total_supply;
data.field_19 = jsonresponse[i].max_supply;
data.field_15 = jsonresponse[i].percent_change_1h;
data.field_16 = jsonresponse[i].percent_change_24h;
data.field_17 = jsonresponse[i].percent_change_7d;
data.field_20 = jsonresponse[i].last_updated;


setTimeout(runapi(data), myDelay);

}; // for
// Knack.hideSpinner();
alert('All information refreshed');

function runapi(data) {
$.ajax({
url: "https://us-api.knack.com/v1/objects/object_2/records/",
type: "POST",
headers: {
"X-Knack-Application-Id": "***********",
"X-Knack-REST-API-Key": "***********"
},
data: data,
success: function(response) {
//console.log('Checks updated!!!');
}
});

}


});


});
});

 

 

Hello Bruno,

It's simple you can just call ajax call to update knack object like below

 

$.ajax({
url: "https://us-api.knack.com/v1/objects/" + Knack_Object_id + "/records/" + id,
type: 'PUT',
data: data,
headers: {
'X-Knack-Application-Id': API_ID,
'X-Knack-REST-API-Key': API_Key
},
success: function (response) {

}

});

Hope this help otherwise you can contact me on skype : sunny_singla04

Thanks 

ok, so here what I accomplish so far:

I've created a button on a form to refresh information from the external source, it's a json file, and that part is already working, I can read it's contents, now I just don't know how to use this data to update my object, I want to replace all data on my object with this Json records.

$(document).on('knack-scene-render.scene_1', function(event, scene) {

$('#view_1 .view-header').after('<div style="padding:5px"><a href="#" id="refreshcmc" class="kn-button">Update From Coinmarketcap</a></div>');

$('#refreshcmc').click(function(event) {
event.preventDefault();
// get data from coinmarketcap (Json)

function loadJSON(callback) {

var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'https://api.coinmarketcap.com/v1/ticker/', true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {

// .open will NOT return a value but simply returns undefined in async mode so use a callback
callback(xobj.responseText);

}
}
xobj.send(null);

}

loadJSON(function(response) {
   jsonresponse = JSON.parse(response);

alert(jsonresponse[0].name);  // JUST TO CHECK IF DATA HAS PASSED
});

});
});