The unofficial Resend SDK for NestJS, providing a type-safe and dependency injection friendly way to interact with Resend's email service.
npm install @mnmadhukar/resend-nestjs
# or
yarn add @mnmadhukar/resend-nestjs
# or
pnpm add @mnmadhukar/resend-nestjs
import { Module } from '@nestjs/common';
import { ResendModule } from '@mnmadhukar/resend-nestjs';
@Module({
imports: [
ResendModule.forRoot({
apiKey: 'your_api_key_here',
}),
],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { EmailsService } from '@mnmadhukar/resend-nestjs';
@Injectable()
export class YourService {
constructor(private readonly emailsService: EmailsService) {}
async sendEmail() {
const result = await this.emailsService.send({
from: 'you@example.com',
to: 'recipient@example.com',
subject: 'Hello from Resend',
html: '<p>Hello world!</p>',
});
return result;
}
}
import { Injectable } from '@nestjs/common';
import { ApiKeysService } from '@mnmadhukar/resend-nestjs';
@Injectable()
export class YourService {
constructor(private readonly apiKeysService: ApiKeysService) {}
async createApiKey() {
const result = await this.apiKeysService.create({
name: 'My API Key',
permission: 'sending_access',
});
return result;
}
}
import { Injectable } from '@nestjs/common';
import { AudiencesService } from '@mnmadhukar/resend-nestjs';
@Injectable()
export class YourService {
constructor(private readonly audiencesService: AudiencesService) {}
async createAudience() {
const result = await this.audiencesService.create({
name: 'Newsletter Subscribers',
});
return result;
}
}
import { Injectable } from '@nestjs/common';
import { ContactsService } from '@mnmadhukar/resend-nestjs';
@Injectable()
export class YourService {
constructor(private readonly contactsService: ContactsService) {}
async createContact() {
const result = await this.contactsService.create({
email: 'user@example.com',
firstName: 'John',
lastName: 'Doe',
unsubscribed: false,
});
return result;
}
}
import { Injectable } from '@nestjs/common';
import { DomainsService } from '@mnmadhukar/resend-nestjs';
@Injectable()
export class YourService {
constructor(private readonly domainsService: DomainsService) {}
async createDomain() {
const result = await this.domainsService.create({
name: 'example.com',
});
return result;
}
}
- π Type-safe: Full TypeScript support with detailed type definitions
- π NestJS native: Built with dependency injection in mind
- π¨ Complete API coverage: All Resend API endpoints supported
- π Idempotency: Support for idempotent requests
- β‘ Async configuration: Support for async module configuration
- π§ͺ Testable: Built with testing in mind
- π Error handling: Comprehensive error handling and type definitions
EmailsService
: Send transactional emailsApiKeysService
: Manage API keysAudiencesService
: Manage email audiencesContactsService
: Manage contacts within audiencesDomainsService
: Manage email sending domainsBatchService
: Send batch emails efficiently
import { Module } from '@nestjs/common';
import { ResendModule } from '@mnmadhukar/resend-nestjs';
@Module({
imports: [
ResendModule.forRoot({
apiKey: 'your_api_key_here',
}),
],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { ResendModule } from '@mnmadhukar/resend-nestjs';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot(),
ResendModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
apiKey: configService.get('RESEND_API_KEY'),
}),
}),
],
})
export class AppModule {}
All services return responses in a consistent format:
interface Response<T> {
data: T | null;
error: {
message: string;
name: string;
statusCode: number;
} | null;
}
Example error handling:
try {
const { data, error } = await emailsService.send({
from: 'you@example.com',
to: 'recipient@example.com',
subject: 'Hello',
html: '<p>World</p>',
});
if (error) {
console.error('Failed to send email:', error.message);
return;
}
console.log('Email sent successfully:', data.id);
} catch (e) {
console.error('Unexpected error:', e);
}
# Install dependencies
npm install
# Build
npm run build
# Run tests
npm run test
# Run tests with coverage
npm run test:cov
# Lint code
npm run lint
# Format code
npm run format
We welcome contributions! Please feel free to submit a Pull Request.
MIT License