kintone REST API Request

 

Introduction

The kintone REST API Request allows you to run kintone REST APIs from your JavaScript code.
You'll find that you are limited to several actions when just completely relying on the JavaScript API - for example, you'll need to use the kintone REST API if you need to retrieve data of all the records inside your app.
This is where you will use the kintone REST API Request, to run the REST API.

kintone REST API Requestkintone.api()

REST APIs with the GET, POST, PUT, DELETE method can be used.

Function

kintone.api(pathOrUrl, method, params, callback, opt_errback)

Request Parameters

PARAMETER VALUE REQUIRED DESCRIPTION
pathOrUrl String Yes The path of the kintone REST API, or the URL obtained with kintone.api.url .
If the URL of the API is https://{subdomain}.kintone.com/k/v1/xxx.json, then specify the parameter as /k/v1/xxx. If the app is to be used inside a guest space, specify the parameter as kintone.api.url("/k/v1/xxx", true).
method String Yes The HTTP method. Specify one of the following: GET / POST / PUT / DELETE.
params Object Yes The parameters to be used with the API, specified as an object.
callback Function Optional The callback function called when the API succeeds.
The parameter for this function is an object.
If ignored, a kintone.Promise object will be returned that can be fulfilled with the parameter passed to the callback.
errback Function Optional The callback function called when the API fails.
The parameter for this function is a JSON response. If the JSON response cannot be parsed, an unparsed string will be given.
If the callback is ignored, a kintone.Promise object will be returned that can be rejected with the parameter passed to the errback.

Response

A kintone.Promise object will be returned if the callback parameter is ignored. Otherwise, there will be no response

Sample Request

kintone.events.on('app.record.index.show', function(event) {
	
	//The function called on success
	var callback = function(event){
		alert(event['record']['text_0']['value']);
	}

	//The function called on error
	var opt_errback = function(event){
		var errmsg = 'There was an error when retrieving the data';     
		if (event.message !== undefined){
			errmsg += '\n' + event.message;
		}
		alert(errmsg);
	}

	//kintone REST API Request calling the kintone Get Record API
	kintone.api('/k/v1/record', 'GET', {app: 1, id: 7}, callback, opt_errback);
});

Sample Request using Promises

kintone.events.on('app.record.create.submit', function(event) {
	return kintone.api('/k/v1/records', 'GET',  {app: 20}).then(function(resp) {
		alert("First pop-up");
	}).then(function(){
		alert("Second pop-up");//This should appear after the function before is resolved.
	}).catch(function(error) {
		var errmsg = 'There was an error when retrieving the data';      
  		 //If an error is included in the response message, show it
  		 if (error.message !== undefined){
  		 	errmsg += '\n' + error.message;
  		 }
  		 alert(errmsg);
  		});
});

Limitations

File upload/downloads cannot be run. You will need to retrieve the CSRF token, and run an HTTP request.

Get URLkintone.api.url()

Returns a URL from a shortened API path.

Function

kintone.api.url(path,detectGuestSpace)

Request Parameters

PARAMETER VALUE REQUIRED DESCRIPTION
path String Yes The kintone REST API path string beginning with /.
If the URL of the API is https://{subdomain}.kintone.com/k/v1/xxx.json, then specify the parameter as /k/v1/xxx.
opt_detectGuestSpace Boolean Optional If this is set to true, and is used in a guest space app, the URI of the guest space will be returned. Default is false.

Response

A URL string.

Sample Response

https://{subdomain}.kintone.com/k/v1/records.json

Sample Request

var url = kintone.api.url('/k/v1/records');
console.log(url);

Get URL (including query)kintone.api.urlForGet()

Returns a URL including a query string, from an API path and parameters.

Function

kintone.api.urlForGet(path, params,opt_detectGuestSpace)

Request Parameters

PARAMETER VALUE REQUIRED DESCRIPTION
path String Yes The kintone REST API path string beginning with /
If the URL of the API is https://{subdomain}.kintone.com/k/v1/xxx.json, then specify the parameter as /k/v1/xxx.
params Object Yes The parameters to be used with the API, specified as an object.
opt_detctGuestSpace Boolean Optional If this is set to true, and is used in a guest space app, the URI of the guest space will be returned. Default is false.

Response

A URL string including a query string, with URL encoded parameters.

Sample Response

https://{subdomain}.kintone.com/k/v1/records.json?app=7&fields[0]=text_0

Sample Request

var urlForGet = kintone.api.urlForGet('/k/v1/records', {app: 7,fields: ["text_0"]});
console.log(decodeURIComponent(urlForGet));

Get CSRF Tokenkintone.getRequestToken()

Retrieves a CSRF token to be used for file downloads/uploads.
This can be used for all APIs that have an HTTP method of POST, PUT and DELETE. APIs with an HTTP method of GET do not need a CSRF token.

Function

kintone.getRequestToken()

Request Parameters

None

Response

A string.

Available Period

86400 seconds (1 day) from the last access.

Sample Response

19324485-251e-2f6h-9332-824c9933g53e

Sample Request

var token = kintone.getRequestToken();
console.log(token); 

Sample for APIs using Query Strings

https://{subdomain}.kintone.com/k/v1/records.json?__REQUEST_TOKEN__=19324485-251e-2f6h-9332-824c9933g53e

Sample for APIs using JSON

{
    "__REQUEST_TOKEN__": "19324485-251e-2f6h-9332-824c9933g53e"
}

Request headers needed with the APIs

Header Content
X-Requested-With XMLHttpRequest
Content-Type application/json
Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.