Migrating Watson Assistant Nodejs code from V1 to V2

Code changes to shift from API V1 to API V2 of Watson Assistant

Many of my chatbots and voicebots built using Watson Assistant in the last few years used the Watson Assistant API V1. Digging up one of these oldies, I hit into some import and require conflicts in Nodejs. Googling around, I felt moving on to API V2 would be good.

ps: API V2 does not support modification of the workspace.

Here is a link to the official documentation to do a migration to API V2


However, here are the changes I had to make to my Nodejs code to get it going with API V2 of Watson Assistant.

Step 1: Open your Watson Assistant service on IBM Cloud and launch the service. This will take you to the landing page for Assistants. If you dont have an Assistant created for your skill then create one. Here are screenshots to help. Create the assistant and note Assistant Id from the settings to be used later.

Click on the Create Assistant Button to op up this screen. Add in name and description and hit the button.
Create Assistant Screen
Add a dialog skill to the Assistant
Add a dialog Skill to the new Assistant
Get the settings of the Assistant
Check the API Details on the settings screen for the Assistant ID
Use API Details link to get the Assistant ID

Step 2: Getting into the code — npm has deprecated the use of the module ‘ watson-developer-cloud‘, it has moved onto ‘ibm-watson’. So change package.json to reflect this change.

Step 3: You will need to pull in Watson Assistant V2 in your code. So add in

var AssistantV2 = require(‘ibm-watson/assistant/v2’);

Step 4: You will need IamAuthenticator so add in

const { IamAuthenticator } = require(‘ibm-watson/auth’);

Step 5: Instantiate the assistant object. Replace ApiKey,url and Assistant Id below with details from your respective Watson Assistant service

const assistant = new AssistantV2({
authenticator: new IamAuthenticator({ apikey: ‘<ApiKey>’ }),
url: ‘<url>',
version: ‘2019–02–28’
var assistant_id=’<Assistant Id>’;

Step 4: Next step is to initilize the payload. Here is a capture of how I did it.

Payload Initialisation

The ‘return_context:true’ is important if are trying to play around with context details.

‘user_defined’ is where the user defined context variables go in.

Step 5: All communication with an assistant takes place within the context of a session, which maintains conversation state throughout the duration of the conversation. State data, including any context variables that are defined by your dialog or client application, are automatically stored by Watson Assistant, without any action required on the part of your application. — As quoted on the website

So lets create a session and add in the Session Id to the payload.

Step 6: Make a call to the ‘message’ function as you would have done earlier

assistant.message(payload, function(err, data) {….

Step 7: Working with the result

You have to note that the json structure of the response is completely different in this version. Here is a snippet from the original documentation at https://cloud.ibm.com/docs/services/assistant?topic=assistant-api-migration&locale=dk

Changes to response format from the Watson Assistant website. Link is shared.
Changes to response format from the Watson Assistant website. Link is shared.
Changes to the response structure from the IBM website

This meant that I had to access the output text like this


And the custom context details like this

data.result.context.skills[‘main skill’].user_defined.mycustomcontext

If you are accessing intents and entities it would be



These changes got me going. I hope you find it helpful too.

Programmer by blood, heart and soul.