Email Domain MX Validation: Fly + Fastify + Redis

Hey peeps, just thought I would share a little endpoint I deployed on Fly today to help us with email MX validation for our online checkout app Better Cart (https://getbettercart.com)

Small apps and endpoints like this are absolutely perfect on Fly.io. We love knowing that this high demand endpoint is not only going to auto scale when needed, but also scale where it is needed, as this endpoint is being used by our client side apps so scaling near the customers is a huge win.

Let me know if you have any questions, happy to chat!

2 Likes

This is :100: awesome!

2 Likes

This is pretty :fire:.

I’d been experimenting with the Mailgun email validation API but it would be nice to use our own stuff.

Thanks!

We took a look around the web and couldn’t find anything with reasonable usage plans, nor did we even want to pay for something as simple as a MX lookup haha :slight_smile:

We now use this inside our <Formik> forms as a custom validator paired with Yup as well, super clean and simple to implement.

export const validate = email => {
  return new Promise(async (resolve, reject) => {
    try {
      const response = await fetch('FLY_MX_VALIDATE_ENDPOINT', {
        method: 'POST',
        mode: 'cors',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          email,
        }),
      })
      const data = await response.json()
      resolve(data)
    } catch (error) {
      reject(error)
    }
  })
}

const CheckoutSchema = Yup.object().shape({
    email: Yup.string()
      .email('Email not valid')
      .required('Email is required')
      .test('email-mx', 'Invalid email domain', async value => {
        const response = await validate(value)
        return response.valid
      }),
})

return (
    <Formik
      initialValues={{
        email: checkout.email || '',
      }}
      validationSchema={CheckoutSchema}
    />
  )