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:
Download email-validation-nestjs-1.0.0.tgz (MIT licence) into your NestJS app.
Install it:
npm install ./email-validation-nestjs-1.0.0.tgzTo keep it in your dependencies, commit the file to your repository: npm records its path in
package.json.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
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, useforRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (config) => ({ apiKey: config.get('EV_API_KEY') }) }).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 }; } }Try it with the free test addresses:
accepted@sandbox.email-validation.orgreaches your handler,no_mailbox@sandbox.email-validation.organddisposable@sandbox.email-validation.orgget 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'sconfigure(). - 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
EmailValidationServiceand callcheck(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.
invalidanddisposableby default. Choose your own list from our five statuses (valid,invalid,catch_all,unknown,disposable), for the whole app inforRoot()or for one route:@ValidateEmail({ stopStatuses: ['invalid', 'disposable', 'catch_all'] }).catch_allandunknownaddresses 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. SetfailOpen: falseto answer 503 instead. - Another field or place:
@ValidateEmail({ field: 'contact_email' }), orgetEmail: (req) => req.query.email(it may be async). - Your own refusal: catch
EmailValidationExceptionwith an exception filter;statusCodeandmessageschange 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(orEV_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.
Related
- Validating sign-up forms: what to block and what to accept
- The JavaScript client library for other calls, such as bulk lists