Skip to main content
The TypeScript SDK wraps Torpedo’s REST API for server-side JavaScript and TypeScript apps. It supports transactional email, email batches, SMS, and TypeScript-only email templates powered by React Email. Other languages should continue using the REST API examples until dedicated SDKs are available.

Install

pnpm add torpedo-sdk

Create a client

import { Torpedo } from 'torpedo-sdk'

const torpedo = new Torpedo({
  apiKey: process.env.TORPEDO_API_KEY!,
})

Send raw HTML email

await torpedo.emails.send({
  from: 'Acme <hello@myapp.com>',
  to: 'user@example.com',
  subject: 'Welcome to Acme',
  html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
})

Send a template email

Template support is specific to the TypeScript SDK. The SDK renders the template to html, generates plain text, and sends the existing Torpedo email API request. React Email support is optional. Install it only when you want to send React templates:
pnpm add react react-dom @react-email/components @react-email/render
import { Body, Html, Text } from '@react-email/components'
import { Torpedo } from 'torpedo-sdk'

function WelcomeEmail({ name }: { name: string }) {
  return (
    <Html>
      <Body>
        <Text>Welcome {name}</Text>
      </Body>
    </Html>
  )
}

const torpedo = new Torpedo({
  apiKey: process.env.TORPEDO_API_KEY!,
})

await torpedo.emails.send({
  from: 'Acme <hello@myapp.com>',
  to: 'user@example.com',
  subject: 'Welcome to Acme',
  template: <WelcomeEmail name="Eliot" />,
})
Provide text if you want full control over the plain-text part:
await torpedo.emails.send({
  from: 'Acme <hello@myapp.com>',
  to: 'user@example.com',
  subject: 'Welcome to Acme',
  template: <WelcomeEmail name="Eliot" />,
  text: 'Welcome Eliot',
})

Send a batch

Batch sends can mix raw HTML and templates:
await torpedo.emails.sendBatch({
  emails: [
    {
      from: 'Acme <hello@myapp.com>',
      to: 'one@example.com',
      subject: 'Welcome',
      html: '<h1>Welcome!</h1>',
    },
    {
      from: 'Acme <hello@myapp.com>',
      to: 'two@example.com',
      subject: 'Welcome',
      template: <WelcomeEmail name="Eliot" />,
    },
  ],
})
Batch requests do not support idempotency keys.

Send SMS

await torpedo.sms.send({
  to: '+258841234567',
  body: 'Your verification code is 482910.',
})
The SDK automatically adds an idempotency key for each write request so SDK retries are safe.

Read messages

const emails = await torpedo.emails.list({ limit: 20 })
const email = await torpedo.emails.get(emails.data[0].id)

const smsMessages = await torpedo.sms.list({ limit: 20 })
const sms = await torpedo.sms.get(smsMessages.data[0].id)