Creating an appointment reminder app with Node SDK
Set up your Node.js application
First, we'll create the project folder and add a simple directory structure.
- Navigate to the directory in which you'd like to store your project.
- Create a folder called
sinch-appointment-reminder
. - Create the following directory structure:
sinch-appointment-reminder │ .env │ app.js │ routes.js │ ├───public │ └───css │ style.css │ └───views patient_details.html success.html
Note:
You'll learn what each of the files and folders does as you progress through the tutorial.
- Make sure you're at the top level of
sinch-appointment-reminder
, open a terminal or command prompt, and run the following command:This command creates the Node.js project and adds thenpm init
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.
Dependencies
This application requires a number of node modules to function.
- Open your project's
package.json
file. - Add the following:
"dependencies": { "@sinch/sdk-core": "", "connect-flash": "", "dotenv": "", "ejs": "", "express": "", "express-session": "", "luxon": "", "sessionstorage-for-nodejs": "" }, "devDependencies": { "nodemon": "^3.1.0" }
- Install the dependencies with the command:
npm install
Let's look at how these modules will help our application.
@sinch/sdk-core
is the Node SDK itself. It enables the app to send an SMS when an appointment is entered.dotenv
lets your app load configuration settings from environment variables so they don't have to be hardcoded.ejs
andconnect-flash
control the appearence of your application, as well as giving it the ability to display error messages.- The final dependency is called
nodemon
. This isn't essential to create the app, but will be very helpful for testing it.nodemon
lets you make changes to your app while it's running, just as you would with React.
Client information
When using the SDK, you must initialize the client.
Before initializing a client using this SDK, you'll need three pieces of information:
- Your Project ID
- An access key ID
- An access key Secret
Note:
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.
You'll add these values to the file described in the next section (along with others).
Adding the environment variables
Your app stores information, such as your Sinch credentials, as environment variables in the.env
file. This is much more flexible and secure than simply hardcoding them.- Open the
.env
file. - Paste the following text into the
.env
file:KEY_ID='YOUR_key_id' KEY_SECRET='YOUR_key_secret' PROJECT_ID='YOUR_project_id' DEBUG=True COUNTRY_CODE_EU='YOUR_EU_country_code' COUNTRY_CODE_US='+1' FROM_NUMBER='YOUR_sinch_number' SMS_REGION='YOUR_sms_region(us/eu)'
- Populate the fields in the file with your own values. Refer to the table below for guidance:
Field Description KEY_ID
Your access key ID, used for authentication. Refer to the Client information section for more information. KEY_SECRET
Your access key secret, used for authentication. Refer to the Client information section for more information. PROJECT_ID
Your project ID. Refer to the Client information section for more information. DEBUG
Set to True
to enable debugging.COUNTRY_CODE_EU
If you live in the EU or UK, this is the country code of your home country. For example the UK country code is +44
.COUNTRY_CODE_US
If you live in the US, this is the country code of your home country. For the US, it's +1
.FROM_NUMBER
Any number you've assigned to your Sinch account. Find the number on your Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page. SMS_REGION
The region in which you would like to send SMS messages. Either us
oreu
.
Setting up your application entry point
app.js
defines the entry point for your application. This file is a keystone of all Node.js applications, playing a couple of important roles:- It acts like a starter motor, starting up the server that runs your application on a given port.
- It tells your app which components it needs to use.
app.js
and add the following code:const express = require("express");
const app = express();
const session = require("express-session");
const flash = require("connect-flash");
const routes = require("./routes");
app.use(express.static(__dirname + "/public"));
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: "secret key",
resave: false,
saveUninitialized: false,
})
);
app.use(flash());
app.use(routes);
app.set("port", process.env.PORT || 3000);
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
app.listen(app.get("port"), function () {
console.log("server started on port " + app.get("port"));
});
app.js
defines the agenda that calls on each component to play their part.