For more information about MailerLite API, please visit the following link:
API keys are a quick way to implement machine-to-machine authentication without any direct inputs from a human beyond initial setup.
For more information how to obtain an API key visit the following link
- MailerLite Python SDK
- Getting started
- Table of Contents
- Installation
- Usage
- Subscribers
- Groups
- Segments
- Fields
- Automations
- List all automations
- Get an automation
- Get subscribers activity for an automation
- Create an automation
- Update an automation
- Delete an automation
- Copy an automation
- Enable an automation
- Disable an automation
- Send a test of an automation
- Add subscribers to an automation
- Re-add subscribers to an automation
- Copy automation subscribers to groups
- Cancel active automation runs
- Skip an automation delay
- List automation triggers
- Create an automation trigger
- Get an automation trigger
- Update an automation trigger
- Delete an automation trigger
- List automation steps
- Create an automation step
- Get an automation step
- Update an automation step
- Delete an automation step
- Copy an automation step
- Move an automation step
- Select the winner of an A/B test step
- Get the content of an automation email
- Update the content of an automation email
- Get the report stats of an automation email
- Get the unsubscribe reasons of an automation email
- List automation templates
- Create an automation from a template
- Get the run counts of automation steps
- Get the run counts of automation triggers
- Get the stats of A/B test steps
- List the subscribers on an automation step
- List the subscribers on an automation trigger
- Get the monthly email totals of an automation
- Campaigns
- Emails
- Get an email
- Get the HTML of an email
- Get a block
- Insert a block
- Update a block
- Delete a block
- Move a block
- Validate a block
- Update the design settings
- Validate the design settings
- Save an email as a template
- List the versions of an email
- Save a version of an email
- Restore a version of an email
- Retry a write with an idempotency key
- Brand styles
- Files
- Forms
- Batching
- Webhooks
- Timezones
- Campaign languages
Please run the following command:
python -m pip install mailerliteThe first thing that you need to do is to import mailerlite into your project and to instantiate the client by passing your API key:
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})MailerLite API supports versioning, so you can pass additional argument to specify the API version:
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key", "api_version": "2038-01-19"})API responses with a 4xx or 5xx status code raise a mailerlite.ApiError exception. ApiError is a subclass of requests.HTTPError, so except requests.HTTPError continues to work. The exception has these attributes:
status_code: the HTTP status code.message: the error message of the API.code: a machine-readable error code, if the API sends one. For example,version_conflict.etag: the current etag of the email, if the API sends one. The API sends it withversion_conflict.errors: the validation errors for each field, if the API sends them.response: the fullrequests.Response.
The attributes are None when the response does not have them.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
try:
response = client.subscribers.get("some@email.com")
except MailerLite.ApiError as e:
print(e.status_code) # e.g. 404
print(e.message) # error message from the API
print(e.errors) # validation errors, if any
print(e.response.json()) # full error body from the APINetwork-level failures (DNS errors, connection resets, timeouts) raise the standard requests exceptions such as requests.ConnectionError and requests.Timeout. Catch requests.RequestException to handle both HTTP and network errors with a single handler.
Note: the batch endpoint (client.batch.request()) returns HTTP 200 even when individual sub-requests fail, so no exception is raised for them — check the per-request status codes in the returned body.
Upgrading from 0.1.x: methods that previously returned False or a raw status code on failure now raise requests.HTTPError instead. In particular, subscribers.delete() and subscribers.forget() used to return 204/200 and now return True on success.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.subscribers.list(limit=10, page=1, filter={"status": "active"})import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.subscribers.create(
"some@email.com",
fields={"name": "John", "last_name": "Doe"},
ip_address="1.2.3.4",
optin_ip="1.2.3.4",
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.subscribers.update(
"some@email.com",
fields={"name": "New", "last_name": "Name"},
ip_address="1.2.3.5",
optin_ip="1.2.3.5",
)update sends PUT /subscribers/{subscriber}. The first argument is the email address or the ID of the subscriber. The subscriber must exist, or the API returns 404. groups replaces the subscriber's groups: groups you leave out are removed. To create a subscriber, or to add groups without removing others, use create. Pass new_email to change the email address.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.subscribers.get("some@email.com")import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
subscriber_id = 1234567890
response = client.subscribers.delete(subscriber_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
subscriber_id = 1234567890
response = client.subscribers.forget(subscriber_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
subscriber_id = 1234567890
response = client.subscribers.activity_log(
subscriber_id, limit=10, page=1, filter={"log_name": "email_open"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.groups.list(limit=10, page=1, filter={"name": "My"}, sort="name")import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
group_name = "My Group"
response = client.groups.create(group_name)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
group_id = 1234567
group_name = "My New Group"
response = client.groups.update(group_id, group_name)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
group_id = 1234567
response = client.groups.delete(group_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
group_id = 1234567
response = client.groups.get_group_subscribers(
group_id, limit=10, filter={"status": "active"}
)
next_cursor = response["meta"]["next_cursor"]
response = client.groups.get_group_subscribers(
group_id, cursor=next_cursor, limit=10, filter={"status": "active"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
group_id = 1234567
subscribers = [
{"email": "subscriber1@example.com", "name": "Subscriber One"},
{"email": "subscriber2@example.com", "name": "Subscriber Two"},
]
response = client.groups.import_subscribers_to_group(group_id, subscribers)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
subscriber_id = 111222
group_id = 1234567
response = client.subscribers.assign_subscriber_to_group(subscriber_id, group_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
subscriber_id = 111222
group_id = 1234567
response = client.subscribers.unassign_subscriber_from_group(subscriber_id, group_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.segments.list(limit=10, page=1)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
segment_id = 123456
name = "My New Segment Name"
response = client.segments.update(segment_id, name)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
segment_id = 123456
response = client.segments.delete(segment_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
segment_id = 123456
response = client.segments.get(segment_id, limit=10, filter={"status": "active"})import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
segment_id = 123456
response = client.segments.get_subscribers(
segment_id, limit=10, filter={"status": "active"}
)
next_cursor = response["meta"]["next_cursor"]
response = client.segments.get_subscribers(
segment_id, cursor=next_cursor, limit=10, filter={"status": "active"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.fields.list(
limit=10, page=1, sort="name", filter={"keyword": "abc", "type": "text"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
name = "My Field"
type = "text"
response = client.fields.create(name, type)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
field_id = 123456
name = "My New Field"
response = client.fields.update(field_id, name)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
field_id = 123456
response = client.fields.delete(field_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.automations.list(
limit=10, page=1, filter={"enabled": True, "name": "some name", "group": 123456}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.get(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.activity(
automation_id,
page=1,
limit=10,
filter={"status": "active", "date_from": "2022-12-20", "date_to": "2022-12-31"},
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.automations.create("My automation")import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.update(automation_id, "My new name")import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.delete(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.copy(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.enable(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.disable(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.test(automation_id, "some@email.com")import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.add_subscribers(
automation_id, emails=["some@email.com"], groups=[1234567]
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.readd(
automation_id, "last_completed_step", {"status": "completed"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.activity_add_to_group(
automation_id, ["1234567"], {"status": "completed"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.activity_cancel_automation(
automation_id, {"status": "active"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.activity_skip_delay(automation_id, {"status": "active"})import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.list_triggers(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.create_trigger(
automation_id, "subscriber_joins_group", data={"group_ids": [1234567]}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
trigger_id = 123456
response = client.automations.get_trigger(automation_id, trigger_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
trigger_id = 123456
response = client.automations.update_trigger(
automation_id, trigger_id, data={"group_ids": [1234567]}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
trigger_id = 123456
response = client.automations.delete_trigger(automation_id, trigger_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.list_steps(automation_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.create_step(
automation_id, "delay", parent_id=123456, data={"unit": "days", "value": 1}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.get_step(automation_id, step_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.update_step(
automation_id, step_id, data={"unit": "days", "value": 2}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.delete_step(
automation_id, step_id, branch_to_delete="all"
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.copy_step(automation_id, step_id, parent_id=123456)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.reorder_steps(
automation_id, step_id, new_parent_step_id=123456
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.select_ab_test_winner(automation_id, step_id, "a_path")import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
email_id = 123456
response = client.automations.get_email_content(automation_id, email_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
email_id = 123456
response = client.automations.update_email_content(
automation_id, email_id, "<p>Hello</p>"
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
email_id = 123456
response = client.automations.email_report_stats(
automation_id, email_id, start_date="2026-01-01", end_date="2026-01-31"
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
email_id = 123456
response = client.automations.email_unsubscribe_reasons(automation_id, email_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.automations.list_templates(limit=10, page=1, filter={"category": 1})import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
template_id = 123456
response = client.automations.select_template(template_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.steps_run_counts(automation_id)
response = client.automations.step_run_counts(automation_id, step_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
trigger_id = 123456
response = client.automations.triggers_run_counts(automation_id)
response = client.automations.trigger_run_counts(automation_id, trigger_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.ab_steps_stats(automation_id)
response = client.automations.ab_step_stats(automation_id, step_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
step_id = 123456
response = client.automations.step_subscribers(
automation_id, step_id, "queued", limit=10, page=1
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
trigger_id = 123456
response = client.automations.trigger_subscribers(
automation_id, trigger_id, "completed", limit=10, page=1
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
automation_id = 123456
response = client.automations.email_monthly_totals(
automation_id, start_date="2026-01-01", end_date="2026-06-30"
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.campaigns.list(
limit=10, page=1, filter={"status": "ready", "type": "regular"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
campaign_id = 123456
response = client.campaigns.get(campaign_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
params = {
"name": "Test Campaign",
"language_id": 1,
"type": "regular",
"emails": [
{
"subject": "This is a test campaign",
"from_name": "Test Man",
"from": "testuser@mailerlite.com",
"content": "Hi there, this is a test campaign!",
}
],
}
response = client.campaigns.create(params)To build the email with the simple editor, set editor to MailerLite.EDITOR_SIMPLE, and do not send content. Then edit the email with the email methods.
template_idis optional. It is a shared template of the simple editor that the email starts from. It needseditor.- All emails of a campaign must have the same
editorandtemplate_id. - Only
createacceptseditorandtemplate_id.updatedoes not.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
params = {
"name": "Test Campaign",
"type": "regular",
"emails": [
{
"subject": "This is a test campaign",
"from_name": "Test Man",
"from": "testuser@mailerlite.com",
"editor": MailerLite.EDITOR_SIMPLE,
}
],
}
response = client.campaigns.create(params)
email_id = response["data"]["emails"][0]["id"]import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
campaign_id = 123456
params = {
"name": "New Campaign Name",
"language_id": 2,
"emails": [
{
"subject": "This is a test campaign",
"from_name": "Test Man",
"from": "testuser@mailerlite.com",
"content": "Hi there, this is a test campaign!",
}
],
}
response = client.campaigns.update(campaign_id, params)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
campaign_id = 123456
params = {
"delivery": "scheduled",
"schedule": {"date": "2022-12-31", "hours": "22", "minutes": "00"},
}
response = client.campaigns.schedule(campaign_id, params)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
campaign_id = 123456
response = client.campaigns.cancel(campaign_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
campaign_id = 123456
response = client.campaigns.delete(campaign_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
campaign_id = 123456
response = client.campaigns.activity(campaign_id)These methods edit emails built with the simple editor. To create such an email, create a simple editor campaign. The ID of the email is in response['data']['emails'][0]['id'].
- IDs are strings of digits. You can also give them as
int. An empty ID raisesmailerlite.EmptyIdError, and an ID that does not contain only digits raisesmailerlite.InvalidIdError. The SDK sends no request in these cases. - Before the request, the SDK checks only the IDs and the
settingsofupdate_settings(). It sends all other values as you give them, and the API checks them. An invalid value raisesmailerlite.ApiError, for example with status422. - The methods return a
mailerlite.ApiResponse. It is adictwith the response body, and it also hasstatus_codeandheaders. - Each write needs the etag from the last response for the email, and returns a new etag. The etag covers the whole email, so send the writes to one email one after another.
- If the email changed after you read it, the write raises
mailerlite.ApiErrorwithcodeequal tomailerlite.VERSION_CONFLICT. Itsetagis the current etag. Get the email again, and then repeat the write with the current etag. - The keys of
editor_contentare camelCase, for exampletextAlign. The SDK sends them as you give them.
Returns the email with its settings and blocks, and the etag of the email.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
response = client.emails.get(email_id)
email = response["data"]
etag = response["etag"]Returns the HTML that the email sends. The API can return a temporary 503 error. If it does, try again later.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
response = client.emails.get_html(email_id)
html = response["data"]["html"]Returns the block and its place in the email: placement has the IDs of the blocks after and before it, or None.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
block_id = "57"
response = client.emails.get_block(email_id, block_id)
block = response["data"]Use the block types in MailerLite.BlockType. editor_content can be empty. Without placement, the API puts the block at the end of the email. The before and after positions also need a block_id. The API ignores the block_id of start and end. The SDK sends the placement as you give it.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
etag = client.emails.get(email_id)["etag"]
response = client.emails.create_block(
email_id,
etag,
MailerLite.BlockType.PARAGRAPH,
{"attrs": {"textAlign": "left"}, "content": [{"type": "text", "text": "Hello"}]},
placement={"position": MailerLite.BlockPosition.START},
)
block_id = response["data"]["id"]
etag = response["etag"]The new editor_content replaces the whole content of the block. The API removes the keys that you do not send.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
block_id = "57"
etag = "etag-from-the-last-response"
response = client.emails.update_block(
email_id,
block_id,
etag,
{
"attrs": {"textAlign": "center"},
"content": [{"type": "text", "text": "Hello again"}],
},
)
etag = response["etag"]Also deletes the blocks in it. The response has only the new etag.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
block_id = "57"
etag = "etag-from-the-last-response"
response = client.emails.delete_block(email_id, block_id, etag)
etag = response["etag"]The before and after positions also need a block_id. The API ignores the block_id of start and end. The SDK sends the placement as you give it.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
block_id = "57"
etag = "etag-from-the-last-response"
response = client.emails.move_block(
email_id,
block_id,
etag,
{"position": MailerLite.BlockPosition.AFTER, "block_id": "12"},
)
etag = response["etag"]Checks a block before you insert it. It needs no email and no etag. An invalid block does not raise an error: the response has valid set to False and the errors for each field.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.emails.validate_block(
MailerLite.BlockType.BUTTON,
{
"attrs": {"href": "https://example.com"},
"content": [{"type": "text", "text": "Buy now"}],
},
)
if not response["valid"]:
print(response["errors"])Send only the keys to change, in the shape of settings in the email. The groups are brand, layout, typography, button and footer.
- Send all style values as strings, for example
'16'and not16. - The API stores each value that you send, also a value that the email inherits from your account. Then the email no longer follows your account for that value.
- To unset a brand colour, send an empty string. The API stores the value empty, and the email then uses the account value.
- To unset a footer text, send an empty editor document:
{'editor_content': {}}or{'editor_content': {'content': []}}. - Other colours, sizes,
button.border_radiusandbrand.stylecannot be empty. An empty string gives a422error. footer.social_linksreplaces the whole list. Keep thelink_idof each link to keep its click history.brand.modeis one ofMailerLite.BrandMode. To apply a preset palette, send itsidentifierfrom the brand styles inbrand.style.- If the API removed unsafe values, the response has
rejectedwith the keys of these values. settingsmust be adictwithout anetagkey, because the SDK adds theetagargument to it. Otherwise the SDK raisesTypeErrororValueErrorand sends no request.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
etag = "etag-from-the-last-response"
response = client.emails.update_settings(
email_id,
etag,
{
"brand": {"mode": MailerLite.BrandMode.CUSTOM, "background_color": "#f4f4f4"},
"typography": {"paragraph": {"p1": {"size": "18"}}},
},
)
etag = response["etag"]
if response.get("rejected"):
print(response["rejected"]["keys"])Checks a change with the rules of the update, and saves nothing. It needs no etag. Invalid settings do not raise an error: the response has valid set to False and the errors for each field.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
response = client.emails.validate_settings(
email_id,
{
"button": {"border_radius": "8"},
},
)
if not response["valid"]:
print(response["errors"])Copies the email to a new template. The API cannot delete templates.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
response = client.emails.create_template(email_id, "Monthly newsletter")
template_id = response["data"]["id"]A version is a saved copy of the content and the settings of an email. The list starts with the newest version. current marks the version that the email has now.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
response = client.emails.list_versions(email_id, page=1, limit=10)Writes through the API do not save versions. If nothing changed after the last version, the API saves no new version and returns the last version. response.status_code is 201 for a new version and 200 for the last version.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
response = client.emails.create_version(email_id)
version_id = response["data"]["id"]
is_new = response.status_code == 201Puts the email back to the version, and returns the full email with the new etag. Before the restore, the API saves the current state as a version, so you can undo the restore.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
version_id = "901"
etag = "etag-from-the-last-response"
response = client.emails.restore_version(email_id, version_id, etag)
etag = response["etag"]If a write fails with a network error, you do not know if the API did the write. Give the write an idempotency_key, and retry it with the same key and the same arguments. If the first request succeeded, the API returns the stored response and does not do the write two times. Then response.is_idempotent_replay is True.
- Use a new key for each write. The API keeps a key for 24 hours.
- The key must be 1 to 255 printable ASCII characters. The SDK sends the key as you give it. The API answers an invalid key with status
400. - If
requestscannot send the key in a header, it raisesrequests.exceptions.InvalidHeader. For example, a key with a space at the start or a new line causes this error. - The same key with different arguments raises
ApiErrorwith status422. If the first request is still running, the API returns409without acode. - These methods accept a key:
create_block,update_block,delete_block,move_block,update_settings,create_template,create_versionandrestore_version.
import uuid
import requests
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
email_id = "123456"
etag = "etag-from-the-last-response"
key = str(uuid.uuid4())
try:
response = client.emails.create_block(
email_id, etag, MailerLite.BlockType.DIVIDER, {}, idempotency_key=key
)
except (requests.ConnectionError, requests.Timeout):
# Network error: retry with the same key and the same arguments.
response = client.emails.create_block(
email_id, etag, MailerLite.BlockType.DIVIDER, {}, idempotency_key=key
)
print(response["data"]["id"], response.is_idempotent_replay)Returns the palettes of your account: brand (your brand styles, or None if your account has none), default and the presets. Match a palette on its identifier, not on its name. To apply a preset to an email, update the design settings with {'brand': {'style': identifier}}.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.brand_styles.get()
identifiers = [preset["identifier"] for preset in response["data"]["presets"]]Returns the files of your media library. Use their url in image blocks. width and height are None for files that are not images.
filter:name(a part of the file name) andtype(one ofMailerLite.FileType).sort: one ofMailerLite.FileSort. The default is newest first.
import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.files.list(
filter={"name": "summer sale", "type": MailerLite.FileType.IMAGE},
sort=MailerLite.FileSort.SIZE_DESCENDING,
page=1,
limit=25,
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.forms.list(
limit=10, page=1, sort="name", filter={"name": "form name"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
form_id = 123456
response = client.forms.get(form_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
form_id = 123456
name = "New Form Name"
response = client.forms.update(form_id, name)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
form_id = 123456
response = client.forms.delete(form_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
form_id = 123456
response = client.forms.get_subscribers(
form_id, page=1, limit=10, filter={"status": "active"}
)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
params = [
{"method": "GET", "path": "api/subscribers/list"},
{"method": "GET", "path": "api/campaigns/list"},
]
response = self.client.batches.request(params)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.webhooks.list()import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
webhook_id = 123456
response = client.webhooks.get(webhook_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
events = ["subscriber.created", "subscriber.updated", "subscriber.unsubscribed"]
url = "https://my-url.com"
name = "Webhook name"
response = client.webhooks.create(events, url, name)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
webhook_id = 123456
events = ["subscriber.created", "subscriber.updated", "subscriber.unsubscribed"]
url = "https://my-url.com"
name = "Webhook name"
enabled = False
response = client.webhooks.update(webhook_id, events, url, name, enabled)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
webhook_id = 123456
response = client.webhooks.delete(webhook_id)import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.timezone.list()import mailerlite as MailerLite
client = MailerLite.Client({"api_key": "your-api-key"})
response = client.campaigns.languages()