AWS Lambda(node.js)からkintone REST API(getRecords)を実行する方法
getRecordsメソッドでkintoneアプリに登録されている全レコードを取得します。
@kintone/rest-api-clientモジュールを使用して実現します。
npm init -y npm i @kintone/rest-api-client touch index.js
index.js
LambdaはESMで記述しています。必須パラメータはappのみです。
import { KintoneRestAPIClient } from '@kintone/rest-api-client'
export async function handler(event, context) {
const client = new KintoneRestAPIClient({
baseUrl: 'https://xxxxxxxxxx.cybozu.com',
auth: {
username: 'ログインID',
password: 'パスワード'
}
})
const results = await client.record.getRecords(
{
app: '4', // required
}
)
return {
statusCode: 200,
body: `Hello`
}
}
zip圧縮
簡単に試すために7zでzip圧縮し、Lambdaデプロイします。
テスト
顧客アプリです。
「Test」を押します。
顧客アプリのレコードが全件出力されていることが確認できます。
query(like)
queryプロパティを使えば条件を絞ることが可能です。
会社名でlike検索する例です。
import { KintoneRestAPIClient } from '@kintone/rest-api-client'
export async function handler(event, context) {
const client = new KintoneRestAPIClient({
baseUrl: 'https://xxxxxxxxxx.cybozu.com',
auth: {
username: 'ログインID',
password: 'パスワード'
}
})
const results = await client.record.getRecords(
{
app: '4',
query: 'company like "大阪"'
}
)
console.log(results.records)
return {
statusCode: 200,
body: JSON.stringify()
}
}
結果です。
query(=)
会社名で=検索する例です。
import { KintoneRestAPIClient } from '@kintone/rest-api-client'
export async function handler(event, context) {
const client = new KintoneRestAPIClient({
baseUrl: 'https://xxxxxxxxxx.cybozu.com',
auth: {
username: 'ログインID',
password: 'パスワード'
}
})
const results = await client.record.getRecords(
{
app: '4',
query: 'company = "大阪"'
}
)
console.log(results.records)
return {
statusCode: 200,
body: JSON.stringify()
}
}
0件の場合は、[]が返ります。
最大件数
一度に取得できる件数は500件です。
Retrieves details of multiple records from an app by specifying the app ID and a query string. The number of records that can be retrieved at once is 500.
500件以上取得したい場合はgetAllRecordsを使用します。
参考サイト
js-sdk/packages/rest-api-client/docs/record.md at main ?? kintone/js-sdk
A monorepo for Kintone development tools :wrench:. Contribute to kintone/js-sdk development by creating an account on Gi...

Get Records
Retrieves details of multiple records from an App by specifying the App ID and a query string.

Query string
Filters multi records from an App by query string.

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^






コメント