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
      from: { type: String }
      to: { type: String }
      subject: { type: String }
      text: { type: String }
      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`

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

  send: async (inputs) => {
    // Sends an email with the inputs
    const result = await sendgrid.send({
      from: inputs.from,
      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 workflow

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

# liteflow.yml
      - type: trigger
        service: http
        eventKey: request
        endpointKey: send-email
      - type: task
          src: ./services/email
        taskKey: send
          to: { key: }
          subject: { key: body.subject }
          text: { key: body.text }

This workflow 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 workflows on the documentation.

# Get a sendgrid API key

You now need to get your own private SendGrid API key. You can create it here:

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 and be able to hit this endpoint to trigger your email.

curl -XPOST \
  -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

Increase your product development speed

Grow your business faster with the Liteflow Toolkit for Lean Software Development

Sign up for free

No credit card required