Example Integrations

SendGrid

package.json

{
  "@sendgrid/mail": "^6.2.1"
}

Integration code

const sgMail = require('@sendgrid/mail')

module.exports = async ({ payload, secrets }) => {
  const { email, heading, viewedItem, recommendations } = payload
  const { SENDGRID_API_KEY } = secrets

  sgMail.setApiKey(SENDGRID_API_KEY)

  const msg = {
    to: email,
    from: 'no-reply@qshop-demo.com',
    subject: 'Don\'t forget these!',
    templateId: '66074b80-c939-41cf-bfed-acbe96f0da1e',
    substitutions: {
      heading,
      ...viewedItem,
      ...recommendations
    },
    text: ' ',
    html: ' '
  }

  const [response] = await sgMail.send(msg)
  if (response.statusCode !== 202) {
    console.error('Something went wrong!')
  } else {
    console.log('Email sent!')
  }
}

Experience Snippet

Direct Invocation

To call this integration from an experience:

options.integration('SendGrid').execute(payload)

Where payload is an object containing heading, viewedItem, and recommendations, which are passed as variables to the template in sendgrid.

Deferred Invocation

To queue up an integration to be executed in a define amount of time:

options.integration('SendGrid').schedule(payload, {
  delay: '10s'
}).then(() => {
  alert('Integration scheduled')
})

Where payload is an object containing heading, viewedItem, and recommendations, which are passed as variables to the template in sendgrid which will be executed in 10 seconds time.

Last updated: May 2020
Did you find this article useful?