---
title: "Building a DNS Email Validation Service with Cloudflare Workers & ApyHub API"
url: https://apyhub.com/blog/dns-email-validation-service-with-cloudflare-workers-and-apyhub-api
author: Sohail Pathan
published: 2024-02-14T00:00:00Z
tags: [tutorials]
---

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

# Build a DNS Email Validation Service with Cloudflare Workers & ApyHub API


## Introduction

**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](https://workers.cloudflare.com/) and [ApyHub's DNS Email Validation API](https://apyhub.com/utility/validator-dns-email)

[![DNS-Email-Validation.png](https://i.postimg.cc/zfvSCRP3/DNS-Email-Validation.png)](https://apyhub.com/blog/email-validation-api)

**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](https://www.cloudflare.com/en-gb/) Workers.
4. An API key from ApyHub after signing up for their [DNS Email Validation API](https://apyhub.com/utility/validator-dns-email)

Want to test the API beforehand?
[![Chrome-extension.jpg](https://i.postimg.cc/FsF2MTY6/Chrome-extension.jpg)](https://apyhub.com/utility/validator-dns-email)

## PART 1: Setting Up Your Project

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

```bash
cloudflare-email-validator
cd cloudflare-email-validator
```

### Step 2: Install the Wrangler package

To install the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) package in the directory, run the following command in the terminal:

```bash
npm install wrangler --save-dev
```

### Step 3:  Create a new Worker project

To start creating your Worker project, run the following command:

```bash
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.

```tsx
export default {

	async fetch(request: Request, env: any, ctx: any): Promise<Response> {
		if (request.method !== 'POST') {
            return new Response('Expected POST',{ status: 405 });
   }
```

### 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.

```tsx
const formData = await request.formData();
const file = formData.get('file');
if (!file || !(file instanceof File)) {
    return new Response('No file uploaded', { status: 400 });
}

// The content of the file is read as text, and each line is split into individual email addresses. 

// Empty lines are filtered out.

const text = await file.text();
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.



```tsx
for (let i = 0; i < emails.length; i++) {
            const email = emails[i];
            const result : any = await this.validateEmail(email);
            if (result.data) {
                validEmails.push(email);
            } else {
                invalidEmails.push(email);
            }
            // Wait for 1 second before processing the next email
            await new Promise(resolve => setTimeout(resolve, 1000))
}
```

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.

```tsx
let csvContent = 'Emails,Invalid Emails\n';
emails.forEach((email, index) => {
    csvContent += `${email},${invalidEmails[index]}\n`;
});

//`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.`

return new Response(csvContent.replaceAll('undefined', ''), {
    headers: {
        'Content-Type': 'text/csv',
        'Content-Disposition': 'attachment; filename="email_validation_result.csv"'
    }
});
```

### 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.

```tsx
const apiResponse = await fetch('https://api.apyhub.com/validate/email/dns', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'apy-token': 'YOUR_SECRET_API_KEY' //provide your secret-token-here
    },
    body: JSON.stringify({email})
});
```

*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 [Voiden](https://voiden.md/) or `cURL` for this purpose.

### 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.

```bash
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](https://github.com/iamspathan/Email-Validation-Worker).


## FAQs


**1. What is DNS email validation and why use it?**
DNS email validation checks if email addresses are real and deliverable by verifying DNS records, reducing bounces and improving data accuracy.

**2. How does Cloudflare Workers help in email validation?**
Cloudflare Workers allows you to run serverless functions that process large email lists, validate them via APIs, and return results efficiently.

**3. How do I integrate ApyHub’s DNS Email Validation API?**
Send POST requests to ApyHub’s API with email addresses, including your API key in headers, and process the response to categorize valid and invalid emails.

**4. Can I validate large CSV email lists using this setup?**
Yes. The tutorial demonstrates batch processing and CSV parsing to validate emails at scale and generate a new output CSV with results.

**5. What are the prerequisites for building this service?**
You need Node.js, a standard IDE (like VSCode), a Cloudflare Workers account, and an ApyHub API key for DNS Email Validation.

**6. How do I deploy the email validation service?**
After coding and testing your function locally, deploy it to Cloudflare Workers using npx wrangler deploy to make it accessible via a serverless URL.
