Most Popular
Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.
Sign-up
Follow along with the video tutorial:
We are launching our first hackathon next week, and giving away over $1K in prizes! Join us in building a cool project and winning any of the following prizes đ
Additionally, everyone who submits a project successfully integrating the Courier API will receive a $20 Amazon gift card!
Not sure where to start? In this tutorial, we will be building a Node.js app that sends multi-channel notifications in morse code.
We are secret agents today, and our goal is to send encoded messages to our spy network. Some spies prefer reading emails, and others prefer reading texts, so we need to ensure that our app can accommodate all spy preferences.
Note: The first five secret agents to successfully complete this tutorial and this task will receive a gift from Courier.
In Chapter 1, we will first integrate the Gmail and Twilio APIs, which Courier will use to send emails and text messages. In Chapter 2, we will demonstrate how to send single messages and setup routing to send multi-channel notifications. In Chapter 3, we will integrate a translation API to convert our messages into Morse code.
We are hosting our first hackathon next month, starting September 5th until September 30th. Register now to submit this project for a chance to win some cool prizes.
Register for the Hackathon: https://courier-hacks.devpost.com/
In this first Chapter, we will need to authorize our API to send the secret messages. Letâs get started by integrating the Gmail and Twilio APIs, enabling Courier to send emails and messages from a single API call.
Once you can see the dancing pigeon, you are ready to use Courier to send more notifications. Before we build out our application, we need to set up the Twilio provider to enable text messages.
Lasty, you need to locate the Messaging Service SID, which you create in the Messaging tab on the left menu. Checkout Twilioâs docs on how to create a Messaging Service SID, linked in the description.
In this next Chapter, you will start sending messages. To send the secret messages, head to the Send API documentation. Here you can find everything related to sending messages.
On the right, you will see some starter code and can select a language of your choice from cURL, Node.js, Ruby, Python, Go, or PHP.
1// Dependencies to install:2// $ npm install node-fetch --save34const fetch = require('node-fetch');56const options = {7method: 'POST',8headers: {9Accept: 'application/json',10'Content-Type': 'application/json'11},12body: JSON.stringify({13"message": {14"template": "NOTIFICATION_TEMPLATE"15}16})17};1819fetch('https://api.courier.com/send', options)20.then(response => response.json())21.then(response => console.log(response))22.catch(err => console.error(err));
This ]basic POST request can be edited to include the spiesâ data such as how to contact them and the message you need to send. The âNotification Templateâ can be replaced with your own template.
1// Dependencies to install:2// $ npm install node-fetch --save34const fetch = require('node-fetch');56const options = {7method: 'POST',8headers: {9Accept: 'application/json',10'Content-Type': 'application/json'11},12body: JSON.stringify({13"message": {14"template": "NOTIFICATION_TEMPLATE",15"to": {16"email": "courier.demos+secretmessage@gmail.com"17}18}19})20};2122fetch('https://api.courier.com/send', options)23.then(response => response.json())24.then(response => console.log(response))25.catch(err => console.error(err));
Next, you need to add the actual message you are sending. These messages are pretty simple, so you can write them directly into the API call instead of creating a template.
1// Dependencies to install:2// $ npm install node-fetch --save34const fetch = require('node-fetch');56const options = {7method: 'POST',8headers: {9Accept: 'application/json',10'Content-Type': 'application/json'11},12body: JSON.stringify({13"message": {14"to": {15"email": "courier.demos+secretmessage@gmail.com"16},17"content": {18"title": "new subject",19"body": "message"20}21}22})23};2425fetch('https://api.courier.com/send', options)26.then(response => response.json())27.then(response => console.log(response))28.catch(err => console.error(err));
As before, the data on the left automatically appears in the code snippet on the right. There is a content object that encompasses the title and body parameters.
Now you just need to make sure that this API call has access to your Courier account, which links to the Gmail and Twilio APIs.
1// Dependencies to install:2// $ npm install node-fetch --save34const fetch = require('node-fetch');56const options = {7method: 'POST',8headers: {9Accept: 'application/json',10'Content-Type': 'application/json',11Authorization: 'Bearer apikey'12},13body: JSON.stringify({14"message": {15"to": {16"email": "courier.demos+secretmessage@gmail.com"17},18"content": {19"title": "new subject",20"body": "message"21}22}23})24};2526fetch('https://api.courier.com/send', options)27.then(response => response.json())28.then(response => console.log(response))29.catch(err => console.error(err));
Now you can integrate this code into our own Node.js application.
index.js
.index.js
file.1$ npm install node-fetch --save
1$ node index.js
1npm install node-fetch@2
Now when you run this program, you should get a response from Courier that includes the requestID
in the VS Code console. This indicates that the API call was made successfully, and you can head over to the Courier datalog to determine if the message was also sent successfully.
Since you are a Secret Agent, you should probably protect the API key in case our code gets into the wrong hands.
.env
.1APIKEY="fksdjfgjsdkfgndfsmn"
index.js
file.process.env.APIKEY
.require('dotenv').config()
to the top of the index.js
file.At this point, you can send a single message to the spies via email. However, you know that some spies prefer to use text messages, so you will need to enable multi-channel notifications. Letâs head back to the Courier docs and scroll down to the routing
object, which contains the method
and channels
. Two types of methods are available - all
and single
. âAllâ means that Courier will attempt to send the message to every channel listed. âSingleâ means that Courier will attempt to send it to the first channel that works. Letâs integrate this into our program.
routing
object anywhere within the message
object, at the same level as to
and content
.routing
object - you can choose SMS or email, in this case, since you already have an email address defined.1"message": {2"to": {3"email": process.env.EMAIL4},5"content": {6"title": "new subject",7"body": "message"8},9"routing": {10"method": "single",11"channels": "email"12},13}
channels
property into an array to define multiple channels and list both email and SMS.1"channels": ["email", "sms"]
You now have two different channels that this message can be sent to. All
methods would send this message to both email and SMS. Single
method would try to send this to the first that works. Since you have the userâs email address but not their phone number, this program can only send it via email.
If the two channels were reversed, Courier would try to send an SMS, fail to do so, and then default to sending an email.
1"channels": ["sms", "email"]
1"message": {2"to": {3"email": process.env.EMAIL,4"phone_number": process.env.PHONENUMBER5},6"content": {7"title": "new subject",8"body": "message"9},10"routing": {11"method": "single",12"channels": ["sms", "email"]13},14}
all
and run the program again.1"message": {2"to": {3"email": process.env.EMAIL,4"phone_number": process.env.PHONENUMBER5},6"content": {7"title": "new subject",8"body": "message"9},10"routing": {11"method": "all",12"channels": ["sms", "email"]13},14}
Courier can now send via Twilio and Gmail within the same API call.
NOTE: The Morse API has a rate limit, which may give you an error if you run it too many times within the hour. In this case, you will have to wait for some time before continuing.
In this last Chapter, you will integrate the Fun Translations Morse API to encode the secret messages and send them over to the spies. You can search for documentation on the Morse API on the Fun Translations website.. Here you have access to all the information you need to make the call - you have an endpoint and an example demonstrating that the original message is a parameter for the endpoint.
đ Fun Translations: https://funtranslations.com/api/#morse
đ Fun Translations API: https://api.funtranslations.com/
options
to courier_options
.1// Dependencies to install:2// $ npm install node-fetch --save34const fetch = require('node-fetch');5require('dotenv').config()67async function send_secret_message() {89const courier_options = {10method: 'POST',11headers: {12Accept: 'application/json',13'Content-Type': 'application/json',14Authorization: 'Bearer ' + process.env.APIKEY15},16body: JSON.stringify({17"message": {18"to": {19"email": process.env.EMAIL,20"phone_number": process.env.PHONENUMBER21},22"content": {23"title": "new subject",24"body": "message"25},26"routing": {27"method": "all",28"channels": ["sms", "email"]29},30}31})32};3334fetch('https://api.courier.com/send', courier_options)35.then(response => response.json())36.then(response => console.log(response))37.catch(err => console.error(err));3839}4041send_secret_message()
Before sending the message, you first need to make a call to the Morse API to translate the message. You can use node-fetch in the same way as you did for Courier to make this call.
options
to morse_options
for the first call.body
object.1// Dependencies to install:2// $ npm install node-fetch --save34const fetch = require('node-fetch');5require('dotenv').config()67async function send_secret_message() {89const morse_options = {10method: 'GET',11headers: {12Accept: 'application/json',13'Content-Type': 'application/json'14}15};1617const original_message = "hey%20secret%20agent%20x%20this%20is%20your%20message"18const morse_endpoint = "https://api.funtranslations.com/translate/morse.json?text="+original_message1920fetch(morse_endpoint, morse_options)21.then(response => response.json())22.then(response => console.log(response))23.catch(err => console.error(err));2425const courier_options = {26method: 'POST',27headers: {28Accept: 'application/json',29'Content-Type': 'application/json',30Authorization: 'Bearer ' + process.env.APIKEY31},32body: JSON.stringify({33"message": {34"to": {35"email": process.env.EMAIL,36"phone_number": process.env.PHONENUMBER37},38"content": {39"title": "new subject",40"body": "message"41},42"routing": {43"method": "all",44"channels": ["sms", "email"]45},46}47})48};4950fetch('https://api.courier.com/send', courier_options)51.then(response => response.json())52.then(response => console.log(response))53.catch(err => console.error(err));5455}5657send_secret_message()
When you run this program, we may receive an error that states that there is an error parsing the JSON. This issue is caused by an error in the documentation, which here states that it should be a POST
request. However, on a separate API documentation, it is written as a GET
request. Update the call type to GET,
and you should see the translated message within the response.
ObviouslyClearly, you donât want to send all of this information to the spies, y. You only need the secret message.
response.contents.translated
.1fetch(morse_endpoint, morse_options)2.then(response => response.json())3.then(response => console.log(response.contents.translated))4.catch(err => console.error(err));
You need to be able to access the translation from this API call in the body of the Courier API call.
morse_response
, which will hold the entire response from this call.message
.1const morse_response = await fetch(morse_endpoint, morse_options)2// .then(response => response.json())3// .then(response => console.log(response.contents.translated))4// .catch(err => console.error(err));5const translation = await morse_response.json();6const message = translation.contents.translated7console.log(message)
message
variable.1"message": {2"to": {3"email": process.env.EMAIL,4"phone_number": process.env.PHONENUMBER5},6"content": {7"title": "new secret message",8"body": message9},10"routing": {11"method": "all",12"channels": ["sms", "email"]13},14}
The Courier datalog should show that the messages were successfully encoded and sent via both SMS and email. Hereâs what the email looks like:
Our spies are now ready to receive their secret encoded messages. Try changing the body of the content to your own secret message and send it over to courier.demos+secretmessage@gmail.com
,and we will send a gift to the first five Secret Agents who complete this task! Donât forget to submit your project to our Hackathon for a chance to win over $1000 in cash and prizes!XYZ.
đ GitHub Repository: https://github.com/shreythecray/secret-messages
đ Video tutorial: https://youtu.be/6W2rIyUdmas
đ Courier: app.courier.com
đ Register for the Hackathon: https://courier-hacks.devpost.com/
đ Courier's Get Started with Node.js: https://www.courier.com/docs/guides/getting-started/nodejs/
đ Courier Send API Docs: https://www.courier.com/docs/reference/send/message/
đ Twilio Messaging Service SID Docs: https://support.twilio.com/hc/en-us/articles/223181308-Getting-started-with-Messaging-Services
đ Node-fetch: https://www.npmjs.com/package/node-fetch
đ Dotenv: https://www.npmjs.com/package/dotenv
đ Fun Translations: https://funtranslations.com/api/#morse
đ Fun Translations API: https://api.funtranslations.com/
Courier is a notification service that centralizes all of your templates and messaging channels in one place which increases visibility and reduces engineering time.
Sign-up
How to Set Up Automatic Push Notifications Based on Segment Events
Push notifications have carved their own niche as a powerful tool for continuous user engagement. Regardless of whether an app is actively in use, they deliver your messages straight to your user's device. Two key players that can combine to enhance your push notification strategy are Segment and Courier. In this tutorial, we show you how to set up Courier to listen to your Segment events and then send push notifications to an Android device based on data from these events.
Sarah Barber
November 17, 2023
How to Send Firebase Notifications to iOS Devices Using Courier
This tutorial explains how to send push notifications to iOS devices from your iOS application code using Firebase FCM and Courierâs iOS SDK.
Martina Caccamo
November 01, 2023
Free Tools
Comparison Guides
Send up to 10,000 notifications every month, for free.
Get started for free
Send up to 10,000 notifications every month, for free.
Get started for free
Š 2024 Courier. All rights reserved.