Skip to contentSkip to navigationSkip to topbar
Rate this Page:

Send Email Verifications with Verify and Twilio SendGrid


Here's your checklist for setting up your account to send email verifications with Verify and SendGrid. Detailed instructions for each step are included below.

  1. [SendGrid] Create or log in to your SendGrid account
  2. [SendGrid] Create a SendGrid API Key
  3. [SendGrid] Set up domain authentication
  4. [SendGrid] Create an email template
  5. [Verify] Create a Verify service(link takes you to an external page)
  6. [Verify] Create an email integration and connect to your Verify service
  7. [Verify] Send an email verification code
  8. [Verify] Check an email verification code

The initial configuration in Steps 1-6 only needs to be done once. You can then use your email-configured Verify service to send verification emails through the API.

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) via the API or in the SendGrid dashboard

(warning)

Warning

We strongly recommend generating SendGrid API Keys with limited scopes(link takes you to an external page).

Create an API Key in one of two ways:

Option 1: Create an API Key with the SendGrid API(link takes you to an external page).

Option 2: Create an API Key in the SendGrid UI(link takes you to an external page). For permissions, select "Restricted Access" and choose the following limited scopes:

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 allows you to send an email from your company's domain without "via sendgrid.net". Head over to the SendGrid docs to learn about how to set up domain authentication(link takes you to an external page) 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 set-up and testing while domain authentication is pending by using Single Sender Verification(link takes you to an external page). 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. Read Sender Identity(link takes you to an external page) for more information on the differences between domain authentication and single sender verification.

Create an Email Template

create-an-email-template page anchor

Navigate to the SendGrid Dynamic Templates page(link takes you to an external page) and create a new template. In this process you will name and create or select a design for your template. For more information on creating and working with dynamic templates, reference the SendGrid documentation(link takes you to an external page).

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

Available VariablesDescription
{{twilio_code}}The 4-10 digit numeric One Time Passcode. OTP only, no descriptive text.
{{twilio_message}}Contains both internationalized descriptive text and OTP. i.e. "Your MyServiceName verification code is: 123456" or "Su codigo de verificacion para MyServiceName es: 123456". Default language is English, override language using the Locale parameter.
{{twilio_message_without_code}}Contains the internationalized descriptive text only. i.e. "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

_40
<html>
_40
<head>
_40
<style type="text/css">
_40
body, p, div {
_40
font-family: Helvetica, Arial, sans-serif;
_40
font-size: 14px;
_40
}
_40
a {
_40
text-decoration: none;
_40
}
_40
</style>
_40
<title></title>
_40
</head>
_40
<body>
_40
<center>
_40
<p>
_40
Example 1 - just the code (no localization in the message):
_40
</p>
_40
<p>
_40
The verification code is: <strong>{{twilio_code}}</strong>
_40
</p>
_40
<p>
_40
Example 2 - use the code in a clickable link to trigger a verification check:
_40
</p>
_40
<p>
_40
<a href="https://your-company.com/signup/email/verify?token={{twilio_code}}"
_40
style="background-color:#ffbe00; color:#000000; display:inline-block; padding:12px 40px 12px 40px; text-align:center; text-decoration:none;"
_40
target="_blank">Verify Email Now</a>
_40
</p>
_40
<p>
_40
Example 3 - entire localized message and code:
_40
</p>
_40
<p>
_40
<strong>{{twilio_message}}</strong>
_40
</p>
_40
<p><a href="https://sendgrid.com/blog/open-source-transactional-email-templates/">Check out more templates</a></p>
_40
<span style="font-size: 10px;"><a href=".">Email preferences</a></span>
_40
</center>
_40
</body>
_40
</html>

This will produce an email that looks like:

verify email template.
(information)

Info

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


Create a Verify email integration

create-a-verify-email-integration page anchor

Head to the Email Integration(link takes you to an external page) section of the Twilio Verify console to create a new integration. Name your integration, this can be changed later. Fill in:

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

Now the exciting part! Send your first email verification with the following code:

Start a verification with email

start-a-verification-with-email page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_11
// Download the helper library from https://www.twilio.com/docs/node/install
_11
// Find your Account SID and Auth Token at twilio.com/console
_11
// and set the environment variables. See http://twil.io/secure
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11
.verifications
_11
.create({to: 'recipient@foo.com', channel: 'email'})
_11
.then(verification => console.log(verification.sid));

Output

