seddonym
seddonym

Reputation: 17229

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.

Upvotes: 10

Views: 5381

Answers (2)

pors
pors

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

seddonym
seddonym

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

Related Questions