LoginSignup
0
0

Node.js環境からAxiosでkintoneにリクエストする方法について

Last updated at Posted at 2023-12-21

あまり載ってないので備忘録として残しておきます。

環境

macOS 10.15.7
Node.js v17.3.0

記載の環境はmacですが、Windowsでも同じです。

準備

下記を準備しておきます。

Node.jsのインストール
作業用のフォルダを作成 -> mkdir debug
作成したフォルダに移動 -> cd debug
コマンドラインからnpm initする -> npm init
dotenvモジュールをインストール -> npm i dotenv --save
axiosモジュールをインストール -> npm i axios
.envファイルを作成する -> touch .env

Node.js
dotenv
axios

kintoneのアプリの作成とAPIトークンの生成等も事前に行います。
また、.envファイルに環境変数としてアプリIDやAPIトークンも設定しておきます。

record.json

ファイル作成
touch fetch01.js

axios

axiosを利用したリクエストは下記の感じ。

const axios = require('axios');
require('dotenv').config({debug: true});

(async () => {
    try {
        const API_TOKEN = process.env.API_TOKEN;
        const APP_ID = process.env.APP_ID;
        const config = {
            headers: {
                'X-Cybozu-API-Token': API_TOKEN,
            },
            params: {
                'app': APP_ID,
                'id': 1,
            }
        }
        await axios.get(`https://${process.env.KINTONE_SUBDOMAIN}.cybozu.com/k/v1/record.json`, config)
        .then(response => {
            console.log(response);
        })
        .catch(e => {
            throw e;
        })
    } catch (error) {
        console.log(error);
    }
})();

kintone rest-api-client

kintone rest-api-clientを利用すると下記の感じ。

const { KintoneRestAPIClient } = require("@kintone/rest-api-client");
require('dotenv').config({debug: true});

(async () => {
    try {
        const API_TOKEN = process.env.API_TOKEN;
        const APP_ID = process.env.APP_ID;
        const params = {
            'app': APP_ID,
            'id': 1,
        }
        const client = new KintoneRestAPIClient({
            baseUrl: `https://${process.env.KINTONE_SUBDOMAIN}.cybozu.com`,
            // Use API token authentication
            auth: { apiToken: API_TOKEN }
        });
        await client.record.getRecord(params)
        .then(response => {
            console.log(response);
        })
        .catch(e => {
            throw e;
        })
    } catch (error) {
        console.log(error);
    }
})();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0