Sign in to fill these examples with your own keys. Sign in Start free trial

Send emails with NestJS

Import PostShibaModule and call sendMail.

What you'll build

A NestJS service that injects the PostShiba client and sends a welcome email with sendMail.

Before you start

  • An API key with Send only access. Full access also works. See Create an API key. Support keys cannot send.
  • A verified sending domain. See Add and verify a domain. from must be an address on it.
  • A cluster that is ready to send (sending_ready is true). See Clusters.
  • Node.js 18 or later.

Install

The Nest module ships in the Node package.

install.sh
1npm install @postshiba/node

Send your first email

  1. Register the module in AppModule. It provides a PostShiba client on the POSTSHIBA token. teamId is only needed for team-scoped calls such as listing clusters, which need a Full access key kept out of your deployed app.
app.module.ts
1import { Module } from "@nestjs/common" 2import { PostShibaModule } from "@postshiba/node/nest" 3import { MailService } from "./mail.service" 4 5@Module({ 6 imports: [ 7 PostShibaModule.register({ 8 apiKey: "YOUR_API_KEY", 9 teamId: "KjkAJW", 10 }), 11 ], 12 providers: [MailService], 13}) 14export class AppModule {}
  1. Inject POSTSHIBA into a service and call sendMail.
send.ts
1import { Inject, Injectable } from "@nestjs/common" 2import { POSTSHIBA, sendMail } from "@postshiba/node/nest" 3import type { PostShiba } from "@postshiba/node" 4 5@Injectable() 6export class MailService { 7 constructor(@Inject(POSTSHIBA) private readonly postshiba: PostShiba) {} 8 9 sendReceipt() { 10 return sendMail(this.postshiba, { 11 from: "hello@mail.example.com", 12 to: "you@example.com", 13 replyTo: "support@example.com", 14 subject: "Your receipt", 15 text: "Thanks for your order.", 16 html: "<p>Thanks for your order.</p>", 17 uniqueArgs: { order_id: "ord_123" }, 18 }) 19 } 20}

Save it as mail.service.ts. sendMail resolves to the JSON body of the 201 response. The message then shows under Events in the dashboard.

response.json
1{ 2 "queued": true, 3 "message_id": "abc@capsule.test", 4 "from": "hello@mail.example.com", 5 "to": ["you@example.com"], 6 "subject": "Your receipt", 7 "unique_args": {"order_id": "ord_123"} 8}

Pin a cluster

sendMail uses your first cluster that is ready to send. To choose one, call the client directly with clusterId.

pin.ts
1await this.postshiba.emails.send(message, { clusterId: "NmQpXr" })

Attachments and templates

Attach a file

Pass attachments with filename, content_type, and base64 content. sendMail forwards them as they are. Up to 10 files, 18 MB in total. See Attachments for allowed types.

attachment.ts
1import { readFile } from "node:fs/promises" 2 3await sendMail(this.postshiba, { 4 from: "hello@mail.example.com", 5 to: "you@example.com", 6 subject: "Your invoice", 7 text: "Your invoice is attached.", 8 attachments: [ 9 { 10 filename: "invoice.pdf", 11 content_type: "application/pdf", 12 content: (await readFile("invoice.pdf")).toString("base64"), 13 }, 14 ], 15})

Send a published template

sendMail does not take template. Call the client directly with the alias or public id and its variables.

template.ts
1await this.postshiba.emails.send({ 2 from: "hello@mail.example.com", 3 to: ["you@example.com"], 4 template: { id: "welcome", variables: { name: "Ada" } }, 5})

Keep the key safe

Use a Send only key in your app. It can send email and call GET /api/v1/users/me. Every other request returns 403 forbidden.

If this key leaks Send only Full access
Send mail from your verified domains Yes Yes
Read events, recipients, templates, and sent messages No Yes
See SMTP passwords and webhook secrets No Yes
Change clusters, domains, credentials, webhooks, and suppressions No Yes
  • Keep Full access keys for setup scripts, the CLI, and MCP. Don't deploy one with your app.
  • Read the key from an environment variable, such as apiKey: process.env.POSTSHIBA_API_KEY in PostShibaModule.register. Don't commit it or your .env file, log it, or put it in a URL.
  • Send from your Nest server. Never put a key, even a Send only key, in browser or mobile code.
  • Create one key per app and environment. If a key leaks, delete it on API keys and create a new one. Deleting stops it straight away.

Troubleshooting

A response outside 2xx rejects with PostShibaError, exported from @postshiba/node/nest, with error, field, and message.

Status error What to do
401 Invalid token or no user signed in The API key is missing, mistyped, or deleted. Copy it again from API keys.
403 forbidden Support keys cannot send. Use a Send only or Full access key. A Send only key gets this on any call other than sending.
403 domain_unverified from is not on a verified domain.
403 kyc_required Your team is pending production approval. Until then, send only to team members, live inbox addresses, and inbox forward-to addresses.
422 invalid field names the problem, such as a missing to, an unpublished template, or a rejected attachment.
429 throttled The cluster hit its hourly throttle or 24-hour cap. The client does not retry, and retrying right away hits the same cap. See What happens at your cap.
429 rate_limited More than 300 send requests in one minute on one API key. Slow down.

Every other status is on Errors.

Next steps