Send a message

Note:

Before you can get started, you need to do the following:

Using the Conversation API, you can send messages to any channel you have configured. This tutorial shows you how to set up and send a message in a Node.js application.

Steps:
  1. Set up your Node.js application
  2. Send your first message

Set up your Node.js application

  1. Create a new node app with npm.
Copy
Copied
npm init
  1. Accept the defaults for the application.
  2. Add the fetch package with npm to generate the necessary dependencies.
Copy
Copied
npm install node-fetch
Note:

The node fetch package requires us to use node modules, so we need to use a .mjs file type instead of a .js.

Create your file

Note:

This tutorial uses basic authentication for testing purposes. We recommend OAuth 2.0 authentication in a production environment. Read more about authentication methods here.

  1. Create a new file named index.mjs in the project and paste the provided "index.mjs" code found on this page into the file. This code sends a text message.
Note:

This sample code is configured for the US region. If your Conversation API app wasn't created in the US region, replace all instances of https://us.conversation.api.sinch.com with https://eu.conversation.api.sinch.com in the sample code.

index.mjs

This code sample is to be used with the instructions on this page to build an application that will send a message.

// Find your App ID at dashboard.sinch.com/convapi/apps
// Find your Project ID at dashboard.sinch.com/settings/project-management
// Get your Access Key and Access Secret at dashboard.sinch.com/settings/access-keys
const APP_ID = '';
const ACCESS_KEY = '';
const ACCESS_SECRET = '';
const PROJECT_ID = '';
const CHANNEL = '';
const IDENTITY = '';

import fetch from 'node-fetch';

async function run() {
  const resp = await fetch(
    'https://us.conversation.api.sinch.com/v1/projects/' + PROJECT_ID + '/messages:send',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: 'Basic ' + Buffer.from(ACCESS_KEY + ':' + ACCESS_SECRET).toString('base64')
      },
      body: JSON.stringify({
        app_id: APP_ID,
        recipient: {
          identified_by: {
            channel_identities: [
              {
                channel: CHANNEL,
                identity: IDENTITY
              }
            ]
          }
        },
        message: {
          text_message: {
            text: 'Text message from Sinch Conversation API.'
          }
        }
      })
    }
  );

  const data = await resp.json();
  console.log(data);
}

run();
  1. Assign your values to the following parameters:
ParameterYour value
APP_IDFind your app ID on your Sinch dashboard.
ACCESS_KEYFind your access key on your Sinch dashboard.
ACCESS_SECRETFind your access secret on your Sinch dashboard.
Note: Access secrets are only available during initial key creation.
PROJECT_IDFind your project ID on your Sinch dashboard.
CHANNELThe channel you want to use to send the message. Available channels are configured for the app on your Sinch dashboard. This guide assumes you've started with an SMS channel, but you can use any channel configured for your app:
  • SMS
  • MESSENGER
  • MMS
  • RCS
  • WHATSAPP
  • VIBER
  • VIBERBM
  • INSTAGRAM
  • TELEGRAM
  • KAKAOTALK
  • APPLEBC
  • LINE
  • WECHAT
IDENTITYThe ID of the contact to which you want to send the message.
  1. Save the file.

Send your first message

Now you can execute the code and send your test message. Run the following command:

Copy
Copied
node index.mjs

You should receive a message in your configured messaging platform.

Next steps

Now that you know how to send a message, next learn how to handle an incoming message.

Additional resources

Read the links below to learn more:

Was this page helpful?

index.mjs

This code sample is to be used with the instructions on this page to build an application that will send a message.

// Find your App ID at dashboard.sinch.com/convapi/apps
// Find your Project ID at dashboard.sinch.com/settings/project-management
// Get your Access Key and Access Secret at dashboard.sinch.com/settings/access-keys
const APP_ID = '';
const ACCESS_KEY = '';
const ACCESS_SECRET = '';
const PROJECT_ID = '';
const CHANNEL = '';
const IDENTITY = '';

import fetch from 'node-fetch';

async function run() {
  const resp = await fetch(
    'https://us.conversation.api.sinch.com/v1/projects/' + PROJECT_ID + '/messages:send',
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: 'Basic ' + Buffer.from(ACCESS_KEY + ':' + ACCESS_SECRET).toString('base64')
      },
      body: JSON.stringify({
        app_id: APP_ID,
        recipient: {
          identified_by: {
            channel_identities: [
              {
                channel: CHANNEL,
                identity: IDENTITY
              }
            ]
          }
        },
        message: {
          text_message: {
            text: 'Text message from Sinch Conversation API.'
          }
        }
      })
    }
  );

  const data = await resp.json();
  console.log(data);
}

run();