Lasso Soft Inc. > Home


AuthorJason Huck
LicensePublic Domain
Posted23 Feb 2009
Updated14 Nov 2011
More by this author...


This custom type provides a Lasso interface to the MailChimp API. You can choose to use JSON or XML as the output format. Requires a valid API key (check here once you have an account) For JSON, [encode_json] and [decode_json] are required, and the results are returned as Lasso-native data structures (maps and arrays). For XML, [xml_tree] is required, and the results are returned as an XML Tree object. For each method listed in the API documentation (, use the method name as a member tag, and submit the other parameters as standard keywords. See below for examples.

Sample Usage

// include the required tags/types

// create a new mailchimp object
var('mychimp') = mailchimp( -key='YOUR_API_KEY_HERE', -output='json');

// ping the mailchimp server
// returns: "Everything's Chimpy!"

// list all campaigns in your account	
// returns an array of maps containing campaign data

// get the ID of the first mailing list associated
// with your account
// returns an alphanumeric ID

// add a new subscriber to one of your mailing lists
// returns true on success, false otherwise

// get the id of the first campaign associated with
// your account - comes back as a quoted string, so
// it's necessary to strip the quotes before using
var('campaignID') = $mychimp->campaigns->first->find('id');

// send a test email from a specific campaign
// recipients are expected to be provided as an array 
// with a zero-based index, so we have have to fudge
// it just a little
var('recipients') = array(0 = '');

// returns true on success, otherwise false	
var('success') = $mychimp->campaignSendTest(


Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

	-description='Lasso wrapper for the MailChimp API.'
	// Lasso wrapper for MailChimp API
	// author: Jason Huck/Core Five Creative
	// requires xml_tree for XML output, or encode_json
	// and decode_json for JSON output
	// defaults to JSON output and version 1.1 of the API
		'apikey' = string,
		'output' = 'json',
		'url' = '',
		'version' = 1.1		

	// requires a valid API key to initialize
	// you can optionally set XML as the output format		
		self->'apikey' = #key;
		local_defined('output') && (: 'json', 'xml') >> #output ? self->'output' = #output;

	// the main function which makes the call to MailChimp
	// and retrieves the results (in raw form)	
		-req='method', -type='string',
		-req='params', -type='array', -copy,
		local('uri') = self->'url' + '/' + self->'version' + '/';
		#uri += '?output=' + self->'output' + '&method=' + #method;
		#params->insert('apikey' = self->'apikey');
			local('out') = include_url(
				-sendmimeheaders=(: 'User-Agent' = client_browser),
				local('out') = null;

	// the _unknowntag callback function lets us call any
	// API method by name without explicitly defining a
	// tag for each one	
	// NOTE: method names appear to be case-sensitive!	
		local('params') = array;
		// loop through the supplied params and strip the
		// leading slash from keywords
		// if an array is given as the value of a param,
		// explode it for use on the query string like so:
		// ?myparam[a]=A&myparam[b]=B&myparam[c]=C
			iterate(params, local('i'));
				local('n') = string(#i->first)->removeleading('-')&;
					iterate(#i->second, local('j'));
						local('k') = #n + '[' + #j->first + ']';
						local('v') = #j->second;
						#params->insert(#k = #v);
					#params->insert(#n = #i->second);
		// add the method name (the unknown tag) and grab
		// the response via the ->connect method
		local('response') = self->connect(tag_name, #params);
		// decode the response - JSON objects are converted
		// to native Lasso types, XML objects are returned
		// as XML_Tree objects, otherwise the raw response
		// is returned unaltered
		if(#response && (#response->beginswith('[') || #response->beginswith('{')) && self->'output' == 'json');
			#response = decode_json(#response);
		else(#response && #response->beginswith(''output' == 'xml');
			#response = xml_tree(#response);

Related Tags


14 Nov 2011, Jason Huck

Added User-Agent header.

Added a User-Agent header to API calls per request of MailChimp.

14 Nov 2011, Jason Huck

Bug fix.

Corrected an issue where the JSON response would not be decoded in many cases.

10 Mar 2009, Jason Huck

Updated to use POST.

Adjusted to use POST instead of GET to avoid length limits in generated URI's.

25 Feb 2009, Jason Huck

Minor Tweak

Adjusted the decoding to avoid null->beginswith errors if the remote call to MailChimp fails.

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home



©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft