How to check an email address in Cloudflare Pages

Cloudflare Pages is a cloud platform that enables developers to build, deploy, and scale websites and web applications with ease; it leverages Cloudflare's global network infrastructure to ensure fast and reliable content delivery, enhanced security, and seamless scalability. At its core, Cloudflare Pages simplifies the web development process by providing a streamlined workflow for developers.

One of the standout features of Cloudflare Pages is Pages Functions, which allow developers to run serverless functions alongside their static site or application, without the need to manage infrastructure: these functions can be written in JavaScript and are executed at the edge of Cloudflare's network, ensuring low latency and optimal performance for end users, and can be used to offload complex logic, handle dynamic content generation, and integrate with third-party services - all without the overhead of managing servers, empowering developers to build highly dynamic and interactive web experiences while maintaining simplicity and scalability.

In this article, we'll show how to block invalid emails and typos in Cloudflare Pages using the Verifalia email checker, which can determine both the existence of an email address and its ability to receive emails: by doing so, you can minimize bounce rates, block disposable / temporary email addresses, and guarantee that your contact forms or signup forms only accept deliverable emails in Cloudflare Pages.

How to verify email addresses in Cloudflare Pages

Verifalia offers a free and open-source SDK for JavaScript, enabling quick and easy email address verification within any Node.js application; the same SDK also allows integration with front-end technologies such as Angular, React, Vue, and vanilla JavaScript scripts.

To validate email addresses in a Cloudflare Pages Function, start by adding the Verifalia NPM package as a dependency of your project:

npm install --save verifalia

After installation, you can directly call the Verifalia API from any Cloudflare Pages Function using the VerifaliaRestClient class.

Since the Cloudflare Workers runtime is based on the V8 JavaScript engine, importing this class into a Pages Function requires specifying the path of the exposing ES module precisely, as shown below:

import { VerifaliaRestClient } from 'verifalia/browser/esm/index.js';

Next, the Pages Function needs to instantiate the VerifaliaRestClient class, passing the authentication credentials and any other settings; while the Verifalia API supports multiple authentication methods including client certificates and bearer tokens, here is how to use basic authentication:

const verifalia = new VerifaliaRestClient({
    username: 'YOUR-USERNAME-HERE',
    password: 'YOUR-PASSWORD-HERE'
});

Authentication is performed by way of either the credentials of your root Verifalia account or of one of its users: if you don't have a Verifalia account, just register for free. For security reasons, it is always advisable to create and use a dedicated user for accessing the API, as doing so will allow to assign only the specific needed permissions to it.

From now on, the Pages Function can invoke any of the methods exposed by the VerifaliaRestClient class, including the emailValidations.submit() function which submits the passed email address to the Verifalia API to check if a given email address exists.

const result = await verifalia
    .emailValidations
    .submit('batman@gmail.com');

Here is the complete code sample for validating an email address in a Cloudflare Pages Function:

import { VerifaliaRestClient } from 'verifalia/browser/esm/index.js';

export async function onRequest(context) {
    const verifalia = new VerifaliaRestClient({
        username: 'YOUR-USERNAME-HERE',
        password: 'YOUR-PASSWORD-HERE'
    });

    const result = await verifalia
        .emailValidations
        .submit('batman@gmail.com');

    // const classification = result.entries[0].classification;
    // const status = result.entries[0].status;

    return new Response('Result: ' + JSON.stringify(result))
}

The Verifalia SDK for JavaScript provides several additional features and classes, enabling you to customize settings for your email verifications, including data retention, desired quality level, and much more. To explore further and access additional code samples, please view the reference documentation.

Need Help?

We're here to assist you.

Visit Our Help Center
Explore our collection of technical and non-technical articles about Verifalia's services.
Send Us a Message
Reach out to us with any questions or comments. Support is always free of charge.

Want to chat?
Click the button below to chat live with one of our support team right now.