Integrations · Frameworks and SDKs

Validate emails in NestJS

A NestJS module that checks the email address of a request before your route's handler runs. It refuses addresses that cannot receive email and throwaway inboxes, hands the result to your handler, and by default never blocks your sign-ups when our API cannot answer.

@Post('signup')
@ValidateEmail()
signup(@EmailOutcome() outcome: EmailValidationOutcome) { /* … */ }

Install

The package is not on npm yet. Download it here and install it with npm:

  1. Download email-validation-nestjs-1.0.0.tgz (MIT licence) into your NestJS app.

  2. Install it:

    npm install ./email-validation-nestjs-1.0.0.tgz

    To keep it in your dependencies, commit the file to your repository: npm records its path in package.json.

  3. Create an API key on the API keys page (the default permission "Validate addresses" is enough), and give it a daily credit cap to limit what a busy route can spend. Set it in your environment as EV_API_KEY; never put it in code.

It needs NestJS 12 or 11 on its Express platform (tested; the Fastify platform is not) and Node.js 22.12 or newer. The package has no dependencies besides NestJS itself and loads as ESM and as CommonJS, with TypeScript types.

Use it

  1. Import the module:

    import { Module } from '@nestjs/common';
    import { EmailValidationModule } from '@email-validation/nestjs';
    import { SignupController } from './signup.controller.js';
    
    @Module({
      imports: [EmailValidationModule.forRoot({ apiKey: process.env.EV_API_KEY })],
      controllers: [SignupController],
    })
    export class AppModule {}

    With @nestjs/config, use forRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (config) => ({ apiKey: config.get('EV_API_KEY') }) }).

  2. Put @ValidateEmail() on the routes where people enter an address:

    import { Controller, Post } from '@nestjs/common';
    import { EmailOutcome, type EmailValidationOutcome, ValidateEmail } from '@email-validation/nestjs';
    
    @Controller()
    export class SignupController {
      @Post('signup')
      @ValidateEmail()
      signup(@EmailOutcome() outcome: EmailValidationOutcome) {
        return { created: true, status: outcome.result?.status ?? null };
      }
    }
  3. Try it with the free test addresses: accepted@sandbox.email-validation.org reaches your handler, no_mailbox@sandbox.email-validation.org and disposable@sandbox.email-validation.org get a 422 answer:

    {"error":{"code":"email_invalid","message":"This address cannot receive email. Please check it for typos.","status":"invalid","did_you_mean":null}}

    On a route that mails the address (a sign-up, a welcome), try only the ones that are refused: one that passes reaches your code, and its mail would bounce.

Other ways to use it

  • Middleware: consumer.apply(EmailValidationMiddleware).forRoutes({ path: 'signup', method: RequestMethod.POST }) in a module's configure().
  • Forms with a file upload (multipart/form-data) are parsed after the guard and middleware: check the address in the handler with the service, as below.
  • In your own code: inject EmailValidationService and call check(email); throw service.exceptionFor(outcome) refuses the way the guard does.
  • Every route: { provide: APP_GUARD, useClass: EmailValidationGuard }; routes without an address go on unchecked.

Settings

  • Which statuses are refused. invalid and disposable by default. Choose your own list from our five statuses (valid, invalid, catch_all, unknown, disposable), for the whole app in forRoot() or for one route: @ValidateEmail({ stopStatuses: ['invalid', 'disposable', 'catch_all'] }). catch_all and unknown addresses usually belong to real people, so they pass unless you list them: see what each status means.
  • When our API cannot answer (a timeout, 8 seconds by default, a network error, a rate limit, an empty balance or any other error), the request goes on, and NestJS's logger gets one line with our error code and request id. Your handler can see why in the outcome's notChecked. Nothing is retried. Set failOpen: false to answer 503 instead.
  • Another field or place: @ValidateEmail({ field: 'contact_email' }), or getEmail: (req) => req.query.email (it may be async).
  • Your own refusal: catch EmailValidationException with an exception filter; statusCode and messages change the default answer.
  • No address in the request: by default your own validation decides (missing: 'refuse' answers 422). A value that is not an address (an array, an object, no @) is refused with 422 by default (malformed: 'next' lets it through to your own validation).
  • EU data residency: baseUrl: EU_BASE_URL (or EV_BASE_URL=https://api-eu.email-validation.org); the same key works on both.

The address and the key are never logged.

Costs

One POST /v1/validate per checked request. Each completed check costs one credit; unknown results, malformed addresses and the test addresses are free. See credits and billing.

More on NestJS

Updated · Maintained by: Email Validation LLC

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