In this MessageBird Developer Tutorial we'll show you how to keep your customers up to date with this easy-to-build runnable order notification application powered by the MessageBird SMS Messaging API. Have you ever ordered home delivery to find yourself wondering whether your order was received correctly and how long it will take to arrive? Some experiences are seamless and others... not so much.
For on-demand industries such as food delivery, ride-sharing and logistics, excellent customer service during the ordering process is essential. One easy way to stand out from the crowd is providing proactive communication to keep your customers in the loop about the status of their orders. Regardless of whether your customer is waiting for a package delivery or growing hangry (a combination of hungry and angry) awaiting their food delivery, sending timely SMS order notifications is a great strategy to create a seamless user experience.
The MessageBird SMS Messaging API provides an easy way to fully automate and integrate a notifications application into your order handling software. Busy employees can trigger the notifications application with the push of a single button. No more confused hangry customers and great user experience, just like that!
The application is a prototype order management system build in Node.js for our fictitious food delivery company, Birdie NomNom Foods.
Birdie NomNom Foods have set up the following workflow:
Thanks to this workflow, Birdie NomNom Foods saves time, money, and energy that would otherwise be spent answering "Where's my order?" calls. To run the application, you’ll need Node and npm, you can easily install them for free.
Pro-tip: Follow this tutorial to build the whole application from scratch or, if you want to see it in action right away, you can download, clone or fork the sample application from the MessageBird Developer Tutorials GitHub repository.
Let's open a console pointed to the directory into which you've placed the sample application and run the following command to install it:
npm install
This command will install the MessageBird SDK for Node.js and other dependencies from npm as defined in package.json.
Now, let's open index.js, the main file of the sample application. You'll find the following lines:
// Load and initialize MessageBird SDKvar messagebird = require('messagebird')('YOUR_API_KEY');
Next, we'll replace the string YOUR_API_KEY with a live access key. You can create or retrieve a key from the API access (REST) tab in the Developers section in the MessageBird Dashboard. It's also possible to use a test key to test the application. In this case, you can see the API output on the console, but no live SMS messages will be sent.
Pro-tip: Hardcoding your credentials in the code is a risky practice that should never be used in production applications. A better method, also recommended by the Twelve-Factor App Definition, is to use environment variables.
We've added dotenv to the sample application, so that you can supply your API key in a file named .env too:
MESSAGEBIRD_API_KEY=YOUR-API-KEY
The sample application triggers SMS delivery in the /updateOrder route together with updating the stored data.
Sending a message with the MessageBird SDK is straightforward—we simply call the messagebird.messages.create() method with a parameters object.
// Send the message through MessageBird APImessagebird.messages.create({originator : 'Birdy NomNom',recipients : [ order.phone ],body : body}, function (err, response) {if (err) {// Request has failedconsole.log(err);res.send("Error occured while sending message!");} else {// Request was successfulconsole.log(response);res.redirect('/');}});
You should set at least the following attributes in this object:
Make sure to replace the values in the sample code with adequate data for testing. There are additional optional attributes as well; you can find them in the SMS Messaging API documentation.
The MessageBird API call is asynchronous and executes a callback function once finished. This callback function takes two parameters, err and response. If err is defined, it indicates that something went wrong with the request. If everything went well, response contains… you guessed it, the response from the API.
You’re done! The sample application works on a set of test data defined in a variable called OrderDatabase in index.js. To test the full flow, replace one of the phone numbers with your own to receive the message on your phone:
// Set up Order "Database"var OrderDatabase = [{name : 'Hannah Hungry',phone : '+319876543210', // <- put your number here for testingitems : '1 x Hipster Burger + Fries',status : 'pending'},{name : 'Mike Madeater',phone : '+319876543211', // <- put your number here for testingitems : '1 x Chef Special Mozzarella Pizza',status : 'pending'}];
It's time to run the following command from your console:
node index.js
Open http://localhost:8080/ in your browser to see the list of orders.
Click on one of the buttons in the Action column to trigger a status change and, at the same time, automatically send a message.
Awesome! You can use the flow, code snippets, and UI examples from this tutorial as an inspiration to build your own SMS Notifications system. Don't forget to download the code from the MessageBird Developer Tutorial GitHub repository.
Nice work! 🎉
You now have a running SMS Notifications application with Node.js!
Want to build something similar but not quite sure how to get started? Feel free to let us know at support@messagebird.com; we'd love to help!