Adding Records (Multiple)
kintone offers a rich set of APIs that allow you to directly manipulate application data from your programs.
In particular, the REST API is a powerful tool for manipulating kintone data through HTTP requests.
This page provides a detailed explanation of how to add multiple records using the kintone REST API.
If you want to add only one record, the endpoint is different. Please refer to the following page for details.
Using from kintone
When executing the REST API as a JavaScript customization in kintone, you can easily execute the REST API by using the provided JavaScript API.
By using the kintone.api.url
method of the JavaScript API, you can obtain the endpoint of the REST API without being aware of the space in which it is executed.
/**
* Add multiple records to an app using the kintone REST API
*
* @param { Object } params
* @param { string | number } params.app - App ID
* @param { Record<string, any>[] } params.records - Information of the records to be added
* @return { Promise<{ id: string[]; revision: "1"[] }> } - IDs and revision numbers of the added records
*/
const addRecords = (params) => {
const { app, records } = params;
return kintone.api(kintone.api.url('/k/v1/records.json', true), 'POST', { app, records });
};
Note that when adding only one record, specify record.json
, but when adding multiple records, specify records.json
.
Specify the app ID to which the records will be added and the information of the records to be added as arguments.
If the addition is successful, an object with id
and revision
properties will be returned.
(() => {
const addRecords = (params) => {
/** Omitted */
};
kintone.events.on(['app.record.detail.show'], async (event) => {
const appId = kintone.app.getId();
await addRecord({
app: appId,
records: new Array(10).fill('').map((record, i) => ({ FieldCode: { value: i } })),
});
return event;
});
})();
(() => {
const addRecords = (params) => {
/** Omitted */
};
kintone.events.on(['app.record.index.show'], async (event) => {
const input = prompt('Please enter the record information to be registered');
const appId = kintone.app.getId();
await addRecords({
app: appId,
records: new Array(10).fill('').map((record, i) => ({ FieldCode: { value: `${input}-${i}` } })),
});
return event;
});
})();
Using from outside kintone
The kintone REST API can be used from various environments such as Excel, GAS, and Node.js.
By using the REST API, you can operate applications using kintone data from environments other than kintone.
When obtaining kintone data from environments other than kintone, you need to include authentication information in the request.
There are several types of authentication information, but by choosing an API token, you can easily obtain authentication information while ensuring security.
The simplest method is to use an ID and password, but due to security risks, it is recommended to use an API token with the minimum necessary permissions.
Using Node.js
When using the kintone REST API from Node.js, you can send HTTP requests with almost the same code as executing JavaScript in the browser.
/**
* Add multiple records to an app using the kintone REST API
*
* @param { Object } params
* @param { string | number } params.app - App ID
* @param { Record<string, any>[] } params.records - Information of the records to be added
* @return { Promise<{ id: string[]; revision: "1"[] }> } - IDs and revision numbers of the added records
*/
const addRecords = (params) => {
const { app, records } = params;
const url = `https://__your_subdomain__.cybozu.com/k/v1/records.json`;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Cybozu-API-Token': 'YOUR_API_TOKEN',
},
body: JSON.stringify({ app, records }),
};
return fetch(url, options).then((response) => response.json());
};
Please change the __your_subdomain__
part and the YOUR_API_TOKEN
part according to your environment.
You can set them as arguments, but it is also fine to set them directly within the function.
Unlike the code used from kintone mentioned earlier, if the referenced app is in a guest space, you need to change the URL.
Using GAS
When using the kintone REST API from Google Apps Script (GAS), you can send HTTP requests using the UrlFetchApp
class.
/**
* Add multiple records to an app using the kintone REST API
*
* @param { Object } params
* @param { string | number } params.app - App ID
* @param { Record<string, any>[] } params.records - Information of the records to be added
* @return { Promise<{ id: string[]; revision: "1"[] }> } - IDs and revision numbers of the added records
*/
const addRecords = (params) => {
const { app, records } = params;
const url = `https://__your_subdomain__.cybozu.com/k/v1/records.json`;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Cybozu-API-Token': 'YOUR_API_TOKEN',
},
payload: JSON.stringify({ app, records }),
};
const response = UrlFetchApp.fetch(url, options);
return JSON.parse(response.getContentText());
};
Limitations
When adding multiple records in kintone, there are the following limitations:
- The maximum number of records that can be registered in one request is 100.
For methods to add records without being aware of this limitation, please refer to the following page.