Tutorials

Building a DNS Email Validation Service with Cloudflare Workers & ApyHub API

This step-by-step guide will help you build your own bulk email validation service through an API. Learn how Cloudflare Workers and ApyHub's API enable filtering invalid addresses at scale. Validate email lists in minutes by implementing this serverless function.
Building a DNS Email Validation Service with Cloudflare Workers & ApyHub API
SO
Sohail Pathan
Last updated on February 14, 2024
Use case: An application requires a mechanism to validate a list of emails provided by the user in a CSV file format. The goal is to filter out emails that do not pass the DNS validation process, separating them between valid and invalid ones. This can ensure that the application only processes or uses email addresses that are likely to be active and properly configured according to DNS records.
Approach: Creating a serverless function that validates email addresses using Cloudflare Workers and ApyHub's DNS Email Validation API
DNS-Email-Validation.png
What the serverless function will do:
  • Receive and parse the CSV file of email addresses
  • Validate the email address and assign it to a category. ( validEmails and invalidEmails)
  • After all, emails are processed, the function generates and returns a new output CSV file that contains valid and invalid email addresses.
This tutorial will guide you through setting up your development environment, writing the serverless function in TypeScript, testing its functionality, and deploying the function with Cloudflare Workers.
Prerequisites:
  1. Standard IDE ( VSCode/Sublime)
  2. Node.js installed on your computer.
  3. An account with Cloudflare Workers.
  4. An API key from ApyHub after signing up for their DNS Email Validation API
Want to test the API beforehand? Chrome-extension.jpg

PART 1: Setting Up Your Project

Step 1: Create a new directory for your project and navigate into it:

1
cloudflare-email-validator
2
cd cloudflare-email-validator

Step 2: Install the Wrangler package

To install the Wrangler package in the directory, run the following command in the terminal:
1
npm install wrangler --save-dev

Step 3: Create a new Worker project

To start creating your Worker project, run the following command:
1
npm create cloudflare@latest
This command will prompt you to answer a few questions. Here are the answers you should provide:
  • In which directory do you want to create your application? Write email-validation-worker and press Enter.
  • What type of application do you want to create? Select Hello World worker and press Enter.
  • Do you want to use TypeScript? Select Yes and press Enter.
  • Do you want to deploy your application? Select Yes, you will be asked to authenticate using your Cloudflare account (if not logged in already)
Once you've answered all the questions, Wrangler will create a new directory with the name email-validation-worker. In the src/ folder of your new project, you will find the index.ts file. This is where you will write your email validation function.
Note: Feel free to delete the existing template code, as we will start writing from scratch.

PART 2: Writing a Serverless Function

Step 1: Handling the Incoming Request

The function starts by checking if the incoming request is a POST request. If not, it returns a 405 status code, indicating that the method is not allowed.
1
export default {
2
3
async fetch(request: Request, env: any, ctx: any): Promise<Response> {
4
if (request.method !== 'POST') {
5
return new Response('Expected POST',{ status: 405 });
6
}

Step 2: Parsing the Uploaded CSV File

It then attempts to parse the uploaded file from the request's form data. If no file is found, or the file is not a valid File object, it returns a 400 status code, indicating a bad request.
1
const formData = await request.formData();
2
const file = formData.get('file');
3
if (!file || !(file instanceof File)) {
4
return new Response('No file uploaded', { status: 400 });
5
}
6
7
// The content of the file is read as text, and each line is split into individual email addresses.
8
9
// Empty lines are filtered out.
10
11
const text = await file.text();
12
const emails = text.split('\n').map(email => email.trim()).filter(email => email !== '');

Step 3: Validating Emails in Batches

The function then processes the list of emails. For each email, it validates the email by calling the validateEmail function and waits for the result. This is done sequentially, with a 1-second delay between each email validation.
1
for (let i = 0; i < emails.length; i++) {
2
const email = emails[i];
3
const result : any = await this.validateEmail(email);
4
if (result.data) {
5
validEmails.push(email);
6
} else {
7
invalidEmails.push(email);
8
}
9
// Wait for 1 second before processing the next email
10
await new Promise(resolve => setTimeout(resolve, 1000))
11
}
After receiving the validation results, emails are categorised into validEmails and invalidEmails based on the response from the validateEmail function.

Step 5: Generating a New CSV File

Once all emails are processed and categorised, the function generates a new CSV content string. It iterates over the original list of emails and matches them with their validation status. The CSV string is constructed by combining both valid and invalid emails, ensuring that undefined values are replaced with an empty string.
1
let csvContent = 'Emails,Invalid Emails\n';
2
emails.forEach((email, index) => {
3
csvContent += `${email},${invalidEmails[index]}\n`;
4
});
5
6
//`csvContent` is then returned as a response with the appropriate headers to indicate the content type as CSV and to prompt the user to download the file named `email_validation_result.csv.`
7
8
return new Response(csvContent.replaceAll('undefined', ''), {
9
headers: {
10
'Content-Type': 'text/csv',
11
'Content-Disposition': 'attachment; filename="email_validation_result.csv"'
12
}
13
});

Step 6: Validating an Email with ApyHub's API

The validateEmail function performs the actual validation by sending a POST request to ApyHub's API endpoint with the email address to be validated. The API key is included in the request headers.
1
const apiResponse = await fetch('https://api.apyhub.com/validate/email/dns', {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
'apy-token': 'YOUR_SECRET_API_KEY' //provide your secret-token-here
6
},
7
body: JSON.stringify({email})
8
});
Note: It checks if the API response is successful. If not, it logs an error and returns false. If the response is successful, it returns the JSON-parsed response, which includes the validation result.

Step 7: Testing Your Function

After deploying, you can test your serverless function by sending a POST request with a CSV file of email addresses to your Cloudflare Worker's URL. Use tools like Fusion or cURL for this purpose.
Testing function using Fusion API Client
Testing function using Fusion API Client

Step 8: Deploy your function to Cloudflare Workers.

If you did not deploy your Worker during Step 1 of setting up your project, deploy your function via this command.
1
npx wrangler deploy

That's it! 👏

You have successfully created and tested the email validation worker. Now it's time to celebrate with a drink. Cheers 🥂

References:

For detailed documentation on Cloudflare Workers and ApyHub's API, visit their official websites.
The complete code and additional resources for this project are available in this GitHub repository.