Skip to main content

Overview

The server-side useLettermint() function returns the initialized Lettermint SDK instance, giving you direct access to the full Lettermint API. This is useful when you need features beyond the simplified sendEmail utility.
This function can only be used in server contexts such as API routes, server middleware, or Nitro plugins. For client-side use, see the client-side useLettermint composable.

Signature

function useLettermint(): Lettermint

Return Value

return
Lettermint
required
Returns a singleton instance of the Lettermint SDK client, configured with your API key.

Configuration

The Lettermint instance is automatically configured with:
  • API Token: Read from NUXT_LETTERMINT_API_KEY environment variable or the module configuration
  • Singleton Pattern: The instance is created once and reused across all server contexts
The function will throw an error if no API key is configured:
Error: Lettermint API key is not configured. Please set NUXT_LETTERMINT_API_KEY environment variable or configure it in nuxt.config.ts

Usage Examples

// server/api/send-email.post.ts
export default defineEventHandler(async (event) => {
  const lettermint = useLettermint()
  
  const result = await lettermint.email
    .from('[email protected]')
    .to('[email protected]')
    .subject('Welcome!')
    .html('<h1>Welcome to our service!</h1>')
    .tag('welcome')
    .metadata({ userId: '12345' })
    .send()
  
  return result
})

Fluent Email Builder API

The Lettermint SDK uses a fluent (chainable) API for building emails:

Basic Methods

from
(email: string) => EmailBuilder
Set the sender email address.
to
(email: string) => EmailBuilder
Add a recipient. Can be called multiple times to add multiple recipients.
subject
(text: string) => EmailBuilder
Set the email subject.
.subject('Welcome!')
text
(content: string) => EmailBuilder
Set the plain text body.
.text('Plain text content')
html
(content: string) => EmailBuilder
Set the HTML body.
.html('<h1>HTML content</h1>')

Additional Recipients

cc
(email: string) => EmailBuilder
Add a CC recipient. Can be called multiple times.
bcc
(email: string) => EmailBuilder
Add a BCC recipient. Can be called multiple times.
replyTo
(email: string) => EmailBuilder
Add a reply-to address. Can be called multiple times.
.replyTo('[email protected]')

Metadata and Organization

tag
(tag: string) => EmailBuilder
Add a tag for categorization. Can be called multiple times.
.tag('welcome')
.tag('onboarding')
metadata
(data: Record<string, string>) => EmailBuilder
Attach custom metadata for tracking.
.metadata({ userId: '12345', campaign: 'summer' })
headers
(headers: Record<string, string>) => EmailBuilder
Set custom email headers.
.headers({ 'X-Custom-Header': 'value' })

Attachments

attach
(filename: string, content: string) => EmailBuilder
Add a file attachment. Content should be a base64-encoded string.
.attach('document.pdf', base64String)

Sending

send
() => Promise<any>
Send the email and return the result.
const result = await email.send()

Implementation Details

The useLettermint() function:
import { Lettermint } from 'lettermint'
import { useRuntimeConfig } from '#imports'

let lettermintInstance: Lettermint | null = null

export function useLettermint(): Lettermint {
  if (!lettermintInstance) {
    const config = useRuntimeConfig()

    if (!config.lettermint?.apiKey) {
      throw new Error('Lettermint API key is not configured. Please set NUXT_LETTERMINT_API_KEY environment variable or configure it in nuxt.config.ts')
    }

    lettermintInstance = new Lettermint({
      apiToken: config.lettermint.apiKey,
    })
  }

  return lettermintInstance
}

Key Features:

  1. Singleton Pattern: Creates the instance only once and reuses it
  2. Lazy Initialization: Instance is created on first use
  3. Configuration Validation: Throws an error if API key is missing
  4. Runtime Config: Reads API key from Nuxt runtime configuration

When to Use

Use the direct SDK instance when you need:
  • Fluent API: Prefer the chainable builder pattern
  • Fine-grained control: Need access to advanced SDK features
  • Dynamic email construction: Building emails conditionally with complex logic
  • SDK-specific features: Features not exposed by the sendEmail utility
Use the sendEmail utility when you:
  • Want a simpler, object-based API
  • Don’t need advanced SDK features
  • Prefer a more straightforward function call

Build docs developers (and LLMs) love