Build an email service with Liteflow

Today we will learn how to create and use a Liteflow service that sends emails using SendGrid.

# Create the Liteflow Service

It's time to create our Liteflow application with its first service. Open a terminal and run the following commands:

npm i -g @liteflow/cli
liteflow init service-email-sendgrid-tuto
cd service-email-sendgrid-tuto
liteflow service:init services/email --template javascript

You will now have a new directory services/email with the boilerplate of a service.

# Send emails through a task

Let's define the task we want to create in the liteflow.yml file (in your services/email directory).

# Configure your service

This service will use Sendgrid to send emails and require the Sendgrid library and a Sendgrid API key. You can add the variable SENDGRID_API_KEY as env of your service.

The services also define a task called send responsible for sending an email. This task has the following inputs/outputs:

# Inputs:

  • from (String): the account to send the email from
  • to (String): the recipient of the email
  • subject (String): the subject of the email
  • text (String): the text of the email

# Outputs:

  • status (Number): status of the sent email

With the configuration and the send task, your liteflow.yml should look like:

name: sendgrid-service
configuration:
  env:
    - SENDGRID_API_KEY=
tasks:
  send:
    inputs:
      from: { type: String }
      to: { type: String }
      subject: { type: String }
      text: { type: String }
    outputs:
      status: { type: Number }

# Write the code of the task

The next step is to write the code for the send task of your service.

To do that, we will need the @sendgrid/mail library.

npm install --save @sendgrid/mail

Then require and initialize it in our js file:

const sendgrid = require('@sendgrid/mail')
// Environment from the service definition in the `liteflow.yml`
sendgrid.setApiKey(process.env.SENDGRID_API_KEY)

Let's now write the content of your send task.

liteflow.listenTask({
  send: async (inputs) => {
    // Sends an email with the inputs
    const result = await sendgrid.send({
      from: inputs.from,
      to: inputs.to,
      subject: inputs.subject,
      text: inputs.text
    })
    // Return the success output
    return {
      status: result[0].statusCode
    }
  }
})

This code sends an email with the inputs defined in the liteflow.yml and returns the status code.

# Testing

# Using this service in a process

To use this service, we will create a new process. Go back to the root directory of your application service-email-sendgrid-tuto and create a new process file named process.yml with the following content.

name: email-from-http
steps:
  - type: trigger
    endpointKey: send-email
  - type: task
    instance:
      src: ./services/email
      env:
        - SENDGRID_API_KEY=$(env:SENDGRID_API_KEY)
    taskKey: send
    inputs:
      from: test@email.com
      to: { key: body.to }
      subject: { key: body.subject }
      text: { key: body.text }

This process triggers the task send of your service every time an endpoint is hit. It also configures the service with the right environment. More details on processes on the documentation.

# Get a sendgrid API key

You now need to get your own private SendGrid API key. You can create it here: https://app.sendgrid.com/settings/api_keys.

Once your API key is ready, add it in your .env file with the following SENDGRID_API_KEY=__YOUR_SENDGRID_API_KEY__.

# Send emails

Everything is ready.

Let's run the command:

liteflow deploy

You can also run it locally using liteflow dev. Note that this command requires docker.

Now you should see an URL like http://gateway.liteflow.com/G1qw789ChAcfEXLaKfQYvgKkhT3VVBFG2Jt7HKLSUk8Q/send-email and be able to hit this endpoint to trigger your email.

curl -XPOST http://gateway.liteflow.com/G1qw789ChAcfEXLaKfQYvgKkhT3VVBFG2Jt7HKLSUk8Q/send-email \
  -d "to=your_email" \
  -d "subject=Subject of your email" \
  -d "text=Text for your email"

Congratulations! 🎉 Get some rest now. You've done an excellent job creating your first service with Liteflow. Of course, you can always create more complicated services. Make sure to check out the documentation for more details.

New to Liteflow? Code and launch your applications quickly with a backend framework and integrated cloud solution. Visualize and manage applications anytime with the console. Sign up for free and deploy your idea in seconds!



play icon developers icon

Liteflow is built for developers

Discover Liteflow

Deploy your idea in seconds

While in beta, unlimited hosting is free for all!

Sign up for free

No credit card required