How to Use Syncano's New SendGrid Integration

Todd WackerTodd Wacker

Syncano allows you to integrate with many third-party APIs. In this tutorial, we're going to take a look at our newest integration with SendGrid.

About SendGrid

SendGrid is an easy-to-use platform for managing the emails you want to send to your users. With the recent news that competitor Mandrill is discontinuing its free tier for delivering transactional emails, SendGrid is another alternative for developers. Many businesses such as Airbnb, Foursquare, Spotify, and Uber use SendGrid for their transactional and marketing email needs. With its convenient Node.js library, we can integrate SendGrid directly into our Syncano Dashboard via a Script.

Syncano's Scripts

If you're unfamiliar with Syncano's Scripts, they allow users to run code in the cloud. Scripts can be written in Node, Python, Ruby, Go, PHP, and Swift. By running code in a Script, you can free up client-side resources to run other parts of your application. With this approach, you don't have to worry about provisioning and managing servers since Syncano takes care of all of this for you.

What are Syncano Solutions?

Syncano's Solutions are a way for developers to share code with each other. Solutions can be as simple as a single Class or Script, or as complex as several Scripts working together with Triggers and Schedules. Simply click "Solutions" at the top right corner of the Dashboard to view a list of the available Solutions, or to create your own!

How the Solution Works

Our Solution for this exercise is an integration with SendGrid's Node.js library. With a simple "require" statement, we are able to gain full access to the SendGrid functions. We don't have to worry about installing the correct module. Other third-party integrations are also easily available in this way.

To install the Solution, select "Solutions" at the top right of your Syncano Dashboard. Scroll down until you find the "SendGrid Mailer" Solution and install it to the Instance you want.

SendGrid Mailer Installation

To use this Solution, simply update the "config" tab with the information you need. One piece of required information is your SendGrid API Key. This can be found in your SendGrid Dashboard by clicking "Settings" then "API Keys". Click "Create API Key" at the top of the page and select "General API Key". Give the API Key a name and select "Full Access" under "Mail Send". Save the API Key and copy it. Navigate to your Syncano Dashboard and select the "SendGrid Mailer" Script. Under the "config" tab, update the "apiKey" value with your SendGrid API Key.

config

You can configure this Script any way you want. For example, if you wanted every email your application sends to include the same from email and fromName, just update those fields under the config tab.

Sending an Email from Anywhere in Your Application with Script Endpoints

You may want to send different emails when users perform different actions in your application. One way to do this is with a Script Endpoint. Script Endpoints allow you to run a Script by simply making an API call. You can also use a Script Endpoint to pass data your Script needs to run.

Luckily, the SendGrid Mailer Solution already comes with a Script Endpoint we can use called send_email. We can send emails anytime we want by calling this Script Endpoint. To do this in JavaScript, you can use the Syncano JavaScript Library.

var Syncano = require('syncano');  
var account = new Syncano({accountKey: "ACCOUNT_KEY"});  
var payload = {  
    "to": "YOUR_EMAIL_HERE", 
    "subject": "SendGrid Mailer Webhook Test", 
    "text": "This email is a test to see if the mailer worked correctly."
};

account.instance('INSTANCE_NAME').webhook('send_email').run(payload)  
  .then(function(res) {
    console.log(res);
  })
  .catch(function(err) {
    console.log(err);
  });

With the SendGrid Mailer Integration, you can include HTML in your emails, along with many other fields. Check out the config tab for more information.

For information about running Script Endpoints in different languages, click here.

Sending an Email when a User Profile is Updated with Triggers

For our second use case, we're going to send an email every time a user updates their user profile. First, let's go into the Script config and update the necessary fields. Update the from field with your email address. Give the email a subject such as "Your Profile Has Been Updated". Also, give the email some text such as "Congratulations! Your profile has been successfully updated!"

Next, we'll need to create a Trigger. This will run our Script every time our data changes, such as when we update a user_profile. In the Dashboard, select "Tasks". Click the pink button at the bottom right to create a Trigger. We'll give our Trigger a name such as "Profile Update Email". We'll set the signal to update, the class to user_profile, and the Script to SendGrid Mailer.

Creating a Trigger

By default, our user_profile Class will need to have a to field to hold the email address we want to send this email to. You could rename this to “email” or whatever you want, as long as you update the SendGrid Mailer Script to look at ARGS.email(or whatever you rename it as) instead of ARGS.to for this information.

Now, go into your user_profile Data Objects and edit the data in one of the fields. An email should be sent to the email address you listed, telling you that your profile has been updated! If you don't get an email, you can debug issues by clicking on the name of your Script Endpoint in the Dashboard. This will bring up a list of Traces. Clicking on one will show you any error messages that popped up during your request.

Sending an Email Every 5 Minutes with Schedules

The final use case for the SendGrid Mailer Solution would be if you wanted to send an email periodically. This would be useful if you wanted to send a weekly email to your customers, or if you wanted to check at the end of the month for customers who haven't renewed their service with you, so that you can remind them to pay. We'll use a simple example to illustrate how Schedules work, but know that the SendGrid Mailer can be customized to fit your application's specific needs.

Go back to the SendGrid Mailer Script config again and enter your email in the to field. Change the subject to something like "Just Checking In" and the text to something like "It's been five minutes. Just wanted to make sure everything is going great!".

Navigate back to "Tasks", but this time we're going to create a Schedule. Give the Schedule a label such as "5 Minute Mailer", set the Script to "SendGrid Mailer", and the CronTab to "Run every 5 minutes".

Creating a Schedule

Five minutes from now, you should receive an email with the subject and text you specified earlier. Make sure to go back into the Dashboard to delete the Schedule. Otherwise, you'll receive another email (or 144 if you leave the Schedule running overnight like I did).

Conclusion

You've just learned the basics of what can be accomplished with Syncano's SendGrid Mailer Integration. The great thing about Solutions is that they offer a way to integrate with third-party services, or another user's code, while still allowing you to customize the Solution to fit a variety of needs and use cases.

We're working to build more ready-made Solutions to integrate with services such as SendGrid. Of course, you can always develop (and share) your own. Just click the slider to make a Solution public once you've written it, and you'll be able to share your Solution with the community. Happy coding!

Questions? Comments? Reach me at @WackerTheHacker.

Build powerful apps in half the time

Use our serverless platform to set up your backend in minutes.

Learn more
Todd Wacker
Author

Todd Wacker

Dev Evangelist, Developer