Back

Step by Step Guide to Building a Kintone API Integration in JS

Aug 16, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Kintone API integration? You're in for a treat. We'll be using the @kintone/rest-api-client package to make our lives easier. Trust me, it's a game-changer.

Prerequisites

Before we jump in, make sure you've got:

  • Node.js and npm (you're a pro, so I'm sure you do)
  • A Kintone account with an API token
  • Your JavaScript skills and async/await knowledge at the ready

Setting up the project

Let's get this party started:

mkdir kintone-api-project cd kintone-api-project npm init -y npm install @kintone/rest-api-client

Configuring the API client

Time to bring in the big guns:

const { KintoneRestAPIClient } = require('@kintone/rest-api-client'); const client = new KintoneRestAPIClient({ baseUrl: 'https://your-subdomain.kintone.com', auth: { apiToken: 'your-api-token' } });

Basic CRUD operations

Retrieving records

const getRecords = async () => { const app = 1; // Your app ID const records = await client.record.getRecords({ app }); console.log(records); };

Creating records

const createRecord = async () => { const app = 1; const record = { text_field: { value: 'Hello, Kintone!' } }; const result = await client.record.addRecord({ app, record }); console.log(result); };

Updating records

const updateRecord = async () => { const app = 1; const id = '1'; const record = { text_field: { value: 'Updated text' } }; const result = await client.record.updateRecord({ app, id, record }); console.log(result); };

Deleting records

const deleteRecord = async () => { const app = 1; const ids = ['1', '2', '3']; const result = await client.record.deleteRecords({ app, ids }); console.log(result); };

Advanced API usage

Querying with KQL

const queryRecords = async () => { const app = 1; const query = 'text_field = "Hello, Kintone!"'; const records = await client.record.getRecords({ app, query }); console.log(records); };

Bulk operations

const bulkInsert = async () => { const app = 1; const records = [ { text_field: { value: 'Bulk 1' } }, { text_field: { value: 'Bulk 2' } } ]; const result = await client.record.addRecords({ app, records }); console.log(result); };

File handling

const uploadFile = async () => { const filePath = './example.txt'; const fileKey = await client.file.uploadFile({ file: { path: filePath } }); console.log(fileKey); };

Error handling and best practices

Always wrap your API calls in try-catch blocks:

try { await someApiCall(); } catch (error) { console.error('Oops!', error); }

And remember, be nice to the API. Don't hammer it with requests!

Authentication options

We used API token auth earlier, but you can also use password auth:

const client = new KintoneRestAPIClient({ baseUrl: 'https://your-subdomain.kintone.com', auth: { username: 'your-username', password: 'your-password' } });

Testing and debugging

Console.log is your friend. Sprinkle it liberally throughout your code. And if you're feeling fancy, why not set up some Jest tests?

Conclusion

And there you have it! You're now a Kintone API integration wizard. Remember, practice makes perfect, so keep experimenting and building awesome stuff. The Kintone community is always here if you need a hand. Now go forth and code!

For more in-depth info, check out the official Kintone API docs. Happy coding!