> ## 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 Python SDK.

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

```python theme={null}
from mailglyph import MailGlyph

client = MailGlyph("sk_your_secret_key")
```

## List campaigns (`campaigns.list`)

```python theme={null}
page = client.campaigns.list(
    page=1,
    page_size=20,
    status="DRAFT",
)

print(page.total, page.total_pages, len(page.data), len(page.campaigns))
```

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

### Audience: all contacts

```python theme={null}
campaign_all = client.campaigns.create(
    name="Launch campaign",
    subject="Introducing our new feature",
    body="<h1>Big news</h1><p>Check out what is new.</p>",
    from_email="hello@your-verified-domain.com",
    audience_type="ALL",
)

print(campaign_all.id, campaign_all.status)
```

### Audience: filtered contacts

```python theme={null}
campaign_filtered = client.campaigns.create(
    name="Subscribed users campaign",
    subject="Feature update",
    body="<p>Only subscribed users receive this.</p>",
    from_email="hello@your-verified-domain.com",
    audience_type="FILTERED",
    audience_condition={
        "logic": "AND",
        "groups": [
            {
                "filters": [{"field": "subscribed", "operator": "equals", "value": True}]
            }
        ],
    },
)

print(campaign_filtered.id)
```

### Audience: segment

```python theme={null}
campaign_segment = client.campaigns.create(
    name="Segment campaign",
    subject="Offer for premium users",
    body="<p>This goes to one saved segment.</p>",
    from_email="hello@your-verified-domain.com",
    audience_type="SEGMENT",
    segment_id="seg_123",
)

print(campaign_segment.id)
```

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

```python theme={null}
campaign = client.campaigns.get(campaign_all.id)

print(campaign.name, campaign.status)
```

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

```python theme={null}
updated = client.campaigns.update(
    campaign_all.id,
    subject="Updated subject line",
    reply_to="support@your-verified-domain.com",
)

print(updated.subject, updated.reply_to)
```

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

```python theme={null}
send_result = client.campaigns.send(campaign_all.id)

print(send_result)
print(send_result.success, send_result.message, send_result.data.id)
```

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

```python theme={null}
scheduled_result = client.campaigns.send(campaign_all.id, scheduled_for="2026-03-01T10:00:00Z")

print(scheduled_result.success, scheduled_result.message)
```

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

```python theme={null}
test_result = client.campaigns.test(campaign_all.id, email="preview@example.com")

print(test_result)
```

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

```python theme={null}
stats = client.campaigns.stats(campaign_all.id)

print(stats)
```

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

```python theme={null}
cancelled = client.campaigns.cancel(campaign_all.id)

print(cancelled.id, cancelled.status)
```

## Async equivalents

```python theme={null}
from mailglyph import AsyncMailGlyph


async def run() -> None:
    async with AsyncMailGlyph("sk_your_secret_key") as client:
        page = await client.campaigns.list(page=1, page_size=20)
        created = await client.campaigns.create(
            name="Async campaign",
            subject="Async subject",
            body="<p>Async body</p>",
            from_email="hello@your-verified-domain.com",
            audience_type="ALL",
        )
        fetched = await client.campaigns.get(created.id)
        await client.campaigns.update(fetched.id, subject="Async updated")
        await client.campaigns.send(fetched.id)
        await client.campaigns.send(fetched.id, scheduled_for="2026-03-01T10:00:00Z")
        await client.campaigns.test(fetched.id, email="preview@example.com")
        await client.campaigns.stats(fetched.id)
        await client.campaigns.cancel(fetched.id)
        print(page.total)
```

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.