Skip to main content

Create The Client

import { createBrewClient } from '@brew.new/sdk'

const brew = createBrewClient({
  apiKey: process.env.BREW_API_KEY!,
})

1. Upsert A Contact

const contactResult = await brew.contacts.upsert({
  email: 'john@example.com',
  firstName: 'John',
  lastName: 'Doe',
  subscribed: true,
  customFields: {
    plan: 'enterprise',
  },
})

console.log(contactResult.contact.email)
console.log(contactResult.created)

2. List Templates Or Brands

const { templates } = await brew.templates.list({
  brand: 'vercel.com',
})

const { brands } = await brew.brands.list()
Use templates when you want public reference content. Use brands when you want your own brand design context.

3. Generate An Email

const generated = await brew.emails.generate({
  prompt: 'Create a welcome email for new subscribers',
  referenceEmailId: templates[0]?.emailId,
})

if ('emailId' in generated) {
  console.log(generated.emailId)
  console.log(generated.emailHtml)
} else {
  console.log(generated.response)
}

4. List Verified Domains

const { domains } = await brew.domains.list()

if (domains.length === 0) {
  throw new Error('You need a verified sending domain before sending.')
}

5. Start A Send

Use one recipient mode.
  • audienceId
  • emails
Manual email mode example:
if ('emailId' in generated) {
  const sendResult = await brew.sends.create({
    emailId: generated.emailId,
    domainId: domains[0]!.domainId,
    subject: 'Welcome to Brew',
    emails: ['john@example.com'],
  })

  console.log(sendResult.status)
  console.log(sendResult.runId)
}
Scheduled send example:
await brew.sends.create({
  emailId: 'email_123',
  domainId: 'domain_123',
  subject: 'Launch update',
  emails: ['john@example.com'],
  scheduledAt: '2099-01-01T00:00:00.000Z',
})

Handle Errors

import { BrewApiError } from '@brew.new/sdk'

try {
  await brew.domains.list()
} catch (error) {
  if (error instanceof BrewApiError) {
    console.error(error.code, error.message, error.requestId)
  }
  throw error
}

Next Steps

Resource Surface

See the current TypeScript SDK resources and methods.

API Reference

See the raw HTTP contract behind the SDK.

Need Help?

Our team is ready to support you at every step of your journey with Brew. Choose the option that works best for you:

Search Documentation

Type in the “Ask any question” search bar at the top left to instantly find relevant documentation pages.

ChatGPT/Claude Integration

Click “Open in ChatGPT” at the top right of any page to analyze documentation with ChatGPT or Claude for deeper insights.