Prerequisites
- Create an IBM Cloud account and understand the basics of IBM Watson services.
- Create an Airtable account and become familiar with the Airtable API.
- Install Node.js and npm on your machine.
- Familiarize yourself with basic JavaScript programming.
Set Up IBM Watson
- Log in to your IBM Cloud account.
- Navigate to the IBM Watson section and create a new Watson service instance, such as Watson Assistant or Watson Language Translator.
- Retrieve your service credentials: API Key and URL, from the IBM Cloud dashboard for your specific Watson service.
Set Up Airtable
- Log in to your Airtable account.
- Navigate to your Airtable base and create a new base or select an existing one to integrate with IBM Watson.
- Access the Airtable API section and retrieve your API Key.
Install Required Packages
- Initialize a new Node.js project by executing
npm init
in your terminal.
- Install the required packages:
npm install axios airtable ibm-watson
Write the Integration Script
- Create a new JavaScript file, e.g.,
integrate.js
.
- Load the necessary modules and configuration:
const Airtable = require('airtable');
const axios = require('axios');
const { IamAuthenticator } = require('ibm-watson/auth');
const { AssistantV2 } = require('ibm-watson/assistant/v2');
const airtableApiKey = 'YOUR_AIRTABLE_API_KEY';
const baseId = 'YOUR_BASE_ID';
const tableName = 'YOUR_TABLE_NAME';
const ibmApiKey = 'YOUR_IBM_API_KEY';
const serviceUrl = 'YOUR_IBM_SERVICE_URL';
const assistant = new AssistantV2({
version: '2021-06-14',
authenticator: new IamAuthenticator({
apikey: ibmApiKey,
}),
serviceUrl: serviceUrl,
});
- Connect to Airtable and retrieve data:
const base = new Airtable({ apiKey: airtableApiKey }).base(baseId);
base(tableName).select({
maxRecords: 5,
view: "Grid view"
}).eachPage((records, fetchNextPage) => {
records.forEach(record => {
console.log('Retrieved record:', record.fields);
});
fetchNextPage();
}, err => {
if (err) { console.error(err); return; }
});
- Implement IBM Watson API call:
const sessionId;
// First, create a session
assistant.createSession({
assistantId: 'YOUR_ASSISTANT_ID'
})
.then(res => {
sessionId = res.result.session_id;
return assistant.message({
assistantId: 'YOUR_ASSISTANT_ID',
sessionId: sessionId,
input: {
'message_type': 'text',
'text': 'Hello'
}
});
})
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.error(err);
});
Integrate IBM Watson and Airtable
- Combine the data retrieval from Airtable with the Watson API call to process the data:
base(tableName).select({
maxRecords: 5,
view: "Grid view"
}).eachPage(async (records, fetchNextPage) => {
for (const record of records) {
const text = record.fields['TextColumn']; // Replace with your column name
console.log('Processing record:', text);
try {
const messageResponse = await assistant.message({
assistantId: 'YOUR_ASSISTANT_ID',
sessionId: sessionId,
input: {
'message_type': 'text',
'text': text
}
});
console.log('Watson Response:', JSON.stringify(messageResponse.result, null, 2));
} catch (err) {
console.error(err);
}
}
fetchNextPage();
}, err => {
if (err) { console.error(err); return; }
});
Test and Deploy
- Run your script using
node integrate.js
to test the integration.
- Check both Airtable and the Watson service for expected data exchanges and outputs.
- Deploy your script on a server or automation platform if necessary for continuous operation.