Package: mailerlite-api-python
Mailerlite-api-pythonPython Wrapper for Mailerlite API v2DeploymentBuild StatusMetricsLicenseCommunityGetting StartedInstallationThis client is hosted atPyPiunder the namemailerlite-api-python, to install it, simply runpip install mailerlite-api-pythonor install dev version:git clone https://github.com/skoudoro/mailerlite-api-python.git
pip install -e .Method referenceFor the complete reference, visit theofficial MailerLite API reference.ExamplesInitializationFirst, Grab YOUR_API_KEY from your Mailerlite account (Profile > Integrations > Developer Api).>>>frommailerliteimportMailerLiteApi>>>api=MailerLiteApi('YOUR_API_KEY')A second option is to define an environment variable namedMAILERLITE_PYTHON_API_KEY.
Then, you do not need to precise it in your code:>>>frommailerliteimportMailerLiteApi>>>api=MailerLiteApi()CampaignsGet all campaigns or a specific one>>>all_campaigns=api.campaigns.all()>>>draft=api.compaings.all(status='draft')Modify a campaign>>>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)Create / Delete a campaign>>>data={"subject":"Regular campaign subject","name":"Regular campaign name","groups":[2984475,3237221],"type":"regular"}>>>api.campaign.create(data)>>>api.campaign.delete(campaign_id=3971635)Send a campaign>>>data={"subject":"Regular campaign subject","name":"Regular campaign name","groups":[2984475,3237221],"type":"regular"}>>>>>>_,res=api.campaign.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.campaign.update(campaign_id,html=html,plain=plain)>>>api.campaign.send(campaign_id)Cancel a scheduled campaign>>>outbox_campaigns=campaign_obj.all(status='outbox',limit=5)>>>selected_campaign=outbox_campaigns[0]>>>>>>api.campaign.cancel(selected_campaign.id)count campaign>>>api.campaign.count()>>>api.campaign.count(status='draft')SubscribersGet all subscribers>>>api.subscribers.all()>>>api.subscribers.all(stype='active')>>>api.subscribers.active()>>>api.subscribers.unsubscribed()>>>api.subscribers.bounced()>>>api.subscribers.junk()>>>api.subscribers.unconfirmed()Get one subscriber>>>api.subscribers.get(email='demo@mailerlite.com')>>>api.subscribers.get(id=1343965485)search>>>api.subscribers.search(search='demo@mailerlite.com')subscribers groups>>>api.subscribers.groups(id=1343965485)subscribers activity>>>api.subscribers.activity(id='1343965485')>>>api.subscribers.activity(id='1343965485',limit=50,offset=1,atype='clicks')Create subscriber>>>data={'name':'John','email':'demo-678@mailerlite.com','fields':{'company':'MailerLite'}}>>>api.subscribers.create(data)Update subscriber>>>data={'name':'John','fields':{'company':'MailerLite'}}>>>api.subscribers.update(data,id='1343965485')Count subscribersGet 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()GroupsGet all Groups>>>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)Create a Group>>>api.groups.create(group_id=12345,name='My New Group')Rename a Group>>>api.groups.update(group_id=12345,name='New Name')Get a Group>>>api.groups.get(group_id=12345)Delete a Group>>>api.groups.delete()>>>api.groups.delete(group_id=12345)Get all subscribers in a Group>>>api.groups.subscribers(group_id=12345)>>>api.groups.subscribers(group_id=12345,limit=50,offset=1)>>>api.groups.subscribers(group_id=12345,stype='active')Get one subscriber from a Group>>>api.groups.subscriber(group_id=12345,subscriber_id=54321)Add list of subscribers to a GroupThis method calls the import endpointhttps://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_dataargument accepts a list of dictionaries or just one dictionary containing the subscriber name and emailAdd a single subscriber to a GroupThis method calls the add single subscriber endpointhttps://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. Thesubscriber_dataargument accepts all subscriber attributes.
Check available attributes onhttps://developers.mailerlite.com/reference#create-a-subscriberDelete one subscriber from a Group>>>api.groups.delete_subscriber(group_id=12345,subscriber_id=54321)SegmentsGet list of Segments>>>api.segments.all()Get count of Segments>>>api.segments.count()FieldsGet list of Fields>>>api.fields.all()Get one Field>>>api.fields.get(field_id=123456)Create / update / delete one Field>>>api.fields.create(title="my custom title")>>>api.fields.update(field_id=123456,title="my new title 2")>>>api.fields.delete(field_id=123456)WebhooksGet list of Webhooks>>>api.webhooks.all()Get one webhook>>>api.webhooks.get(webhook_id=123456)Create/update/delete one webhook>>>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)Account# 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>>>batch_requests={"requests":[{"method":"GET",..."path":"/api/v2/groups"...},...{"method":"POST",..."path":"/api/v2/groups",..."body":{"name":"New group"}...}...]...}>>>api.batch(batch_requests)TestsStep 1: Install pytestpip install pytestStep 2: Run the testspytest -svv mailerliteContributeWe 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 aPull Request!Start with thecontributing guide!LicenseProject under 3-clause BSD license, more informationshere