Advanced Email Validation using ApyHub’s API in Go
Tags: apyhub, emailvalidation, api, regex
Published: 2023-06-19 12:43:46

Email validation is a crucial aspect of ensuring data accuracy and security, but relying solely on regular expression (regex) patterns for validation is insufficient. While regex can check the syntax of an email address and accept it if it matches the pattern, it does not guarantee the authenticity or validity of the email. This limitation opens the door for fake or disposable email addresses to enter the database, posing a vulnerability to your application. Additionally, considering these fake addresses as active users adds no value.

To address this problem, I have discovered a free online service provided by Apyhub that effectively solves the issue of email verification, providing a more reliable and secure solution.

[ApyHub’s Email Validation API](https://apyhub.com/utility/validator-dns-email) is a cloud-based API service that allows developers to easily integrate email validation functionality into their applications. This API is designed to validate email addresses in real time, providing accurate and reliable results that can help organisations reduce email bounce rates, prevent fraud, and improve their email marketing efforts.

## How does it work?

## Quick tutorial

In this tutorial, we will be going to build an authentication app using Mongoose for storing emails in a database. While the user signup, we will check whether the email is valid or not.

## **Prerequisites**

In this tutorial, we will be going to build an authentication app using Mongoose for storing emails in a database. While the user signup, we will check whether the email is valid or not.

We'll need access to:

- [Go 1.1 and Above](https://go.dev/doc/install)
- [An ApyHub account](https://apyhub.com/auth/signup)
- [Git CLI](https://git-scm.com/downloads)

1. To get started with ApyHub you first need to [register for a free account](https://apyhub.com/auth/signup) to get an API Key (apy-token). You can use Google Authentication or GitHub Authentication as well.

[![image2.png](https://i.postimg.cc/WbNdvzhK/image2.png)](https://postimg.cc/4n0fvJvb)

Once your signup is successful, ApyHub creates a default application with APY-TOKEN. You can find the token by navigating to **Dashboard →  API Keys → Default Application** 
Eg. **APY*************************************************CZR**

Once the API token is generated, We will check our [DNS Email API](https://apyhub.com/utility/validator-dns-email) documentatioan for integration.

[![Login-Screen.png](https://i.postimg.cc/439d5hSK/Login-Screen.png)](https://postimg.cc/f3MDWkZZ)

2. Clone the project from the repository:

```bash
git clone https://github.com/iamspathan/apyhub-email-validation-tutorial-go
```

Or you can download the project from [here](https://github.com/iamspathan/apyhub-email-validation-tutorial.git).

3. Add your Secret APY-TOKEN in main.go

```go
func validateEmail(email string) (bool, error) {
	url := "https://api.apyhub.com/validate/email/dns"

	payload := struct {
		Email string `json:"email"`
	}{
		Email: email,
	}

	jsonPayload, err := json.Marshal(payload)
	if err != nil {
		return false, err
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
	if err != nil {
		return false, err
	}

	if err != nil {
		return false, err
	}
	// Add your apy-token here.
	
	req.Header.Add("apy-token", "*********** ADD YOUR SECRET APY TOKEN HERE **************")
	req.Header.Add("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return false, err
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return false, err
	}

	var response struct {
		Valid bool `json:"data"`
	}
	err = json.Unmarshal(body, &response)
	if err != nil {
		return false, err
	}

	return response.Valid, nil
}
```

4. Now, We can start the server for making API calls. Use this command in terminal to start the server.

```bash
go run main.go
```

*Tip: The **`go run`** command is used in Go to compile and run a Go program in a single step. It allows you to quickly execute a Go source file without explicitly building an executable binary.*

5. Finally, Navigate to http://localhost:3000, You will able to see the Signup page.

[![account-signup.png](https://i.postimg.cc/C1jKyBkR/account-signup.png)](https://postimg.cc/wyjzLBWz)

*Note: The authentication module mentioned in this project is provided solely as a recommendation for the project structure. While we highly encourage its use and integration according to your architecture pattern.*

We just saw, how the email validation process will only allow the verified email to create an account. But there are several use cases in which Advanced Email Validation fits.

## What more can you solve with this?

1. **Bulk email verification**: Enterprises may have large email lists that need to be validated for accuracy and deliverability. An email validation API can be used to check the validity of each email address in the list, ensuring that emails are sent only to valid addresses.
2. **Fraud prevention**: Email validation APIs can also be used to detect fraudulent email addresses used in phishing attacks, preventing unauthorised access to enterprise systems.
3. **Marketing campaigns**: Email validation APIs can help enterprises ensure that their marketing emails reach the intended recipients by verifying the email addresses in their lists and removing invalid addresses.
4. **Compliance**: Email validation APIs can help enterprises comply with data protection regulations by ensuring that emails are sent only to users who have provided valid consent.
5. **Customer support:** Email validation APIs can be used to verify the email addresses of customers who submit support requests, ensuring that the enterprise is communicating with the correct customer.

So, if you're looking to improve email deliverability, reduce bounce rates and avoid spam accounts, [sign up for ApyHub's email validation](https://apyhub.com/utility/validator-dns-email) API today.