Cloud Code Functions
Integrations
SendGrid
11 min
using sendgrid email api introduction this section explains how you can integrate sendgrid with a cloud code function after completing this guide with step by step instructions, you will be ready to use your function in your app and call it at your ios or android app prerequisites to complete this tutorial, you will need an app created at back4app follow the create new app tutorial to learn how to create an app at back4app back4app command line configured with the project follow the setting up cloud code tutorial to learn how to set up cloud code for a project account created in twilio let’s get started! we will write a function using sendgrid that you will be able to work with a lot from possibilities as delivering messages to our customers and configuring parameters to use the sendgrid v3 rest api to learn how to create or access an account in sendgrid, check the links given below create a new account log in to your account 1 create a sendgrid api key the most important step before start to code is create the correct keys to setup your environment after accessing your account, locate in the settings drop down menu, the api keys api keys option as in the picture below after that, at the top right locate and choose an identification to the api key name api key name , like as shown below as you can see at the image above, it’s necessary to select one option to allow the full access to the api key after click on the create & view create & view to proceed with creating of the key, you will be able to see the screen below hint be careful to write it down, as there is no way to retrieve it click on the text to copy it 2 add a function to the cloud code the main strategy to this way of using sendgrid’s api is to create a function on the cloud code named sendgridemail sendgridemail and call it from the app 2 1 install module from sendgrid create a file called package json package json , and inside this file, you need to install the twilio module, like 1 { 2 "dependencies" { 3 "@sendgrid/mail" " " 4 } 5 } 2 2 implement cloud code you should note that every email field has to be send by the app – from the subject to the content – as a parameters the code is as follows parse server 3 x 1 parse cloud define("sendgridemail", async(request) => { 2 const sgmail = require('@sendgrid/mail'); 3 4 // import sendgrid module and call with your sendgrid api key 5 sgmail setapikey("your sendgrid api key here"); 6 7 const msg = { 8 to request params toemail, 9 replyto 'info\@youremail com', 10 from 'info\@youremail com', 11 subject request params subject, 12 text request params body 13 }; 14 15 try{ 16 await sgmail send(msg); 17 return 'ok' 18 } catch (e){ 19 return `error ${e message}` 20 } 21 22 }); parse server 2 x 1 parse cloud define("sendgridemail", (request, response) => { 2 const sgmail = require('@sendgrid/mail'); 3 4 // import sendgrid module and call with your sendgrid api key 5 sgmail setapikey("your sendgrid api key here"); 6 7 const msg = { 8 to request params toemail, 9 replyto 'info\@youremail com', 10 from 'info\@youremail com', 11 subject request params subject, 12 text request params body 13 }; 14 15 sgmail send(msg) then(() => { 16 response success("the message was sent!"); 17 }) 18 catch(error => { 19 //log friendly error 20 response error(error tostring()); 21 }); 22 }); hint remember to change the fields from from and reply to reply to to your personal information then it is necessary to implement a call to the cloud code function on the app 3 call cloud code function in this current step, we can work with two possibilities to call our function, they’re android and ios (swift and objective c) android 1 map\<string, string> params = new hashmap<>(); 2 3 // create the fields "emailaddress", "emailsubject" and "emailbody" 4 // as strings and use this piece of code to add it to the request 5 params put("toemail", emailaddress); 6 params put("subject", emailsubject); 7 params put("body", emailbody); 8 9 parsecloud callfunctioninbackground("sendgridemail", params, new functioncallback\<object>() { 10 @override 11 public void done(object response, parseexception exc) { 12 if(exc == null) { 13 // the function executed, but still has to check the response 14 } 15 else { 16 // something went wrong 17 } 18 } 19 }); ios(swift) 1 pfcloud callfunctioninbackground("sendgridemail", withparameters \[ 2 // these fields have to be defined earlier 3 "toemail" toemail, 4 "subject" subject, 5 "body" body 6 ]) { (response, error) in 7 if error == nil { 8 // the function executed, but still has to check the response 9 } else { 10 // the function returned an error 11 } 12 } ios(objective c) 1 \[pfcloud callfunctioninbackground @"sendgridemail" 2 withparameters @{@"toemail" toemail, 3 @"subject" subject, 4 @"body" body} 5 block ^(nsstring myalertmsg, nserror error){ 6 if(!error) { 7 // the function executed, but still has to check the response 8 } 9 else { 10 // the function returned an error 11 } 12 } 13 ]; 4 it’s done! and that’s it for the sendgrid usage note that you might want to use some sort of authentication before allowing anyone to use your sendgrid api to send emails in case you need any help or a function/link doesn’t work, please contact our team via chat!