⚠️ ⚠️ ⚠️ ⚠️ ⚠️ WARNING ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ : This REPO is no longer maintained in favor of the official mailerlite python client look here
Python Wrapper for Mailerlite API
Deployment | |
Build Status | |
Metrics | |
License | |
Community |
This client is hosted at PyPi under the name mailerlite-api-python, to install it, simply run
pip install mailerlite-api-python
or install dev version:
git clone https://github.com/skoudoro/mailerlite-api-python.git
pip install -e .
For the complete reference, visit the official MailerLite API reference.
This version is handling the V2_BETA API (classic version).
we are currently updating the project to handle the new V2 released last summer 2022.
First, Grab YOUR_API_KEY from your Mailerlite account (Profile > Integrations > Developer Api).
>>> from mailerlite import MailerLiteApi
>>> api = MailerLiteApi('YOUR_API_KEY')
A second option is to define an environment variable named MAILERLITE_PYTHON_API_KEY
.
Then, you do not need to precise it in your code:
>>> from mailerlite import MailerLiteApi
>>> api = MailerLiteApi()
>>> all_campaigns = api.campaigns.all()
>>> draft = api.compaings.all(status='draft')
>>> one_campaign = all_campaigns[0]
>>> html = '<head></head><body><h1>Title</h1><p>Content</p><p><small><a href=\"{$unsubscribe}\">Unsubscribe</a></small></p></body>'
>>> plain = "Your email client does not support HTML emails. "
>>> plain += "Open newsletter here: {$url}. If you do not want"
>>> plain += " to receive emails from us, click here: {$unsubscribe}"
>>> api.campaigns.update(one_campaign.id, html=html, plain=plain)
>>> data = {"subject": "Regular campaign subject",
"name": "Regular campaign name",
"groups": [2984475, 3237221],
"type": "regular"}
>>> api.campaigns.create(data)
>>> api.campaigns.delete(campaign_id=3971635)
>>> data = {"subject": "Regular campaign subject",
"name": "Regular campaign name",
"groups": [2984475, 3237221],
"type": "regular"}
>>>
>>> _, res = api.campaigns.create(data)
>>> campaign_id = res['id']
>>>
>>> html = '<head></head><body><h1>Your Title</h1><p>Your Content</p><p><small>'
>>> html += '<a href=\"{$unsubscribe}\">Unsubscribe</a></small></p></body>'
>>> plain = "Your email client does not support HTML emails. "
>>> plain += "Open newsletter here: {$url}. If you do not want"
>>> plain += " to receive emails from us, click here: {$unsubscribe}"
>>>
>>> api.campaigns.update(campaign_id, html=html, plain=plain)
>>> api.campaigns.send(campaign_id)
>>> outbox_campaigns = campaign_obj.all(status='outbox', limit=5)
>>> selected_campaign = outbox_campaigns[0]
>>>
>>> api.campaigns.cancel(selected_campaign.id)
>>> api.campaigns.count()
>>> api.campaigns.count(status='draft')
>>> api.subscribers.all()
>>> api.subscribers.all(stype='active')
>>> api.subscribers.active()
>>> api.subscribers.unsubscribed()
>>> api.subscribers.bounced()
>>> api.subscribers.junk()
>>> api.subscribers.unconfirmed()
>>> api.subscribers.get(email='demo@mailerlite.com')
>>> api.subscribers.get(id=1343965485)
>>> api.subscribers.search(search='demo@mailerlite.com')
>>> api.subscribers.groups(id=1343965485)
>>> api.subscribers.activity(id='1343965485')
>>> api.subscribers.activity(id='1343965485', limit=50, offset=1, atype='clicks')
>>> data = {'name': 'John',
'email': 'demo-678@mailerlite.com',
'fields': {'company': 'MailerLite'}
}
>>> api.subscribers.create(data)
>>> data = {'name': 'John',
'fields': {'company': 'MailerLite'}
}
>>> api.subscribers.update(data, id='1343965485')
Get the total count of all subscribers in a single call.
Please, be aware that this is not a documented feature in the official API.
>>> api.subscribers.count()
>>> api.groups.all()
>>> api.groups.all(limit=50)
>>> api.groups.all(offset=10)
>>> api.groups.all(gfilters='My Group')
>>> api.groups.all(group_id=12345)
>>> api.groups.create(group_id=12345, name='My New Group')
>>> api.groups.update(group_id=12345, name='New Name')
>>> api.groups.get(group_id=12345)
>>> api.groups.delete()
>>> api.groups.delete(group_id=12345)
>>> api.groups.subscribers(group_id=12345)
>>> api.groups.subscribers(group_id=12345, limit=50, offset=1)
>>> api.groups.subscribers(group_id=12345, stype='active')
>>> api.groups.subscriber(group_id=12345, subscriber_id=54321)
This method calls the import endpoint https://developers.mailerlite.com/reference#add-many-subscribers
>>> api.groups.add_subscribers(group_id=12345, subscribers_data=[{"email": "john@wick.com", "name": "John Wick"}], autoresponders=False, resubscribe=False, as_json=False)
subscriber_data
argument accepts a list of dictionaries or just one dictionary containing the subscriber name and email
This method calls the add single subscriber endpoint https://developers.mailerlite.com/reference#add-single-subscriber
>>> api.groups.add_single_subscriber(group_id=12345, subscribers_data={"email": "john@wick.com", "name": "John Wick" ...}, autoresponders=False, resubscribe=False, as_json=False)
Unlike the method above, this adds only one subscriber to a group. The subscriber_data
argument accepts all subscriber attributes.
Check available attributes on https://developers.mailerlite.com/reference#create-a-subscriber
>>> api.groups.delete_subscriber(group_id=12345, subscriber_id=54321)
>>> api.segments.all()
>>> api.segments.count()
>>> api.fields.all()
>>> api.fields.get(field_id=123456)
>>> api.fields.create(title="my custom title")
>>> api.fields.update(field_id=123456, title="my new title 2")
>>> api.fields.delete(field_id=123456)
>>> api.webhooks.all()
>>> api.webhooks.get(webhook_id=123456)
>>> api.webhooks.create(url="https://yoursite/script-is-here",
... event="subscriber.create")
>>> api.webhooks.update(webhook_id=123456,
... url="https://yoursite/script-is-here",
... event="subscriber.create")
>>> api.webhooks.delete(webhook_id=123456)
# Get some info or stats
>>> api.account.info()
>>> api.account.stats()
>>> api.account.double_optin()
# Set up the double_optin
>>> api.account.set_double_optin(True)
>>> batch_requests = {"requests": [{"method":"GET",
... "path": "/api/v2/groups"
... },
... {"method":"POST",
... "path": "/api/v2/groups",
... "body": {"name": "New group"}
... }
... ]
... }
>>> api.batch(batch_requests)
- Step 1: Install pytest
pip install pytest
- Step 2: Run the tests
pytest -svv mailerlite
We love contributions!
You've discovered a bug or something else you want to change - excellent! Create an issue!
You've worked out a way to fix it – even better! Submit a Pull Request!
Start with the contributing guide!
Project under 3-clause BSD license, more informations here