Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Send Email Verifications with Verify and Twilio SendGrid


This guide explains how to set up and send email verifications using Twilio Verify and Twilio SendGrid.


Prerequisites

prerequisites page anchor

Using Verify with Twilio SendGrid requires the following configurations.

Set up your Twilio SendGrid account

set-up-your-twilio-sendgrid-account page anchor

Create a Twilio SendGrid account(link takes you to an external page) or log in to your existing account(link takes you to an external page). Email verifications use the Twilio SendGrid transactional email API.

Create a Twilio SendGrid API key

create-a-twilio-sendgrid-api-key page anchor

To create a Twilio SendGrid API key, use the Twilio SendGrid API or the Twilio SendGrid console(link takes you to an external page).

Create Twilio SendGrid API keys with limited scopes. Set to Restricted Access with the following limited scopes:

Access detailsAccess level
Mail Send > Mail SendFull Access
Mail Send > Scheduled SendNo Access
Template EngineRead Access

To set up your Verify email integration, save your Twilio SendGrid API key.

Set up domain authentication

set-up-domain-authentication page anchor

Domain authentication lets you send email from your company's domain without via sendgrid.net. Configure domain authentication, then authenticate your domain in the Twilio SendGrid console(link takes you to an external page).


Create an email template

create-an-email-template page anchor

To create or select a design for your template, go to the Twilio SendGrid Dynamic Templates page(link takes you to an external page). To include the Verify code in your email template, use at least one of the following three variables.

VariableNecessityContents
twilio_codeRequiredThe 4-10 digit One Time Passcode (OTP) without descriptive text.
twilio_messageRequiredBoth the internationalized descriptive text and the OTP. Language defaults to English. To override language, use the Locale parameter.
twilio_message_without_codeRequiredThe internationalized descriptive text only.
twilio_service_nameOptionalThe human-readable name given to the Service.
  • The twilio_message value resembles Your MyServiceName verification code is: 123456 or Su codigo de verificación para MyServiceName es: 123456.
  • The twilio_message_without_code value resembles Your MyServiceName verification code is or Su codigo de verificacion para MyServiceName es.

To find a list of your templates and their unique IDs, go to the Twilio SendGrid Dynamic Templates page(link takes you to an external page). Creating a Verify email integration requires a template ID. This ID contains 64 characters with one dash (d-uuid).

Example Twilio SendGrid HTML Template with variables

example-twilio-sendgrid-html-template-with-variables page anchor
1
<html>
2
<head>
3
<style type="text/css">
4
body, p, div {
5
font-family: Helvetica, Arial, sans-serif;
6
font-size: 14px;
7
}
8
a {
9
text-decoration: none;
10
}
11
a.verify {
12
background-color:#ffbe00;
13
color:#000000;
14
display:inline-block;
15
padding:12px 40px 12px 40px;
16
text-align:center;
17
}
18
</style>
19
<title></title>
20
</head>
21
<body>
22
<center>
23
<p>Example 1 - just the code (no localization in the message):</p>
24
<p>The verification code is: <strong>{{twilio_code}}</strong></p>
25
<p>Example 2 - use the code in a clickable link to trigger a verification check:</p>
26
<p>
27
<a href="https://example.com/signup/email/verify?token={{twilio_code}}"
28
class="verify" target="_blank">Verify Email Now</a>
29
</p>
30
<p>Example 3 - entire localized message and code:</p>
31
<p><strong>{{twilio_message}}</strong></p>
32
<p><a href="https://www.twilio.com/en-us/blog/insights/open-source-transactional-email-templates">Check out more templates</a></p>
33
<span style="font-size: 10px;"><a href=".">Email preferences</a></span>
34
</center>
35
</body>
36
</html>

This produces an email that resembles the following:

Email verification examples with code 918135 and 'Verify Email Now' button.

Transactional emails use Email preferences instead of Unsubscribe(link takes you to an external page).


Integrate Verify with Twilio SendGrid

integrate-verify-with-twilio-sendgrid page anchor

Integrating Verify with Twilio SendGrid involves creating an email integration and connecting to the Verify service.

Create an email integration

create-an-email-integration page anchor

To create an integration in the Twilio Console:

  1. Log in to the legacy Twilio Console.

  2. Go to Settings > Email Integration(link takes you to an external page).

  3. Click Create new email integration. The Create new email integration modal appears.

  4. Provide values for the following fields:

    FieldValue
    Integration nameHuman-readable name only visible within the Twilio console.
    “From” email addressThe sender email address displayed in the recipient's inbox. This

address must be verified in Twilio SendGrid. | | "From" name | Sender name displayed in the recipients's inbox. | | SendGrid API key | 69 character long key formatted as SG.xxxxxxxx.yyyyyyyy. | | Template ID | Found in your Twilio SendGrid account when you created your template. | 5. Click Create.

Connect to your Verify service

connect-to-your-verify-service page anchor

Connect your email integration to your Verify service in one of two ways:

Email integrationVerify services
  1. Go to Email Integration(link takes you to an external page).
  2. To associate with an email integration, choose one or more services you want.

You can use one email integration for multiple services.


Send a Verify notification using email

send-a-verify-notification-using-email page anchor

An email verification can use a create Verify request with or without overrides that modify the email template and other details or use Twilio SendGrid substitutions.

Basic verificationVerification with overridesVerification with substitutions
Start a verification with emailLink to code sample: Start a verification with email
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createVerification() {
11
const verification = await client.verify.v2
12
.services("VA305f5177a5c94bd22dba9aed7494c784")
13
.verifications.create({
14
channel: "email",
15
to: "recipient@foo.com",
16
});
17
18
console.log(verification.sid);
19
}
20
21
createVerification();

Check an email verification

check-an-email-verification page anchor

To check an email verification token, use the same code as other channels like SMS or voice.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createVerificationCheck() {
11
const verificationCheck = await client.verify.v2
12
.services("VA305f5177a5c94bd22dba9aed7494c784")
13
.verificationChecks.create({
14
code: "123456",
15
to: "recipient@foo.com",
16
});
17
18
console.log(verificationCheck.sid);
19
}
20
21
createVerificationCheck();