Avatar

Is it possible to create a record to a kintone app from outside of kintone?

Afief

Dear Developers

What I mean is, if for example you create a localhost javascript application, and from that javascript application you want to submit a record to a kintone app you have access to, although I know it can be provided by using API-TOKEN, but I still having doubt about the proper way to do it. Probably using something like kintone proxy, or I don't know.

Please help, any answer would be appreciated, thank you!

Follow 4 followers
1

4 comments

Avatar
Austin Do 2 votes

Afief,

You want to post a record from a non-Kintone domain to a Kintone one, correct? In this case, kintone.api() may not work as your JavaScript file will not be attached to your app. I would recommend using either Microsoft Azure or AWS Lambda to make a function app, then triggering that from your frontend script. I tried this out myself using an HTTPTrigger function with Azure. Here's code for the function app:

And you can just trigger the function with jQuery.get() or AngularJS $http from your JavaScript Application.

Hope this helped, and let me know if you have any follow-up questions.

 

2
Comment actions Permalink
Avatar
Junko Werner 0 votes

As Yuzo mentioned, when you want to add records from outside Kintone, you use the POST method.
However, JS samples in the website referred by Yuzo use kintone.api, so that the kintone.api cannot be used in environments outside Kintone.
Also, Curl cannot be used from JS.

Therefore, if you are running the Kintone API from an external JS,
you will need to use like XMLHttpRequest and jQuery.ajax to run the API.

Here is a sample coding to add a record using XMLHttpRequest.

var body = { 
"app": 1,
"record": {
"Text": {
"value": "ABC"
}
},
// CSRF TOKEN: It is necessary to set it when API (POST, PUT, DELETE) is executed from kintone.
"__REQUEST_TOKEN__": kintone.getRequestToken()
};


var url = 'https://{subdomain}.kintone.com/k/v1/record.json';
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
// success
console.log(JSON.parse(xhr.responseText));
} else {
// error
console.log(JSON.parse(xhr.responseText));
}
};
xhr.send(JSON.stringify(body));

I found the following HTML sample using jQuery.ajax but for GET method.

(function() {
$.ajax({
type: 'GET',
url: 'https://subdomain.kintone.com/k/v1/records.json?app=appID',
success: function(data) {
console.log(data);
}
});
})();
0
Comment actions Permalink
Avatar
Austin Do 3 votes

Junko's answer is good for posting a record from one Kintone app to another (for example, kintone.getRequestToken() from the first snippet won’t work unless the JavaScript file is attached to a Kintone app), but both AJAX and XMLHttpRequest follow the same-origin policy (https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) and ordinarily don't allow cross-domain requests -- this can be bypassed with Azure or AWS Lambda, which run code from the server side. Here is a tutorial on getting record data using Azure: https://developer.kintone.io/hc/en-us/articles/115008801168

3
Comment actions Permalink