Send a message
Note:
Before you can get started, you need to do the following:
- Set all Conversation API configuration settings.
- Node.js and a familiarity with how to create a new app.
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.
Set up your Node.js application
First we'll create a Node project using npm. This creates a package.json and the core dependencies necessary to start coding.
To create the project, do the following steps:
- Create a folder called
conv-send
- Navigate into the folder you created and run the following command.
npm init
This command adds the node_modules folder and the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage.
- Add the fetch package with npm to generate the necessary dependencies.
npm install node-fetch
Note:
.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.
- 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:
https://us.conversation.api.sinch.com
with https://eu.conversation.api.sinch.com
in the sample code.- Assign your values to the following parameters:
Parameter | Your value |
---|---|
APP_ID | Find your app ID on your Sinch dashboard. |
ACCESS_KEY | Find your access key on your Sinch dashboard. |
ACCESS_SECRET | Find your access secret on your Sinch dashboard. Note: Access secrets are only available during initial key creation. |
PROJECT_ID | Find your project ID on your Sinch dashboard. |
CHANNEL | The 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:
|
IDENTITY | The ID of the contact to which you want to send the message. |
- Save the file.
Send your first message
Now you can execute the code and send your test message. Run the following command:
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: