Easily integrate Bolt.new AI with IBM Watson using our quick guide. Learn expert tips, proven steps, and best practices for seamless AI integration.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Create or update your package.json
file in your Bolt.new AI project to include the IBM Watson SDK dependency. Since Bolt.new AI does not offer a terminal, you add the dependency by editing your project file.
{
"name": "my-bolt-ai-project",
"version": "1.0.0",
"dependencies": {
"ibm-watson": "^7.4.0"
// You can add other dependencies as needed
}
}
package.json
file.
Add a new TypeScript file to your project named ibmWatsonService.ts
in the source folder. This file will handle the initialization of the IBM Watson Assistant API client.
import AssistantV2 from 'ibm-watson/assistant/v2';
import { IamAuthenticator } from 'ibm-watson/auth';
// Initialize the Watson Assistant client
const assistant = new AssistantV2({
version: '2021-06-14', // You can adjust the version if necessary
authenticator: new IamAuthenticator({
apikey: process.env.IBMWATSONAPIKEY
}),
serviceUrl: process.env.IBMWATSONURL // e.g., 'https://api.us-south.assistant.watson.cloud.ibm.com'
});
export default assistant;
IBMWATSONAPIKEY
and IBMWATSONURL
via the Bolt.new secrets or configuration settings.
In the same file or a new file (for example, watsonUtils.ts
), create a function to establish a session with IBM Watson Assistant. This session is required to send messages.
import assistant from './ibmWatsonService';
export async function createWatsonSession(): Promise<string> {
try {
const session = await assistant.createSession({
assistantId: process.env.IBMWATSONASSISTANT_ID // Your Watson Assistant ID
});
return session.result.session_id;
} catch (error) {
console.error('Error creating Watson session:', error);
throw error;
}
}
IBMWATSONASSISTANT_ID
environment variable in your project settings.
Create a new function in a separate file or append it to your watsonUtils.ts
file. This function will send a user message to IBM Watson Assistant and receive a response.
import assistant from './ibmWatsonService';
export async function sendMessageToWatson(sessionId: string, message: string): Promise<any> {
try {
const response = await assistant.message({
assistantId: process.env.IBMWATSONASSISTANT_ID,
sessionId: sessionId,
input: {
message_type: 'text',
text: message
}
});
return response.result;
} catch (error) {
console.error('Error sending message to Watson Assistant:', error);
throw error;
}
}
Locate the file where you need to integrate IBM Watson with your Bolt.new AI logic (for example, app.ts
or another controller file). Import the utility functions, create a session, and then send messages as needed.
import { createWatsonSession, sendMessageToWatson } from './watsonUtils';
// Example function that handles user input and queries Watson Assistant
async function processUserInput(userMessage: string) {
try {
// Create a session with IBM Watson Assistant
const sessionId = await createWatsonSession();
// Send the user message to Watson and await response
const watsonResponse = await sendMessageToWatson(sessionId, userMessage);
console.log('Watson Reply:', watsonResponse);
// Handle or forward the Watson response as needed in your application
} catch (error) {
console.error('Error during Watson message processing:', error);
}
}
// Example usage
processUserInput('Hello, Watson!');
Since Bolt.new AI does not offer a terminal, you need to add your IBM Watson credentials using the built-in secrets or environment variables feature. In your project settings, add:
// IBMWATSONAPIKEY: Your IBM Watson API key.
// IBMWATSONURL: The IBM Watson service URL.
// IBMWATSONASSISTANT_ID: Your Watson Assistant ID.
process.env
variables used in your code are correctly populated.
package.json
to add the IBM Watson SDK dependency.ibmWatsonService.ts
that initializes the Watson client.watsonUtils.ts
to create sessions and send messages.
By following these steps, you can successfully integrate IBM Watson services into your Bolt.new AI project using TypeScript.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.