Skip to main content

Documentation Index

Fetch the complete documentation index at: https://usegately.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

The GatelyCampaigns class gives you full programmatic control over Gately’s email campaign system — create and send campaigns, schedule them, generate content with AI, track stats, and manage attachments.

Installation

npm install @gately/sdk

Setup

import { GatelyCampaigns } from '@gately/sdk'

const campaigns = new GatelyCampaigns({
  projectId: 'YOUR_PROJECT_ID',
  // apiUrl: 'https://api.usegately.com' // optional override
})
GatelyCampaigns works in both browser and Node.js environments. In Node.js it uses GatelyNodeClient internally; in the browser it uses GatelyBrowserClient.

Core Methods

list()

Retrieve all campaigns for the project, newest first.
const list = await campaigns.list()
// Campaign[]

get(campaignId)

Fetch a single campaign by ID.
const campaign = await campaigns.get('cmp_abc123')
console.log(campaign.status) // 'sent'

send(request)

Create and send (or schedule) a campaign in one call.
const result = await campaigns.send({
  name: 'June Newsletter',
  subject: 'What we shipped this month',
  content: '<h1>June Update</h1><p>Here is what we built...</p>',

  // Pick one or more recipient sources:
  recipient_all_contacts: true,
  // recipient_emails: ['alice@example.com'],
  // recipient_list_ids: ['list_abc'],
  // recipient_segment_ids: ['seg_xyz'],
})

console.log(`Sent to ${result.successful_sends} of ${result.total_recipients}`)
Scheduling — pass scheduled_at to queue for later:
await campaigns.send({
  name: 'Product Launch',
  subject: 'Introducing our new feature!',
  content: '<p>Check it out.</p>',
  recipient_list_ids: ['list_abc123'],
  scheduled_at: '2025-07-01T09:00:00Z',
})

sendBulk(request)

Send to a list of explicit recipient IDs (format: customer_<email>).
await campaigns.sendBulk({
  name: 'VIP Promo',
  subject: 'Exclusive offer for you',
  content: '<p>Here is your deal.</p>',
  recipient_ids: ['customer_alice@example.com', 'customer_bob@example.com'],
})

update(campaignId, updates)

Update a draft or scheduled campaign. Cannot update campaigns that are sending or sent.
await campaigns.update('cmp_abc123', {
  name: 'June Newsletter (v2)',
  subject: 'Updated subject line',
  content: '<p>Revised content.</p>',
})

delete(campaignId)

Delete a campaign. Cannot delete campaigns currently sending.
await campaigns.delete('cmp_abc123')

getStats(campaignId)

Get delivery and engagement stats. The SDK automatically adds derived rate fields.
const stats = await campaigns.getStats('cmp_abc123')

console.log(`Delivery rate: ${stats.delivery_rate}%`)
console.log(`Open rate:     ${stats.open_rate}%`)
console.log(`Click rate:    ${stats.click_rate}%`)
FieldDescription
sentTotal emails targeted
deliveredAccepted by mail servers
openedUnique opens
clickedUnique link clicks
bouncedHard + soft bounces
complainedSpam complaints
delivery_ratedelivered / sent × 100
open_rateopened / delivered × 100
click_rateclicked / opened × 100

AI Content Generation

aiGenerate(request)

Generate a complete, inline-styled HTML email body using Gately’s built-in AI.
const { html } = await campaigns.aiGenerate({
  campaignName: 'Summer Sale',
  subject: '50% off everything this weekend',
  tone: 'friendly', // 'professional' | 'friendly' | 'casual' | 'urgent'
})

// html is ready to pass directly to send()
await campaigns.send({
  name: 'Summer Sale',
  subject: '50% off everything this weekend',
  content: html,
  recipient_all_contacts: true,
})

aiSend(options) — convenience helper

Generate content with AI and send in a single call.
const result = await campaigns.aiSend({
  campaignName: 'Product Update',
  subject: 'New features just dropped',
  tone: 'professional',
  recipient_all_contacts: true,
  // scheduled_at: '2025-07-01T09:00:00Z', // optional
})

console.log(`Sent to ${result.successful_sends} recipients`)

