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.

  1. Navigate to the directory in which you'd like to store your project.
  2. Create a folder called sinch-appointment-reminder.
  3. Create the following directory structure:
    Copy
    Copied
    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.

  4. Make sure you're at the top level of sinch-appointment-reminder, open a terminal or command prompt, and run the following command:
    Copy
    Copied
    npm init
    This command creates the Node.js project and adds 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.

Dependencies

This application requires a number of node modules to function.

  1. Open your project's package.json file.
  2. Add the following:
    Copy
    Copied
    "dependencies": {
        "@sinch/sdk-core": "",
        "connect-flash": "",
        "dotenv": "",
        "ejs": "",
        "express": "",
        "express-session": "",
        "luxon": "",
        "sessionstorage-for-nodejs": ""
      },
      "devDependencies": {
        "nodemon": "^3.1.0"
          }
  3. Install the dependencies with the command:
    Copy
    Copied
    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 and connect-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
These values can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required.
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.

  1. Open the .env file.
  2. Paste the following text into the .env file:
    Copy
    Copied
    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)'
  3. Populate the fields in the file with your own values. Refer to the table below for guidance:
    FieldDescription
    KEY_IDYour access key ID, used for authentication. Refer to the Client information section for more information.
    KEY_SECRETYour access key secret, used for authentication. Refer to the Client information section for more information.
    PROJECT_IDYour project ID. Refer to the Client information section for more information.
    DEBUGSet to True to enable debugging.
    COUNTRY_CODE_EUIf 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_USIf you live in the US, this is the country code of your home country. For the US, it's +1.
    FROM_NUMBERAny 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_REGIONThe region in which you would like to send SMS messages. Either us or eu.

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.
Open app.js and add the following code:
Copy
Copied
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"));
});
Imagine that your application is like a company. The view engine that displays the web pages, the routing that controls the user journey, and the business logic that defines core functionality are like internal departments that work together to produce the right outcome. app.js defines the agenda that calls on each component to play their part.
We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.