SendGrid Contacts API / Marketing Automations / Javascript

Hi everyone,

So, I’m struggling to use SendGrid to send Welcome emails to users who sign up on my site. I’ve created the welcome emails in their Marketing → Automations section (and not in the email API where you create Template IDs) which is totally fine. SendGrid say that in order for new users to receive the emails, I need to add them as contacts in my SendGrid account. This is done either manually or via the Contacts API. I am trying the latter.

On the SendGrid Contacts API page, I found the Java code, pasted it across into my Bubble application within a ‘run javascript’ step, the third and final step of my workflow since by then, the user will have filled the appropriate information on the app. Note, I added dynamic data into the code, on the user’s email, full name, location, and subscription details. Please see below:

Screenshot 2022-09-03 at 12.47.41 PM

Anyways, I’ve tested it and it doesn’t work. I’ve probably written the wrong code (I’ve also written the code below), so was wondering if someone could take a quick look and let me know where I may have gone wrong? Huge thanks in advance!

Best,
Abbas

import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
try {
SendGrid sg = new SendGrid(System.getenv(“This is where I have inserted my Sendgrid API”));
Request request = new Request();
request.setMethod(Method.PUT);
request.setEndpoint("/marketing/contacts");
request.setBody("{“contacts”: [{“email”: “Current User’s email”, “custom_fields”: {“full_name”: “Current User’s Full Name”, “location”: “Current User’s Location”, “subscription”: “Current User’s Subscription’s Display”}}]}");
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) { throw ex; }
}
}

Why you dont use API connector?

2 Likes

What @Jici said above. You don’t need to work so hard. Get rid of that Javascript (Java !== Javascript) and use a plugin. There are multiple free plugins, SendGrid Pro is a newer one that will add a contact.

Search for the plugin, install and replace the Run javascript step with the action from the plugin.

Finally if you want to see the API code to implement it yourself, you can view the plugin here and see how the API is defined: Bubble Plugin Editor - SendGrid Pro (click the API Calls tab)

2 Likes

Thank you both.

So, downloaded the plug-in and followed the stops above. But, I’m not entirely sure how to complete the final step:

Of course I’d want to collect the user’s email so I’ve added the dynamic data ‘Current User’s email’ into the (body) Email Address. And in the (body) Custom Fields, I’ve added the rest of the data I’d like collected (dynamic). Sadly, it still does not work and I’ve tried without the quotation marks.

Am I going about this in the right way? Do I need to complete another step before, like letting my SendGrid account know about the custom fields?

Huge thanks in advance.

Sorry for the delay on this response @a.alighanbari . I tested to confirm this works last week but failed to respond to you.

Yes you need to setup the custom fields prior to trying to use them. Additionally, SendGrid requires that you use the custom field id rather than the name (which would be so much nicer imo).

Here are some screenshots that show how to get the id and how to use the plugin:

Add the custom field in the SendGrid UI:

Use this endpoint (I’m showing here in postman) to get the ids of your custom fields:

Use the id to add the custom field value via the SendGrid Pro plugin:

Hey! No probs, thanks for getting back to me. I’m still struggling here. I created two custom fields and finally managed to find the id’s via postman and added them to the workflow. FYI one of the fields, location/country, is a reserved field, I’ve included this in the custom fields box (don’t think it should pose a problem?).

I tested this by registering as a new user several times, and no contacts are being added to my SendGrid and thus no welcome emails are being sent.

Any ideas on what the issue could be?

Thanks in advance.

@a.alighanbari You actually indicated the issue in your post:

This is exactly the problem. If you take a look at the documentation for updating custom field you will find this:

You can only update custom fields in the custom fields section. If you remove the reserved field you will find that your contacts will be added with the custom field values you specified.

1 Like

I would stay away from storing data in SendGrid as they charge you to store contacts. I was paying hundreds of dollars a month to them to store a first name, last name, and email address.

Store the data in your system cheaply then use the API to use SendGrid for the actual email sends.

1 Like

Solid advice :point_up:

Just for completeness sake, if for some reason you wanted to store the contact data in SendGrid and wanted to add/update contacts with reserved fields and custom fields, there is another action on the plugin that will let you do just that:

1 Like

Thanks for the advice. Would I still be able to instantly send my fancier image-rich welcome email (saved on SendGrid) or succumb to the rich text email layout on Bubble?

Also, specifically which SendGrid API?

Cheers.

It worked(!) perfect, thank you… But now I’m worried about the costs @csakon mentioned… Having read through their pricing plans, it seems nice and cheap up to a few thousand but once you’re in the hundreds of thousands it starts to add up.

@a.alighanbari @bubble.trouble will I be able to add contacts to a specific list using this API? I don’t see any field for inputting the sendgrid list ID in this plugin

your help will be appreciated