_23
{
_23
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"to": "recipient@foo.com",
_23
"channel": "email",
_23
"status": "pending",
_23
"valid": false,
_23
"date_created": "2015-07-30T20:00:00Z",
_23
"date_updated": "2015-07-30T20:00:00Z",
_23
"lookup": {},
_23
"amount": null,
_23
"payee": null,
_23
"send_code_attempts": [
_23
{
_23
"time": "2015-07-30T20:00:00Z",
_23
"channel": "SMS",
_23
"attempt_sid": "VLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}
_23
],
_23
"sna": null,
_23
"url": "https://verify.twilio.com/v2/Services/VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Verifications/VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}

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

Start a verification with email

start-a-verification-with-email-1 page anchor

Override default email configuration

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_15
// Download the helper library from https://www.twilio.com/docs/node/install
_15
// Find your Account SID and Auth Token at twilio.com/console
_15
// and set the environment variables. See http://twil.io/secure
_15
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_15
const authToken = process.env.TWILIO_AUTH_TOKEN;
_15
const client = require('twilio')(accountSid, authToken);
_15
_15
client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_15
.verifications
_15
.create({channelConfiguration: {
_15
template_id: 'd-4f7abxxxxxxxxxxxx',
_15
from: 'override@example.com',
_15
from_name: 'Override Name'
_15
}, to: 'recipient@foo.com', channel: 'email'})
_15
.then(verification => console.log(verification.sid));

Output

_23
{
_23
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"to": "recipient@foo.com",
_23
"channel": "email",
_23
"status": "pending",
_23
"valid": false,
_23
"date_created": "2015-07-30T20:00:00Z",
_23
"date_updated": "2015-07-30T20:00:00Z",
_23
"lookup": {},
_23
"amount": null,
_23
"payee": null,
_23
"send_code_attempts": [
_23
{
_23
"time": "2015-07-30T20:00:00Z",
_23
"channel": "SMS",
_23
"attempt_sid": "VLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}
_23
],
_23
"sna": null,
_23
"url": "https://verify.twilio.com/v2/Services/VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Verifications/VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}

Channel Configuration also support Sendgrid's Substitutions(link takes you to an external page).

Start a verification with email

start-a-verification-with-email-2 page anchor

Specify Sendgrid Substitutions

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_15
// Download the helper library from https://www.twilio.com/docs/node/install
_15
// Find your Account SID and Auth Token at twilio.com/console
_15
// and set the environment variables. See http://twil.io/secure
_15
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_15
const authToken = process.env.TWILIO_AUTH_TOKEN;
_15
const client = require('twilio')(accountSid, authToken);
_15
_15
client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_15
.verifications
_15
.create({channelConfiguration: {
_15
substitutions: {
_15
username: 'Foo Bar'
_15
}
_15
}, to: 'recipient@foo.com', channel: 'email'})
_15
.then(verification => console.log(verification.sid));

Output

_23
{
_23
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"to": "recipient@foo.com",
_23
"channel": "email",
_23
"status": "pending",
_23
"valid": false,
_23
"date_created": "2015-07-30T20:00:00Z",
_23
"date_updated": "2015-07-30T20:00:00Z",
_23
"lookup": {},
_23
"amount": null,
_23
"payee": null,
_23
"send_code_attempts": [
_23
{
_23
"time": "2015-07-30T20:00:00Z",
_23
"channel": "SMS",
_23
"attempt_sid": "VLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}
_23
],
_23
"sna": null,
_23
"url": "https://verify.twilio.com/v2/Services/VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Verifications/VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}


Check an email verification

check-an-email-verification page anchor

Checking an email verification token uses the same code as other channels like sms or voice.

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_11
// Download the helper library from https://www.twilio.com/docs/node/install
_11
// Find your Account SID and Auth Token at twilio.com/console
_11
// and set the environment variables. See http://twil.io/secure
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11
.verificationChecks
_11
.create({to: 'recipient@foo.com', code: '123456'})
_11
.then(verification_check => console.log(verification_check.sid));

Output

_14
{
_14
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"to": "recipient@foo.com",
_14
"channel": "email",
_14
"status": "approved",
_14
"valid": true,
_14
"amount": null,
_14
"payee": null,
_14
"sna_attempts_error_codes": [],
_14
"date_created": "2020-01-30T20:00:00Z",
_14
"date_updated": "2020-01-30T20:00:00Z"
_14
}


Rate this Page: