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


In this guide, you'll learn how to set up and send email verifications using Twilio Verify and SendGrid. You'll configure your SendGrid account, create an email integration in Verify and send verification emails to your users.

Set up your SendGrid account

set-up-your-sendgrid-account page anchor

Create a 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 SendGrid transactional email API.

Create a SendGrid API key

create-a-sendgrid-api-key page anchor

Next, Create a SendGrid API key(link takes you to an external page) by using the API or the SendGrid dashboard.

(warning)

Warning

We strongly recommend generating SendGrid API keys with limited scopes.

Create an API key in one of two ways:

Access detailsAccess level
Mail Send (nested under Mail Send category)Full Access
Scheduled Send (nested under Mail Send category)No Access
Template EngineRead Access
(information)

Info

Save your SendGrid API key. You'll need it to set up your verification email integration.

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". See Configure domain authentication, and then authenticate your domain in the SendGrid dashboard(link takes you to an external page).

(warning)

Warning

It can take up to 48 hours for the records to verify after you upload them into your DNS host. You can still continue setup and testing while domain authentication is pending by using Single Sender Verification. Single sender verification allows you to send from a single email address that you confirm ownership of by clicking a verification link in the email's inbox. For more information on the differences between domain authentication and single sender verification, see Sender Identity.

Create an Email Template

create-an-email-template page anchor

Go to the SendGrid Dynamic Templates page(link takes you to an external page) and create a new template. In this process, you'll name and create or select a design for your template. Learn more about how to send email with dynamic templates.

Use at least one of the first three variables below to include the verify code in your email template.

Available variablesDescription
twilio_codeThe 4-10 digit numeric One Time Passcode. OTP only, no descriptive text.
twilio_messageContains both internationalized descriptive text and the OTP. For example, "Your MyServiceName verification code is: 123456" or "Su codigo de verificación para MyServiceName es: 123456". Default language is English, override language using the Locale parameter.
twilio_message_without_codeContains the internationalized descriptive text only. For example, "Your MyServiceName verification code is" or "Su codigo de verificacion para MyServiceName es".
twilio_service_name(Optional) the Friendly Name of the Service.

Find Template ID

find-template-id page anchor

You can find a list of your templates and their unique IDs on the SendGrid Dynamic Templates page(link takes you to an external page). A template ID is 64 characters with one dash (d-uuid) and is required for creating a Verify email integration.

(information)

Info

Need template design help? Check out SendGrid's free and open source library of transactional email templates(link takes you to an external page) for robust and responsive designs.

Example SendGrid HTML Template with variables

example-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
</style>
12
<title></title>
13
</head>
14
<body>
15
<center>
16
<p>
17
Example 1 - just the code (no localization in the message):
18
</p>
19
<p>
20
The verification code is: <strong>{{twilio_code}}</strong>
21
</p>
22
<p>
23
Example 2 - use the code in a clickable link to trigger a verification check:
24
</p>
25
<p>
26
<a href="https://example.com/signup/email/verify?token={{twilio_code}}"
27
style="background-color:#ffbe00; color:#000000; display:inline-block; padding:12px 40px 12px 40px; text-align:center; text-decoration:none;"
28
target="_blank">Verify Email Now</a>
29
</p>
30
<p>
31
Example 3 - entire localized message and code:
32
</p>
33
<p>
34
<strong>{{twilio_message}}</strong>
35
</p>
36
<p><a href="https://sendgrid.com/blog/open-source-transactional-email-templates/">Check out more templates</a></p>
37
<span style="font-size: 10px;"><a href=".">Email preferences</a></span>
38
</center>
39
</body>
40
</html>

This will produce an email that resembles the following:

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

Info

We recommend using Email preferences instead of Unsubscribe for transactional emails like these. See Should you include an unsubscribe link in your transactional email messages?(link takes you to an external page)

To learn more about email deliverability, check out the following links:


Create a Verify email integration

create-a-verify-email-integration page anchor

Go to the Email Integration(link takes you to an external page) section of the Verify Console to create a new integration. Name your integration; you can change the name later.

Fill in the following required fields:

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

Option 1: From Email Integration(link takes you to an external page), check the service or services you want to associate with that email integration. A single email integration can be used for multiple services.

or

Option 2: From Verify Services(link takes you to an external page), select your service and navigate to the Email tab to select an email integration. Each service can only have one email integration.


Send an email verification

send-an-email-verification page anchor

Send your first email verification:

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("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.verifications.create({
14
channel: "email",
15
to: "recipient@foo.com",
16
});
17
18
console.log(verification.sid);
19
}
20
21
createVerification();

Response

Note about this response
1
{
2
"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"to": "recipient@foo.com",
6
"channel": "email",
7
"status": "pending",
8
"valid": false,
9
"date_created": "2015-07-30T20:00:00Z",
10
"date_updated": "2015-07-30T20:00:00Z",
11
"lookup": {},
12
"amount": null,
13
"payee": null,
14
"send_code_attempts": [
15
{
16
"time": "2015-07-30T20:00:00Z",
17
"channel": "SMS",
18
"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19
}
20
],
21
"sna": null,
22
"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23
}

You can override the default template ID, default from name, or default from email with a channel configuration:

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("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.verifications.create({
14
channel: "email",
15
channelConfiguration: {
16
template_id: "d-4f7abxxxxxxxxxxxx",
17
from: "override@example.com",
18
from_name: "Override Name",
19
},
20
to: "recipient@foo.com",
21
});
22
23
console.log(verification.sid);
24
}
25
26
createVerification();

Response

Note about this response
1
{
2
"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"to": "recipient@foo.com",
6
"channel": "email",
7
"status": "pending",
8
"valid": false,
9
"date_created": "2015-07-30T20:00:00Z",
10
"date_updated": "2015-07-30T20:00:00Z",
11
"lookup": {},
12
"amount": null,
13
"payee": null,
14
"send_code_attempts": [
15
{
16
"time": "2015-07-30T20:00:00Z",
17
"channel": "SMS",
18
"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19
}
20
],
21
"sna": null,
22
"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23
}

Channel configuration also supports SendGrid substitutions(link takes you to an external page).

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("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.verifications.create({
14
channel: "email",
15
channelConfiguration: {
16
substitutions: {
17
username: "Foo Bar",
18
},
19
},
20
to: "recipient@foo.com",
21
});
22
23
console.log(verification.sid);
24
}
25
26
createVerification();

Response

Note about this response
1
{
2
"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"to": "recipient@foo.com",
6
"channel": "email",
7
"status": "pending",
8
"valid": false,
9
"date_created": "2015-07-30T20:00:00Z",
10
"date_updated": "2015-07-30T20:00:00Z",
11
"lookup": {},
12
"amount": null,
13
"payee": null,
14
"send_code_attempts": [
15
{
16
"time": "2015-07-30T20:00:00Z",
17
"channel": "SMS",
18
"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19
}
20
],
21
"sna": null,
22
"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23
}

Check an email verification

check-an-email-verification page anchor

Checking an email verification token uses the same code as other channels, such as 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("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.verificationChecks.create({
14
code: "123456",
15
to: "recipient@foo.com",
16
});
17
18
console.log(verificationCheck.sid);
19
}
20
21
createVerificationCheck();