Skip to main content

Using external services in Demo Notification App

In this example, we will showcase how to use External services with an application that can send mail via SMTP.

info

You can try the application yourself by following the instructions at project source and deployed application. If you wish to follow along with the tutorial, we recommend cloning the application.

Why external services?

The issue with this application is that the connection information for the e-mail account is not stored in a variable, it's hardcoded in the application.yaml file.

The problem

Imagine we would like to deploy our application to a different environment and indicate sending notifications from that environment by using another account. Or perhaps to connect another application to the same account.

Wouldn't it be much easier to have some kind of object for each account, where the credentials could be accessed with {object.username} and {object.password}? And that's exactly what you can do with External services!

Steps

Create an external service

note

For a step-by-step tutorial on creating external services in CodeNOW, see the External services manual.

The process here consists of two main steps:

  1. Create a service template called mail-account that will hold variables HOSTNAME, PORT, USERNAME and PASSWORD.
  2. Create a service based on our template (we will call it public-gmail-account) and set its variables to the ones in the application.yaml file.

Connect the external service

Now, we need to:

  1. Create a new connection to a mail-account service. We will name the connection MAIL, therefore the variables can be accessed with ${MAIL_%VARIABLE-NAME%}.
  2. Update the application.yaml source code.
  3. Build and deploy the application to see if everything is set up correctly.
    • in the last step of the deployment process, choose the public-gmail-account for our MAIL connection

Connect another account

caution

If you're using Gmail, note that you cannot use the password for your account for third-party-apps. Instead, you need to create a new App password, which you can then pass to our PASSWORD variable.

  1. Choose an account that will send the notifications.
  2. Create a new service based on the mail-account template for that account and fill in the variables accordingly.
  3. Deploy the application to another environment, choose the newly created service in the last step.

Project source and deployed application

Project is deployed in CodeNOW and can be accessed with public demo account. Sign in here with the following credentials, if you want to see the application work in action for yourself.

  • username: public-user
  • password: Public-user123.

This example project can be cloned from here

How to test

You can test the app easily using Swagger:

  1. Open Swagger endpoint.
  2. Choose the sendMail method.
  3. Click on "Try it out".
  4. Fill in the mail body, subject and to (recipient).