Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Clear all filters
code share

Send OTP via sendgrid(email) API

Problem this snippet solves:

This is an example of how to send an OTP generated by the Big-IP via email using the sendgrid api.



Most of the code and examples in this tutorial are just cut and paste from different places with slight modifications. I've decided to try it out after trying the twilio tutorial by Niels van Sluis: https://devcentral.f5.com/codeshare/send-an-one-time-password-otp-via-the-twilio-sms-gateway-1132.

Afterward, I wanted to better understand the irule LX architecture and usage, so I watched this great tutorial by Artiom: https://www.youtube.com/watch?v=7yRP2fPCxIs&index=1&list=PLRL802iBI7n-6y8_eNawyeUZ8E0YwWro8

And this is my first attempt to write a tutorial...


  • In case you're configuring from scratch, you need to install sendgrid plugin, otherwise its already included in the tgz file.

To install the plugin:

cd /var/ilx/workspaces/Common/ilx_sendgrid/extensions/sendgrid/node_modules
npm install --save @sendgrid/mail
  • Attach the irule to the VS:

    when ACCESS_POLICY_AGENT_EVENT { if { [ACCESS::policy agent_id ] eq "sendgrid" } {

    set disName "[ACCESS::session data get session.ad.last.attr.displayName]"
    set generatedOTP "[ACCESS::session data get session.otp.assigned.val]"
    set userEmail "[ACCESS::session data get session.ad.last.attr.mail]"
    set username "[ACCESS::session data get session.logon.last.logonname]"
    log local0.info "Ilx Rule Hit"
    if {[info exists userEmail] && ($userEmail eq "")} {
        log local0. "Error: Email variable is empty; Could not send OTP."
    if {[info exists generatedOTP] && ($generatedOTP eq "")} {
        log local0. "Error: generatedOTP variable is empty; Could not send OTP to user: $username."
    set rpc_handle [ ILX::init sendgrid_plugin sendgrid]
    log local0.info "RPC Handle: $rpc_handle"
    if {[catch {set rpc_resp [ILX::call $rpc_handle -timeout 3000 ilx_sendmail $disName $generatedOTP $userEmail] } result] }{
     log local0.err "Ilx RPC issue: $result"
    log local0. "Sendgrid status for user $username, email: userEmail: $rpc_resp"  }}
  • Create a simple AP(or use this one) and configure it: https://github.com/alexnimo/F5/blob/master/iRules_LX/sendgrid/profile_Common_sendgrid_ap.conf.tar.gz Image Text

  • The index.js includes 2 options to send an email. If you want to use the first option. Simple email, then uncomment before //Use this method to send regular email and comment before //Use this method to send email with dynamic template

There are no additional configurations to be done.

  • If you want to send an email with a dynamic template(customized), the second(default) option then first you need to create the template via the sendgrid UI:


You can make different customizations to your email.

I've just used an example template which can be found on their github: https://github.com/sendgrid/email-templates/blob/master/paste-templates/password-reset.html and played with it just to understand how it works.

Here is an ugly modified template with the variables that will be presented in the email: https://github.com/alexnimo/F5/blob/master/iRules_LX/sendgrid/otp_template.html

More information can be found here:

https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/?mc=email&mcd=inboxchronicles18&utm_medium=email&utm_source=nurture&cvosrc=email.nurture.inboxchronicles18&utm_campaign=inboxchronicles18&mkt_tok=eyJpIjoiWkRoa1pqTTROakJtTVRaaCIsInQiOiJWd0FVdFJJb2N1eEczVjVPdTM1KzNrS0VlVjlNakpDVlM0UHNRTjNWdVwvNjZnQ09rcFBrM2MwclwvTXZsVnR3T2U4dWc0bStodXVnbmQzT2Y4ZndHSXlyXC9YZ092cVwvbk9FK1grblMweU84WllnS3pNMHdiM1JGTmFJdlNGNEdEdDAifQ%3D%3D https://sendgrid.com/docs/ui/sending-email/using-handlebars/

I've generated and used the code from the documentation:


If the default option is used(dynamic templates) then you must include the template id in the variable template_id

Hope I haven't forgot anything....

How to use this snippet:

Code variables:

var api_key = The api key generated in sendgrid

Everything else can be customized.

template_id = Template ID created in sendgrid.

Tested on Version: