Created
June 11, 2015 10:17
-
-
Save freyr/ff43157b80fa761d48a5 to your computer and use it in GitHub Desktop.
GetResponse APIv2 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<meta http-equiv="Content-Language" content="en" /> | |
<meta name="Author" content="Pawel Pabian, Pawel Nejczew http://implix.com" /> | |
<title>Demonstrates how to add new contact to campaign.</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script type="text/javascript"> | |
// your API key is available at | |
// https://app.getresponse.com/my_api_key.html | |
var api_key = 'ENTER_YOUR_API_KEY_HERE'; | |
// API 2.x URL | |
var api_url = 'http://api2.getresponse.com'; | |
function add_contact() { | |
var campaigns = {}; | |
// find campaign named 'test' | |
$.ajax({ | |
url : api_url, | |
data : JSON.stringify({ | |
'jsonrpc' : '2.0', | |
'method' : 'get_campaigns', | |
'params' : [ | |
api_key, | |
{ | |
// find by name literally | |
'name' : { 'EQUALS' : 'test' } | |
} | |
], | |
'id' : 1 | |
}), | |
type : 'POST', | |
contentType : 'application/json', | |
dataType : 'JSON', | |
crossDomain : true, | |
async : false, | |
success : function(response) { | |
// uncomment following line to preview Response | |
// alert(JSON.stringify(response)); | |
campaigns = response.result; | |
} | |
}); | |
// because there can be only (too much HIGHLANDER movie) one campaign of this name | |
// first key is the CAMPAIGN_ID required by next method | |
// (this ID is constant and should be cached for future use) | |
var CAMPAIGN_ID; | |
for(var key in campaigns) { | |
CAMPAIGN_ID = key; | |
break; | |
} | |
$.ajax({ | |
url : api_url, | |
data : JSON.stringify({ | |
'jsonrpc' : '2.0', | |
'method' : 'add_contact', | |
'params' : [ | |
api_key, | |
{ | |
// identifier of 'test' campaign | |
'campaign' : CAMPAIGN_ID, | |
// basic info | |
'name' : 'Test', | |
'email' : '[email protected]', | |
// custom fields | |
'customs' : [ | |
{ | |
'name' : 'likes_to_drink', | |
'content' : 'tea' | |
}, | |
{ | |
'name' : 'likes_to_eat', | |
'content' : 'steak' | |
} | |
] | |
} | |
], | |
'id' : 2 | |
}), | |
type : 'POST', | |
contentType : 'application/json', | |
dataType : 'JSON', | |
crossDomain : true, | |
async : false, | |
success : function(response) | |
{ | |
// uncomment following line to preview Response | |
// alert(JSON.stringify(response)); | |
alert('Contact added'); | |
} | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<p> | |
<a href="#" onclick="add_contact(); return false;">Add contact</a> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lately APIv2 removed CORS headers negotiation (disabled OPTIONS Request). Unfortunately this renders this script useless. I will keep you posted if something changes.