Reputation: 17229
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.
Upvotes: 10
Views: 5381
Reputation: 4054
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
Upvotes: 6
Reputation: 17229
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]'})
Upvotes: 20