Mailchimp code examples curl
Here are some examples of Mailchimp API requests using curl
:
1. Authenticate with Mailchimp API
To authenticate with the Mailchimp API, you need to generate an API key and use it to authenticate your requests. Here's an example of how to do this using curl
:
curl -X POST \
https://login.mailchimp.com/oauth2/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id=YOUR_API_KEY&client_secret=YOUR_API_SECRET'
Replace YOUR_API_KEY
and YOUR_API_SECRET
with your actual Mailchimp API key and secret.
2. Get a list of subscribers
To retrieve a list of subscribers, you can use the GET /lists/{list_id}/members
endpoint. Here's an example:
curl -X GET \
https://api.mailchimp.com/v3.0/lists/YOUR_LIST_ID/members \
-H 'Authorization: Bearer YOUR_API_KEY'
Replace YOUR_LIST_ID
with the ID of the list you want to retrieve subscribers for, and YOUR_API_KEY
with your actual Mailchimp API key.
3. Add a new subscriber
To add a new subscriber to a list, you can use the POST /lists/{list_id}/members
endpoint. Here's an example:
curl -X POST \
https://api.mailchimp.com/v3.0/lists/YOUR_LIST_ID/members \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"email_address": "[email protected]", "status": "subscribed"}'
Replace YOUR_LIST_ID
with the ID of the list you want to add the subscriber to, and YOUR_API_KEY
with your actual Mailchimp API key.
4. Update a subscriber
To update a subscriber, you can use the PATCH /lists/{list_id}/members/{member_id}
endpoint. Here's an example:
curl -X PATCH \
https://api.mailchimp.com/v3.0/lists/YOUR_LIST_ID/members/MEMBER_ID \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"email_address": "[email protected]", "status": "subscribed"}'
Replace YOUR_LIST_ID
with the ID of the list the subscriber is in, MEMBER_ID
with the ID of the subscriber you want to update, and YOUR_API_KEY
with your actual Mailchimp API key.
5. Delete a subscriber
To delete a subscriber, you can use the DELETE /lists/{list_id}/members/{member_id}
endpoint. Here's an example:
curl -X DELETE \
https://api.mailchimp.com/v3.0/lists/YOUR_LIST_ID/members/MEMBER_ID \
-H 'Authorization: Bearer YOUR_API_KEY'
Replace YOUR_LIST_ID
with the ID of the list the subscriber is in, MEMBER_ID
with the ID of the subscriber you want to delete, and YOUR_API_KEY
with your actual Mailchimp API key.
These are just a few examples of what you can do with the Mailchimp API using curl
. Be sure to check out the Mailchimp API documentation for more information on available endpoints and parameters.