> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailglyph.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Campaigns

> Create, send, test, and analyze campaigns with the MailGlyph Node.js SDK.

Use a **secret key** client for campaign methods.

```ts theme={null}
import MailGlyph from 'mailglyph';

const client = new MailGlyph(process.env.MAILGLYPH_SECRET_KEY as string);
```

## List campaigns (`campaigns.list`)

```ts theme={null}
const page = await client.campaigns.list({
  page: 1,
  pageSize: 20,
  status: 'DRAFT'
});

console.log(page.total, page.totalPages, page.data.length);
```

## Create a campaign (`campaigns.create`)

### Audience: all contacts

```ts theme={null}
const campaignAll = await client.campaigns.create({
  name: 'Launch campaign',
  subject: 'Introducing our new feature',
  body: '<h1>Big news</h1><p>Check out what is new.</p>',
  from: 'hello@your-verified-domain.com',
  audienceType: 'ALL'
});

console.log(campaignAll.id, campaignAll.status);
```

### Audience: filtered contacts

```ts theme={null}
const campaignFiltered = await client.campaigns.create({
  name: 'Subscribed users campaign',
  subject: 'Feature update',
  body: '<p>Only subscribed users receive this.</p>',
  from: 'hello@your-verified-domain.com',
  audienceType: 'FILTERED',
  audienceCondition: {
    logic: 'AND',
    groups: [
      {
        filters: [{ field: 'subscribed', operator: 'equals', value: true }]
      }
    ]
  }
});

console.log(campaignFiltered.id);
```

### Audience: segment

```ts theme={null}
const campaignSegment = await client.campaigns.create({
  name: 'Segment campaign',
  subject: 'Offer for premium users',
  body: '<p>This goes to one saved segment.</p>',
  from: 'hello@your-verified-domain.com',
  audienceType: 'SEGMENT',
  segmentId: 'seg_123'
});

console.log(campaignSegment.id);
```

## Get one campaign (`campaigns.get`)

```ts theme={null}
const campaign = await client.campaigns.get(campaignAll.id);

console.log(campaign.name, campaign.status);
```

## Update a campaign (`campaigns.update`)

```ts theme={null}
const updated = await client.campaigns.update(campaignAll.id, {
  subject: 'Updated subject line',
  replyTo: 'support@your-verified-domain.com'
});

console.log(updated.subject, updated.replyTo);
```

## Send a campaign now (`campaigns.send`)

```ts theme={null}
const sendResult = await client.campaigns.send(campaignAll.id);

console.log(sendResult.success, sendResult.message);
```

## Schedule a campaign (`campaigns.send` with `scheduledFor`)

```ts theme={null}
const scheduledResult = await client.campaigns.send(campaignAll.id, {
  scheduledFor: '2026-03-01T10:00:00Z'
});

console.log(scheduledResult.success, scheduledResult.message);
```

## Send a test email (`campaigns.test`)

```ts theme={null}
const testResult = await client.campaigns.test(campaignAll.id, 'preview@example.com');

console.log(testResult.success, testResult.message);
```

## Fetch campaign stats (`campaigns.stats`)

```ts theme={null}
const stats = await client.campaigns.stats(campaignAll.id);

console.log(stats.success, stats.data);
```

## Cancel a scheduled campaign (`campaigns.cancel`)

```ts theme={null}
const cancelled = await client.campaigns.cancel(campaignAll.id);

console.log(cancelled.success, cancelled.message);
```

See full details in the [Campaigns API reference](https://docs.mailglyph.com/api-reference/campaigns/listCampaigns.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.