Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I subscribe someone to a list using the python mailchimp API v2.0?

I want to subscribe a user to a list using the Mailchimp API 2.0 and the official mailchimp python package. I can't find any direct documentation for how.

like image 683
seddonym Avatar asked Nov 27 '14 10:11

seddonym


People also ask

How do I add a subscriber to my Mailchimp API?

Go to mailchimp select account, you will find extras dropdown. Select API keys and in the bottom left you will find a button Create A Key . Click on it and your api key is created. You have to copy the API Key under the API Key header.

Does Mailchimp use Python?

Step 5 - Call the Mailchimp API from python First of all, we use python because our backend is built in Django, but this is where you can let inspiration take hold. Pick a programming language and let mailchimp's API documentation generate the code for you: You just have a few bits to add in and change.


2 Answers

Before you start, you'll need to get your API key and the list id by logging into Mailchimp.

To get the API key, visit Accounts > Extras and generate an API key. To get the list id, visit Lists > My list > Settings > List name and defaults.

Next, make sure you've installed the mailchimp python package:

pip install mailchimp

Finally:

import mailchimp
API_KEY = 'my-api-key'
LIST_ID = 'my-list-id' 

api = mailchimp.Mailchimp(API_KEY)
api.lists.subscribe(LIST_ID, {'email': '[email protected]'})
like image 169
seddonym Avatar answered Dec 03 '22 05:12

seddonym


In addition to the answer by seddonym: If you want to add the name or other details of the subscriber you can do that by adding merge_vars to the function call like this:

api.lists.subscribe(LIST_ID, {'email': email}, merge_vars={'FNAME':fname,'LNAME':lname})

See here for all options: https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

like image 20
pors Avatar answered Dec 03 '22 05:12

pors