Testing Twilio Verify Email OTP: A Developer Guide

Testing Twilio Verify Email OTP: A Developer Guide

Twilio Verify can send one-time codes by email through a SendGrid account that you connect. Test it with addresses you control; a temporary inbox is fine for one manual look, but on a Twilio trial email goes only to your sign-up address.

What you need to set up

Twilio's Verify email guide lists the parts:

  1. A Twilio SendGrid account and an API key with limited scopes.
  2. A verified sender, better an authenticated domain. Without it, recipients see "via sendgrid.net".
  3. A dynamic template in SendGrid that contains the code variable, for example {{twilio_code}}. Its ID starts with d-.
  4. An email integration in Verify that joins key, template and sender, assigned to your Verify service.

Each of these is a place where a test can fail before any mail is sent.

Limits that surprise testers

LimitSource
On a trial, email is restricted to the address you signed up withTwilio trial docs
Trials last 30 days and include 40 verifications and 3,000 emailsSame page
Custom content and custom code templates aren't available in the trialSame page
The verification SID is deleted once approved, expired after 10 minutes, or out of attemptsVerification Check API
Checking a deleted verification gives a 404Same page. Treat it as "expired", not as a server error

The first line decides your approach: with a trial you can't send codes to a throwaway address at all, unless you signed up with it. For an account, that is a bad idea. Why.

Starting a verification

curl -X POST "https://verify.twilio.com/v2/Services/$SERVICE_SID/Verifications" \
  --data-urlencode "[email protected]" \
  --data-urlencode "Channel=email" \
  -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

Checking the code:

curl -X POST "https://verify.twilio.com/v2/Services/$SERVICE_SID/VerificationCheck" \
  --data-urlencode "[email protected]" \
  --data-urlencode "Code=123456" \
  -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"

Test plan

Automated, on every build. Don't call Twilio. Replace it with a stub that answers "pending", "approved", "max_attempts_reached" and 404. You test your own screens and messages quickly and for free.

Automated, once a day. One real round trip on an upgraded account. Send to a catch-all such as verify-{time}@qa.yourdomain, read it by IMAP, and check the code. That exercises key, template, sender and domain.

By hand, before a release.

  1. Send to your own mailboxes at two or three large providers. Look at sender, subject, spam folder and phone preview.
  2. With an upgraded account, send one to a temporary inbox from MTempMail to see the mail without any personal filters. Use a test user with no real data, as the inbox has no password.
  3. Enter a wrong code, an old code, and a right code twice.
  4. Wait eleven minutes and try the first code.
  5. Request two codes and note which one is accepted.

What not to test this way: delivery at scale. Sending many codes to disposable domains tells you little and can hurt your sender reputation.

Reading errors

  • No mail, status pending: look in SendGrid's activity for a drop or bounce. A wrong template ID or unverified sender is the first suspect.
  • Mail without a code: the template lacks the variable.
  • 404 on check: expired, already approved, or a different To spelling. Use the same form of the address in both calls, including letter case.
  • Mail in spam: authenticate the domain. A single verified sender isn't enough for volume.

Common questions

Does Verify need SendGrid for email?

Yes, according to Twilio's guide the email channel uses your SendGrid account.

Can MTempMail's API read the code for me?

Not at the moment. It is off. See other inboxes for tests.

Can I test SendGrid without sending?

There is a sandbox mode for that. How it works.

Tags:
#twilio verify email # twilio verify email otp # test twilio verify # sendgrid verify integration
Zubair Rahmani
Written by
Zubair Rahmani
Zubair is an app and website developer with 7 years of experience. Zubair built and runs MTempMail, a free temporary email service. Contact
Categories