Learn how to seamlessly integrate v0 with IBM Watson. Our guide provides clear steps, best practices, and practical tips for enhanced AI solutions.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
package.json file in your project root and add the following dependencies under "dependencies":
{
"dependencies": {
"ibm-watson": "^7.4.0",
"dotenv": "^16.0.0"
}
}
package.json file. These dependencies will be loaded when your project starts.
watsonService.ts. This file will contain the code necessary to communicate with IBM Watson.
watsonService.ts. This snippet loads environment variables, sets up the Watson Assistant service using your API key, service URL, and assistant ID, and provides functions to create a session and send a message.
import * as dotenv from 'dotenv';
dotenv.config();
import AssistantV2 from 'ibm-watson/assistant/v2';
import { IamAuthenticator } from 'ibm-watson/auth';
const assistant = new AssistantV2({
version: '2021-06-14',
authenticator: new IamAuthenticator({
apikey: process.env.IBMWATSONAPIKEY || 'YOURAPI_KEY'
}),
serviceUrl: process.env.IBMWATSONURL || 'YOURSERVICEURL'
});
export const createSession = async (): Promise<string> => {
const session = await assistant.createSession({
assistantId: process.env.IBMWATSONASSISTANTID || 'YOURASSISTANT_ID'
});
return session.result.session_id;
};
export const sendMessage = async (sessionId: string, message: string): Promise<any> => {
const response = await assistant.message({
assistantId: process.env.IBMWATSONASSISTANTID || 'YOURASSISTANT_ID',
sessionId,
input: {
message_type: 'text',
text: message
}
});
return response.result;
};
'YOURAPIKEY', 'YOURSERVICEURL', and 'YOURASSISTANTID' with your actual IBM Watson credentials. Alternatively, you can set these in an environment file.
.env in your project’s root folder.
.env file as shown below:
IBMWATSONAPIKEY=youractualapikey
IBMWATSONURL=yourserviceurl
IBMWATSONASSISTANTID=yourassistant_id
dotenv when your project starts.
index.ts) or the file where you want to invoke IBM Watson services.
watsonService.ts and add the integration logic. For example, add the following code snippet to create a session and send a message:
import { createSession, sendMessage } from './watsonService';
const startWatsonIntegration = async () => {
try {
const sessionId = await createSession();
const message = 'Hello, how can I assist you today?';
const response = await sendMessage(sessionId, message);
console.log('Watson Response:', response);
} catch (error) {
console.error('Error with Watson integration:', error);
}
};
startWatsonIntegration();
package.json correctly includes the added dependencies.
.env file exists and contains the correct IBM Watson credentials.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.