Automate OTP extraction
How to automate OTP extraction for Snyk API and Web authentication
Automate the extraction of one-time passwords (OTPs) from email and send them to Snyk API & Web for two-factor authentication.
When using alternative OTP for two-factor authentication, you must send the OTP code to Snyk during scans. You can automate this process by creating scripts that monitor your email, extract the OTP, and submit it using the Snyk API.
This article provides examples for automating OTP extraction using:
Google Apps Script (for Gmail)
Microsoft Power Automate (for Outlook)
Prerequisites
Configure alternative OTP two-factor authentication for your target before proceeding. Visit Configure two-factor authentication (2FA) for setup instructions.
You need the UNIQUE 2FA CONFIGURATION URL from the Authentication settings of your target.
Automate OTP extraction from Gmail
Use Google Apps Script to automate OTP extraction from Gmail accounts.
The script polls emails from the Gmail account, extracts the OTP using a regular expression, and sends it to Snyk through the API endpoint.
Create the Google Apps Script
Navigate to https://script.google.com and log in with the Gmail account that receives OTP emails.
Select My Projects and click CREATE APPS SCRIPT.

Click Untitled Project and enter a meaningful name (for example, "Extract OTPs from Email").

In the Code.gs file, replace the default myFunction with the following code:
Configure the script
Customize the runTask function with your specific configuration:
API_ENDPOINT
Replace <UNIQUE 2FA CONFIGURATION URL> with the URL that Snyk provides in the Authentication settings of your target.
SUBJECT_MATCH
The string or regular expression for the email subject line that contains the OTP. For example:
Static string:
'verification code'Regular expression:
/verification|authentication|2FA/i
INBOX_SEARCH_FILTER
Filter to select which emails to parse. The default filters for:
Unread emails
Received in the last hour
Subject matching
SUBJECT_MATCH
If you use a regular expression for SUBJECT_MATCH, remove the subject: parameter from this filter.
OTP_REGEXP_MATCH
Regular expression to extract the OTP from the email body. The default pattern expects the OTP in the second capture group:
First group: Text before the OTP
Second group: The OTP (six digits)
Third group: Text after the OTP
Example: '^(.* code is: )([0-9]{6})(.*)'
If you change the OTP position in the regular expression, update this line accordingly:
Save and test the script
Click Save to save your script.

Test the script manually by clicking Run.
Review the execution log to verify the script works correctly.
Set up automated execution
Configure a trigger to run the script automatically:
Navigate to Triggers and click Add Trigger.

Configure the trigger:

Choose which function to run:
extractOTPFunctionSelect type of time based trigger:
Minutes TimerSelect minute interval:
Every minuteFailure notification settings:
Notify me immediately
Click Save to activate the trigger.
The trigger executes the extractOTPFunction every minute, which runs the runTask function four times per minute (every 15 seconds) to check for new OTP emails.
Automate OTP extraction with Microsoft Power Automate
Use Microsoft Power Automate to automate OTP extraction from Outlook email accounts.
This flow retrieves an OTP from an email and sends it to Snyk through the API endpoint.
Prerequisites
Access to Power Automate (Premium license required for HTTP POST calls)
An email account configured in Power Automate that receives OTP emails
The UNIQUE 2FA CONFIGURATION URL from the Authentication settings of your target. Visit Configure two-factor authentication (2FA).
Create a new automated cloud flow
Log in to Power Automate.
From the left navigation pane, select + Create.
Choose Automated cloud flow.
Provide a flow name (for example, "Send OTP to Snyk API & Web").
Under Choose your flow's trigger, search for and select When a new email arrives (V3) (for Outlook).
Click Create.

Configure the email trigger
Configure the trigger action to identify OTP emails:
Folder: Select the inbox or folder where OTPs arrive.
Subject filter: Enter a keyword or phrase present in the OTP email subject (for example, "Your OTP Code" or "Verification Code").
From: Enter the email address from which the OTP email is sent.
Extract the OTP from the email body
Extract the OTP value from the email content using a compose action:
Click + New step.
Search for Compose and select it.
In the Inputs field, write an expression to extract the OTP:
Where:
13is the number of characters from the start of the phrase to the start of the OTP code6is the number of characters in the OTP codeoutputs(@'email_body')points to the email body
Alternatively, click the lightning bolt icon and select Body from the previous "When a new email arrives" step.
Test your expression carefully with actual OTP email content to ensure it extracts the correct value. Refine the expression based on your specific OTP email format.
Send the OTP via HTTP POST
Configure an HTTP action to send the extracted OTP to Snyk API & Web:
Click + New step.
Search for HTTP and select it.
Configure the HTTP action:
Method: Select POST
URI: Enter the UNIQUE 2FA CONFIGURATION URL from the Authentication settings of your target
Headers: Add a header with:
Name:
Content-TypeValue:
application/json
Body: Enter a JSON object containing the OTP:
Replace
'Compose'with the name of your Compose action if you changed it.
Save and test the flow
Click Save in the top right corner.
Click Test to run a test of the flow.
Select Manually to trigger the flow when an email matching the criteria arrives.
Complete the login process with 2FA to generate a test OTP email.
Verify the flow runs successfully and check the run history for any errors.
Troubleshooting
Email filtering: Double-check your subject filters and email criteria to ensure the correct emails trigger the flow.
OTP extraction: Use the "Test Flow" feature in Power Automate with actual email content to debug your OTP extraction expression. Inspect the outputs of each step to see which data Power Automate processes.
Related content
Last updated
Was this helpful?

