Join us in San Francisco on the 29/30th of October for two days of developer workshops and technical talks

Create a Conversation

In this code snippet you will see how to create a Conversation.

Example

Ensure the following variables are set to your required values using any convenient method:

Key Description
CONV_NAME The unique name of the Conversation.
CONV_DISPLAY_NAME The display name of the Conversation.

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Create Conversation" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

Write the code

Add the following to create-conversation.sh:

Copy to Clipboard
curl -X "POST" "https://api.nexmo.com/beta/conversations" \
     -H 'Authorization: Bearer '$JWT\
     -H 'Content-Type: application/json' \
     -d $'{
  "name": "$CONV_NAME",
  "display_name": "$CONV_DISPLAY_NAME"
}'

View full source

Run your code

Save this file to your machine and run it:

bash create-conversation.sh

Prerequisites

Create an application

A Nexmo application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Nexmo concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Nexmo application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Nexmo needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Create Conversation" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

npm install nexmo@beta
Initialize your dependencies

Create a file named create-conversation.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
})

View full source

Write the code

Add the following to create-conversation.js:

Copy to Clipboard
nexmo.conversations.create({
    "name": CONV_NAME,
    "display_name": CONV_DISPLAY_NAME}, (error, result) => {
        if(error) {
            console.error(error);
        }
        else {
            console.log(result);
        }
    });

View full source

Run your code

Save this file to your machine and run it:

node create-conversation.js

Try it out

When you run the code you will create a new Conversation.