> For the complete documentation index, see [llms.txt](https://docs.mailtrap.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mailtrap.io/developers/email-marketing/email-campaigns/create-an-email-campaign.md).

# Create an email campaign

Creates a new email campaign. The campaign must reference an existing sending domain via `domain_id`, and include a template `subject` within `template_attributes`.

Create accepts the same fields as update — pick the audience with `contact_list_ids`/`contact_segment_ids`, set delivery options, and add the design via `template_attributes.body_html`. The campaign is always created in the `draft` state; scheduling and starting are separate actions (see the `schedule` and `start` endpoints).

```json
{"openapi":"3.1.0","info":{"title":"Email Campaigns","version":"2.0.0"},"tags":[{"name":"Email Campaigns","description":"Manage email campaigns"}],"servers":[{"description":"Mailtrap API","url":"https://mailtrap.io"}],"security":[{"HeaderAuth":[]},{"BearerAuth":[]}],"components":{"securitySchemes":{"HeaderAuth":{"type":"apiKey","description":"Pass the API token in the Api-Token header","in":"header","name":"Api-Token"},"BearerAuth":{"type":"http","description":"Pass the API token as a Bearer token in the Authorization header","scheme":"bearer"}},"schemas":{"EmailCampaignCreateRequest":{"description":"Campaign attributes. `name`, `domain_id`, `from_local_part` and a template `subject` are required.","allOf":[{"$ref":"#/components/schemas/EmailCampaignWritableAttributes"},{"type":"object","required":["name","domain_id","from_local_part","template_attributes"],"properties":{"template_attributes":{"type":"object","required":["subject"]}}}]},"EmailCampaignWritableAttributes":{"type":"object","description":"Campaign attributes. Both create and update accept the same fields. Create and update only\nmanage attributes and audience — the campaign always stays in `draft`; scheduling and\nstarting are performed via the dedicated lifecycle action endpoints.","properties":{"name":{"type":"string","description":"Campaign name."},"domain_id":{"type":"integer","format":"int64","description":"ID of the verified sending domain used for the campaign, as returned by the Sending Domains endpoints."},"from_display_name":{"type":"string","description":"Display name shown in the From header."},"from_local_part":{"type":"string","description":"Local part (before the @) of the From address."},"reply_to":{"$ref":"#/components/schemas/ReplyTo"},"template_attributes":{"$ref":"#/components/schemas/TemplateAttributes"},"delivery_mode":{"type":"string","description":"How the campaign is delivered. `rapid` sends as fast as possible; `gradual` throttles\nsending to `delivery_options.emails_per_hour`.","enum":["rapid","gradual"]},"delivery_options":{"type":"object","description":"Delivery throttling options. Applies when `delivery_mode` is `gradual`.","properties":{"emails_per_hour":{"type":["integer","null"]}}},"contact_list_ids":{"type":"array","description":"IDs of contact lists to send to. Treated as the full set of included lists — lists not\nlisted are removed. Combine with `contact_segment_ids` to target both.","items":{"type":"integer","format":"int64"}},"contact_segment_ids":{"type":"array","description":"IDs of contact segments to send to. Treated as the full set of included segments.","items":{"type":"integer","format":"int64"}}}},"ReplyTo":{"type":"object","description":"Reply-To address parts.","properties":{"display_name":{"type":"string"},"local_part":{"type":"string"},"domain":{"type":"string"}}},"TemplateAttributes":{"type":"object","description":"Inline email template — the campaign's subject and design. Each campaign has exactly\none template, created together with it using the `template_attributes` fields.\nOn update, the campaign's template is always edited in place.\nUpdates are partial: only the sub-fields you provide change; sub-fields that\nare omitted or sent as `null` keep their current value.","properties":{"subject":{"type":"string","maxLength":255,"description":"Email subject line. Required when creating a campaign.\nSupports merge tags, e.g. `Hi {{first_name}}`."},"body_html":{"type":"string","maxLength":10000000,"description":"HTML body of the email (the design). Optional for a draft; required before the\ncampaign can be scheduled or started. Include an unsubscribe link via an anchor\nwhose `href` contains the `__unsubscribe_url__` placeholder — a design without one\nis accepted, but flagged with an \"Unsubscribe link is missing\" warning in the\nMailtrap UI. Supports `{{tag_name}}` merge tags."},"body_text":{"type":["string","null"],"maxLength":10000000,"description":"Optional plain-text alternative of the email body. Supports the same\n`__unsubscribe_url__` placeholder and `{{tag_name}}` merge tags as `body_html`."},"merge_tags":{"type":"array","description":"Bare names of the merge tags referenced in the subject/body, without the `{{ }}`\ndelimiters — e.g. `[\"first_name\"]` when the content contains `{{first_name}}`.\nLets Mailtrap track which contact fields the campaign uses. Replaced as a whole\nwhen provided; defaults to an empty array.","items":{"type":"string"}}}},"EmailCampaign":{"type":"object","properties":{"id":{"type":"integer","format":"int64"},"domain_id":{"type":"integer","format":"int64","description":"ID of the sending domain used for the campaign, as returned by the Sending Domains endpoints."},"domain_name":{"type":"string"},"name":{"type":"string"},"from_local_part":{"type":"string"},"from_display_name":{"type":"string"},"reply_to":{"$ref":"#/components/schemas/ReplyTo"},"current_state":{"type":"string","description":"Current state of the campaign in its lifecycle.","enum":["draft","scheduled","started","queued","paused","terminating","under_review","finished","failed","failed_immediately"]},"current_state_metadata":{"type":"object","description":"Metadata about the most recent state transition. Which keys are present depends on the state.","properties":{"reason":{"type":"string"},"error":{"type":"string","description":"Last error message recorded for a failed campaign."},"errors":{"type":"array","description":"Per-recipient errors recorded when sending failed.","items":{"type":"object","properties":{"message":{"type":"string"},"rcpt_index":{"type":"integer"}}}},"scheduled_at":{"type":"string","format":"date-time","description":"When the campaign is scheduled to send (ISO 8601). Present in the `scheduled` state."}}},"created_at":{"type":"string","format":"date-time"},"updated_at":{"type":"string","format":"date-time"},"last_started_at":{"type":["string","null"],"format":"date-time"},"last_started_at_date":{"type":"string","format":"date","description":"Date the campaign was last started. Present only when the campaign has been started."},"recipient_total_count":{"type":["integer","null"],"description":"Total number of recipients targeted by the campaign. `null` until the audience is resolved."},"contact_list_ids":{"type":"array","description":"IDs of the contact lists included in the campaign's audience.","items":{"type":"integer","format":"int64"}},"contact_segment_ids":{"type":"array","description":"IDs of the contact segments included in the campaign's audience.","items":{"type":"integer","format":"int64"}},"delivery_mode":{"type":"string","description":"How the campaign is delivered. `rapid` sends as fast as possible; `gradual` throttles\nsending to `delivery_options.emails_per_hour`.","enum":["rapid","gradual"]},"delivery_options":{"type":"object","description":"Delivery throttling options. Applies when `delivery_mode` is `gradual`.","properties":{"emails_per_hour":{"type":["integer","null"]}}},"template":{"type":"object","description":"The campaign's template. `body_html` and `body_text` are returned only on\nsingle-campaign responses (create, update, get-by-ID); the list endpoint omits them to\nkeep the payload small.","properties":{"id":{"type":"integer","format":"int64"},"subject":{"type":"string"},"merge_tags":{"type":"array","items":{"type":"string"}},"body_html":{"type":["string","null"],"description":"HTML body (the design). Returned only on single-campaign responses."},"body_text":{"type":["string","null"],"description":"Plain-text body. Returned only on single-campaign responses."}}}}},"UnauthenticatedResponse":{"type":"object","properties":{"error":{"type":"string"}}},"PermissionDeniedResponse":{"type":"object","properties":{"errors":{"type":"string"}}},"ValidationErrorResponse":{"type":"object","properties":{"errors":{"type":"object","additionalProperties":{"type":"array","items":{"type":"string"}}}}},"RateLimitedResponse":{"type":"object","properties":{"error":{"type":"string"}}}},"responses":{"EmailCampaignResponse":{"description":"A single email campaign.","content":{"application/json":{"schema":{"type":"object","properties":{"data":{"$ref":"#/components/schemas/EmailCampaign"}}}}}},"UNAUTHENTICATED":{"description":"Authentication failed — missing or invalid API token.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/UnauthenticatedResponse"}}}},"PERMISSION_DENIED":{"description":"The API token does not have permission to access this resource.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/PermissionDeniedResponse"}}}},"VALIDATION_ERROR":{"description":"The request was rejected due to validation errors.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ValidationErrorResponse"}}}},"RATE_LIMITED":{"description":"Too many requests. The public API is rate limited to 150 requests per 10 seconds per\nAPI token. Retry after the window resets (see the `x-ratelimit-reset` header).","headers":{},"content":{"application/json":{"schema":{"$ref":"#/components/schemas/RateLimitedResponse"}}}}}},"paths":{"/api/email_campaigns":{"post":{"operationId":"createEmailCampaign","summary":"Create an email campaign","description":"Creates a new email campaign. The campaign must reference an existing sending domain via\n`domain_id`, and include a template `subject` within `template_attributes`.\n\nCreate accepts the same fields as update — pick the audience with\n`contact_list_ids`/`contact_segment_ids`, set delivery options, and add the design via\n`template_attributes.body_html`. The campaign is always created in the `draft` state;\nscheduling and starting are separate actions (see the `schedule` and `start` endpoints).","tags":["Email Campaigns"],"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/EmailCampaignCreateRequest"}}}},"responses":{"201":{"$ref":"#/components/responses/EmailCampaignResponse"},"401":{"$ref":"#/components/responses/UNAUTHENTICATED"},"403":{"$ref":"#/components/responses/PERMISSION_DENIED"},"422":{"$ref":"#/components/responses/VALIDATION_ERROR"},"429":{"$ref":"#/components/responses/RATE_LIMITED"}}}}}}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mailtrap.io/developers/email-marketing/email-campaigns/create-an-email-campaign.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
