Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript + MailChimp API subscribe

When making this request:

// Subscribe a new account holder to a MailChimp list
function subscribeSomeoneToMailChimpList()
{
  var options =
  {
    "apikey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "id": "xxxxxx",
    "email":
    {
      "email": "[email protected]"
    },
    "send_welcome": false
  };
  var mcSubscribeRequest = UrlFetchApp.fetch("https://us4.api.mailchimp.com/2.0/lists/subscribe.json", options);
  var mcListObject = Utilities.jsonParse(mcSubscribeRequest.getContentText());
}

This response is returned:

Request failed for https://us4.api.mailchimp.com/2.0/lists/subscribe.json returned code 500. Truncated server response: {"status":"error","code":-100,"name":"ValidationError","error":"You must specify a apikey value"} (use muteHttpExceptions option to examine full response) (line 120, file "v2")

Line 120 is the line on which UrlFetchApp.fetch is called.

The API key is valid (I have tested with simpler API calls that don't include associative arrays). When I append the API key directly to the base URL and remove it from the options, I get an error saying that the list ID is invalid. When I then append the list ID directly to the base URL and remove it from options, I get an error saying that the email address must be in associative array form.

My question is: Using the above format, how does one send requests that contain associative arrays?

The relevant API documentation can be found here.

like image 587
barryedmund Avatar asked Oct 30 '13 00:10

barryedmund


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.


1 Answers

Doing this in javascript exposes your API key to the world. If someone has your key, he/she can make changes to or gain access to your account.

like image 175
John Proestakes Avatar answered Oct 06 '22 08:10

John Proestakes