Attachments

uploadAttachment(file) — browser only

Upload a file and get back a base64 attachment object ready to pass into send().
// Get file from an <input type="file"> element
const fileInput = document.querySelector<HTMLInputElement>('#attachment')
const file = fileInput!.files![0]

const { attachment } = await campaigns.uploadAttachment(file)

await campaigns.send({
  name: 'Monthly Report',
  subject: 'Your report is attached',
  content: '<p>Please find your report attached.</p>',
  recipient_emails: ['user@example.com'],
  attachments: [attachment],
})
Allowed file types: PDF, Word, Excel, plain text, CSV, JPEG, PNG, GIF, WebP
Max size: 10 MB per file

Diagnostics

testEmailConfig()

Check your project’s email sending configuration — useful for debugging domain setup.
const config = await campaigns.testEmailConfig()

console.log('Sending from:', config.fromEmail)
console.log('Domain status:', config.domainStatus)
console.log('Email binding:', config.emailBindingAvailable)

Utility Helpers

waitForCompletion(campaignId)

Poll a campaign until it reaches a terminal status (sent, partially_sent, or failed). Useful after scheduling.
const result = await campaigns.send({
  name: 'Launch',
  subject: 'We are live!',
  content: '<p>Check it out.</p>',
  recipient_all_contacts: true,
})

// Wait for it to finish sending
const final = await campaigns.waitForCompletion(result.campaign.id)
console.log('Final status:', final.status)
console.log('Delivered:', final.delivered_count)
Options:
await campaigns.waitForCompletion(
  campaignId,
  3000,    // poll every 3 seconds (default)
  120_000  // give up after 2 minutes (default)
)

TypeScript Types

All types are exported from @gately/sdk:
import type {
  Campaign,
  CampaignStatus,
  SendCampaignRequest,
  SendBulkCampaignRequest,
  UpdateCampaignRequest,
  AIGenerateRequest,
  CampaignAttachment,
  SendCampaignResponse,
  CampaignStats,
  AIGenerateResponse,
  EmailConfigStatus,
  UploadAttachmentResponse,
} from '@gately/sdk'

CampaignStatus

type CampaignStatus =
  | 'draft'
  | 'scheduled'
  | 'sending'
  | 'sent'
  | 'partially_sent'
  | 'failed'

SendCampaignRequest

interface SendCampaignRequest {
  name: string
  subject: string
  content: string
  recipient_emails?: string[]
  recipient_list_ids?: string[]
  recipient_segment_ids?: string[]
  recipient_all_contacts?: boolean
  scheduled_at?: string          // ISO 8601
  attachments?: CampaignAttachment[]
}

CampaignStats

interface CampaignStats {
  sent: number
  delivered: number
  opened: number
  clicked: number
  bounced: number
  complained: number
  // Added by SDK:
  delivery_rate?: number
  open_rate?: number
  click_rate?: number
}

Complete Example

import { GatelyCampaigns } from '@gately/sdk'

const campaigns = new GatelyCampaigns({ projectId: 'YOUR_PROJECT_ID' })

async function runMonthlyNewsletter() {
  // 1. Generate content with AI
  const { html } = await campaigns.aiGenerate({
    campaignName: 'June Newsletter',
    subject: 'What we shipped in June',
    tone: 'friendly',
  })

  // 2. Send to all contacts
  const result = await campaigns.send({
    name: 'June Newsletter',
    subject: 'What we shipped in June',
    content: html,
    recipient_all_contacts: true,
  })

  console.log(`Campaign ID: ${result.campaign.id}`)
  console.log(`Sent: ${result.successful_sends} / ${result.total_recipients}`)

  // 3. Wait for completion and check stats
  const final = await campaigns.waitForCompletion(result.campaign.id)

  const stats = await campaigns.getStats(final.id)
  console.log(`Open rate:  ${stats.open_rate}%`)
  console.log(`Click rate: ${stats.click_rate}%`)
}

runMonthlyNewsletter()

Send Campaign API

REST endpoint reference

Campaign Stats API

Stats endpoint reference

AI Generate API

AI content generation endpoint

Features: Campaigns

Dashboard usage guide