Integrations · Sign-up and auth

Stop bad emails at Stytch sign-up

Keep sign-ups with bad email addresses out of an app that uses Stytch. Your server checks the address with the API before it calls Stytch. When the result is a status you chose to stop (by default invalid and disposable), Stytch is not called and no user is created. If the API is slow or down, the sign-up goes through.

It is one TypeScript file with no dependencies, under the MIT licence (the text is at the top of the file). Download it: email-validation-stytch.ts. It runs in the server that calls Stytch's backend API. Node 22.18 or later imports the .ts file as it is; with an older Node or a build step, compile it with your app.

How it works, and its limits

Stytch has no hook that runs before it creates a user, and its webhooks arrive after the fact. So the check runs in your server, before your Stytch call:

  • Stopped before the user exists: sign-ups your server sends to Stytch. That covers email magic links, email one-time codes, passwords, user creation and invites (Consumer), and discovery, organization sign-up, invites and member creation (B2B).
  • Not stopped: OAuth sign-ups, Consumer and B2B (your server learns the address only after Stytch created the user or member; in B2B OAuth discovery the address is known before an organization is created or joined, but the check does not cover that step), SSO and SCIM members, users added in the Stytch Dashboard, and sign-ups that Stytch's browser-side components send straight from the browser. For these, an optional webhook handler checks the new address and tells your code. It does not block, delete or change anything in Stytch.

Stytch's log-in-or-sign-up endpoints serve log-ins too. Tell the check which addresses already have an account, and log-ins go through unchecked and cost nothing.

Set it up

This is code for your app's server, added by whoever builds your app: there is no browser-only option. You can read the file in your browser before you add it.

  1. Create an API key on the API keys page with the permission Validate addresses and a daily credit cap. Put it in your server's environment as EV_API_KEY.

  2. Copy email-validation-stytch.ts into your server code, for example to lib/, and create one guard:

    import { createStytchSignupGuard, SignupRefusedError } from './email-validation-stytch.ts';
    
    export const signupGuard = createStytchSignupGuard({
      apiKey: process.env.EV_API_KEY,
      isExistingUser: async (email) => (await db.users.findByEmail(email)) != null,
    });
  3. Wrap each Stytch call that can create a user, for example an Express route:

    app.post('/signup', async (req, res) => {
      try {
        await signupGuard.protect(req.body.email, () =>
          stytch.magicLinks.email.loginOrCreate({ email: req.body.email }),
        );
        res.json({ sent: true });
      } catch (err) {
        if (err instanceof SignupRefusedError) {
          const hint = err.decision.didYouMean ? ` Did you mean ${err.decision.didYouMean}?` : '';
          return res.status(422).json({ error: err.message + hint });
        }
        throw err; // Stytch's own errors pass through unchanged
      }
    });
  4. Send every sign-up through your server. Sign-ups that Stytch's browser-side components make straight from the browser skip the check.

  5. Test it: sign up through your form with disposable@sandbox.email-validation.org, a test address. It is refused before Stytch is called, for free. Never sign up with a test address the check lets through, such as accepted@sandbox.email-validation.org: Stytch would send its magic link or code to a domain that receives no mail, and the bounce hurts your sender. To see a sign-up go through, use an address of yours that receives mail.

The webhook notice (optional)

For the sign-ups the guard cannot see, add a route that receives Stytch's webhooks. It checks each new user or member and calls your onResult; it reports them and changes nothing in Stytch. The signature covers the raw body, so the route must get it unparsed, before any express.json():

import { createStytchWebhookNotice } from './email-validation-stytch.ts';

const handle = createStytchWebhookNotice({
  apiKey: process.env.EV_API_KEY,
  signingSecret: process.env.STYTCH_WEBHOOK_SECRET,
  onResult: async ({ id, outcome }) => {
    if (outcome === 'on_stop_list') {
      // for example, mark the account for review in your own database
    }
  },
});
app.post('/api/webhooks/stytch', express.raw({ type: 'application/json' }), async (req, res) => {
  const result = await handle({ body: req.body.toString('utf8'), headers: req.headers });
  res.status(result.status).json(result.body);
});

In the Stytch Dashboard, open Webhooks, add an endpoint with your route's URL and the user or member create events, and copy its signing secret into STYTCH_WEBHOOK_SECRET. A delivery whose signature does not match, or whose timestamp is more than 5 minutes off, is answered 400 and not checked. Every other delivery is answered 200.

Options

OptionDefaultWhat it does
stop['invalid', 'disposable']Statuses that stop a sign-up: any of invalid, disposable, catch_all, unknown
regionuseu validates addresses in the EU and stores their results there (EU data residency)
timeoutMs6000How long to wait before the sign-up goes through unchecked: whole milliseconds from 1 to 60000; any other value throws a TypeError when the guard or notice is created
isExistingUsernoneAddresses that already have an account go through unchecked
messagePlease sign up with another email address.What the refusal says

A result marked disposable is stopped while disposable is in stop, whatever its status (the API keeps invalid or unknown as the status of some addresses on disposable domains), except an unknown that means our check ran out of time or failed (timeout, internal_error). Otherwise a valid result goes through, and an unknown result is stopped only if you add it to stop, never for timeout or internal_error.

When something fails

No key, a timeout, a network error, a rate limit, a refused key, no credits or any other error answer lets the sign-up through. The handler never resends a check.

What it costs

Each check costs at most 1 credit, like any validation request. unknown results, errors, malformed addresses and test addresses are free. Using both the guard and the webhook notice checks a sign-up twice. Credits come in one-time packs, from $0.15 per 1,000 with the 50,000,000-credit pack (smaller packs cost more per credit), and new accounts get 500 free credits (100 when you sign up with a free email provider such as Gmail, Outlook or Yahoo, another personal mailbox such as a paid one like Fastmail or one from your internet provider, or an address from a relay or alias service). Anyone can call your sign-up route, so rate-limit it and give the key a daily credit cap.

Privacy

The file sends only the address, in the body of a POST to https://api.email-validation.org (or https://api-eu.email-validation.org). It never puts the address in a URL and sends it to no other host, and it never sees your Stytch project secret. It writes nothing to the console, and onResult gets the outcome and the result, never the address. The refusal error carries the suggested correction for you to show the person; do not log it whole.

More on Stytch

Updated · Maintained by: Email Validation LLC

Need help? Contact support, or ask the help chat. Something wrong or missing on this page? Tell us