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.
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.
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.
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]'})
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With