Get Multiple Records with Azure Functions

Contents

Overview

This article introduces how to retrieve multiple records from a Kintone App using Azure Functions.

What is Azure Functions?

Azure Functions is a feature of Microsoft Azure. It is a serverless computing service that can run programming codes in response to triggers, similar to AWS Lambda. From the browser, cron jobs can be set up for batch processing. Node.js and C# codes can run in response from HTTP triggers (Webhooks).

Examples of using Azure Functions

Run Kintone REST APIs periodically

Set up cron expressions to periodically run codes on Azure Functions. Kintone data can be accessed with Kintone REST APIs.

Integrate with Azure services using Webhooks

Set up Kintone's webhooks with Azure Functions to integrate with various other Azure services.

Prepare the Azure Functions environment

Create an account and set up Azure Functions

Prepare an Azure Functions environment with the following steps.

  1. Create a Microsoft Azure account (External link)
  2. Prepare Azure Functions (External link)
  3. Create Functions (External link)

Install the package modules

Follow the steps below to install the Node.js package.

Choose Advanced Tools -> Go->.

From the header, select Debug console -> CMD.

A console screen is displayed when Kudu is opened.
The package can be installed from this console.

From the directory links, navigate to the Function folder that was created.

1
D:\home\site\wwwroot\<function_name>

Run the following npm command. A number of questions will be asked. Enter the name and description, and then skip the rest by holding Enter.

1
D:\home\site\wwwroot\<function_name>> npm init

Install the following packages:

  • request
  • request-promise
1
2
D:\home\site\wwwroot\<function_name>> npm install request --save-dev
D:\home\site\wwwroot\<function_name>> npm install request-promise --save-dev

A node_modules folder and package.json file will be created after the installation is complete.

Open package.json and make sure that the settings are as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "name": "test",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "request": "^2.75.0",
    "request-promise": "^4.1.1"
  }
}

The Function Editor

This is the Function Editor where the code will be managed.

  1. The Endpoint URL. Azure Functions executes the code when an HTTP request is made to this URL.
  2. The code editor, including the code that will run.
  3. An area to display context logs when the code runs.
  4. An area to test HTTP requests.

Sample Code

The following is a sample that retrieves multiple record data from Kintone and displays it in the log. Type it into the code editor of Azure Functions. Replace the values of GET_URL, APP_ID and X-Cybozu-Authorization with the Kintone environment's URL, the target App ID, and the Password Authentication value respectively.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var rp = require('request-promise');

var GET_URL = 'https://{subdomain}.kintone.com/k/v1/records.json'; // Enter your subdomain
var APP_ID = 101; // Enter your App ID

module.exports = function(context, data) {

  var getRecords = function() {
    // Enter your authentication data
    var header_data = {
      'X-Cybozu-Authorization': 'xxxxxxxxxxxxxxxxx'
    };

    var req = {
      'method': 'GET',
      'url': GET_URL + '?app=' + APP_ID,
      'headers': header_data,
      'json': false,
    };
    return req;
  };

  rp(getRecords())
    .then(function(resp) {
      context.log(JSON.stringify(JSON.parse(resp), null, '  '));
      context.done();
    })
    .catch(function(error) {
      context.log('error:' + JSON.stringify(error, null, '  '));
      context.fail();
    }
    );
};

Save the code and click "Run".

If the code runs successfully, data of multiple records should be retrieved from Kintone and be displayed in the log. Status "200 OK" should also be displayed.

Reference

Azure Functions | Documentation (External link) Azure Functions | Kudu (External link)