I would like sample C# code that shows how to use the new API key to set a tag for a contact.
@Doug_Carey I would suggest downloading the Postman app, and if you can build out the request there it can give you the code you’re looking for.
My C# skills are not up with my PHP skills, but I was able to generate the request in Postman, and then I can copy the provided code and modify as needed.
This is the code I took from Postman and it may help point you in the right direction:
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.infusionsoft.com/crm/xmlrpc/v1");
request.Headers.Add("X-Keap-API-Key", "YOUR_KEAP_SAK_GOES_HERE");
var content = new StringContent("<?xml version='1.0' encoding='UTF-8'?>\r\n<methodCall>\r\n <methodName>ContactService.addToGroup</methodName>\r\n <params>\r\n <param>\r\n <value><string>YOUR_KEAP_SAK_GOES_HERE</string></value>\r\n </param>\r\n <param>\r\n <value><int>CONTACT_ID_GOES_HERE</int></value>\r\n </param>\r\n <param>\r\n <value><int>TAG_ID_GOES_HERE</int></value>\r\n </param>\r\n </params>\r\n</methodCall>\r\n\r\n", null, "application/xml");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
To all C# developers,
If you do a search in the forum you will find old C# code examples. Some of the forum posts refer to the EventDay C# Wrapper, unfortunately it has not been updated for 6 years now, and will be based on the Legacy API Key.
@TomScott, all the SDK Examples include the C# one are several years old and are based on the Legacy API Key. They all need to be updated to use the Service Account Key. I am sure other developers will be raising questions on how to do the necessary changes in other programming languages.
The only one Keap owns is the infusionsoft-php
one. The other ones are community driven. We are trying to progress to a model where we publish SDKs for many languages but we are not there yet.
@bradb, as a suggestion, I recommend that you remove the Code Sample references from the documentation, otherwise it will confuse developers.
Marion: Thanks, this worked! Using a similar idea, how do I retrieve the Contact_ID using the contact’s email address?
Give this a try…
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.infusionsoft.com/crm/xmlrpc/v1");
request.Headers.Add("X-Keap-API-Key", "YOUR_KEAP_SAK_GOES_HERE");
var content = new StringContent("<?xml version='1.0' encoding='UTF-8'?>\r\n<methodCall>\r\n <methodName>ContactService.findByEmail</methodName>\r\n <params>\r\n <param>\r\n <value><string>YOUR_KEAP_SAK_GOES_HERE</string></value>\r\n </param>\r\n <param>\r\n <value><string>SEARCH_FOR_THIS_EMAIL</string></value>\r\n </param>\r\n <param>\r\n <value><array>\r\n <data>\r\n <value><string>Id</string></value>\r\n <value><string>FirstName</string></value>\r\n <value><string>LastName</string></value>\r\n </data>\r\n </array></value>\r\n </param>\r\n </params>\r\n</methodCall>", null, "application/xml");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
Thanks. Where are you finding method calls such as ContactService.findByEmail
? Where do I see the documentation for method calls to use?
It’s listed in the Documenation:
https://developer.infusionsoft.com/docs/xml-rpc/
I just copied the XML, pasted it into Postman, and made the request, and then extracted the C# code.
Thanks again! Do you know if there is a way to test which calls will fail and what will succeed once Keap moves to the new authentication key?
Keap has stated that any requests to the XMLRPC using the legacy API key is all that will fail.
Provided that you’re using a PAT or SAK and accessing the XMLRPC w/ the new URL endpoint and header requirement, nothing should fail.
Any OAuth requests are unaffected.
Thanks. I believe I can use my current code and just insert the new key. But when I do that, I get an error of “Unauthorized” when calling findByEmail. Example code is below. The variable key is my new key. I did get this new key to work when I used your sample code from before so the key is valid. Any idea what might be wrong here?
string url = “https://api.infusionsoft.com/crm/xmlrpc/v1”;
((XmlRpcClientProtocol)proxy).Url = url;
((XmlRpcClientProtocol)proxy).Headers[“Authorization”] = "Bearer " + key;
//Create a struct to hold the contact records data
try
{
email = UserName;
XmlRpcStruct conDat = new XmlRpcStruct();
iFace proxysignup = XmlRpcProxyGen.Create<iFace>();
XmlRpcStruct[] contacts = proxysignup.findByEmail(key, email, new[] { "Id", "FirstName", "LastName" });
That authorization would be for the OAuth. The SAK uses a different header:
rs["X-Keap-API-Key"] = key;
Thanks. I thought that would work, but I still get “Unauthorized” when calling findByEmail. My code is below.
iFace proxy = XmlRpcProxyGen.Create();
//Make sure our text boxes are not null.
string url = "https://api.infusionsoft.com/crm/xmlrpc/v1";
((XmlRpcClientProtocol)proxy).Url = url;
((XmlRpcClientProtocol)proxy).Headers["X-Keap-API-Key"] = key;
//Create a struct to hold the contact records data
try
{
//SetTagForContact2(1, 1);
email = UserName;
XmlRpcStruct conDat = new XmlRpcStruct();
iFace proxysignup = XmlRpcProxyGen.Create<iFace>();
XmlRpcStruct[] contacts = proxysignup.findByEmail(key,email, new[] { "Id", "FirstName", "LastName" });
Hi @Doug_Carey,
Interesting, someone else reported the same issue a few years ago in this forum post, but no one replied to it.
Have you tried calling any other API method? For example, what happens if you query the Contacts?
Is this a Service Account Key or Personal Access Token?
@Doug_Carey, I checked out Marion code and it worked all okay using the Service Account Key.
Here is a revision of the code with the XML expanded. Note, I am not a C# programmer either.
Make sure you set the “key” and “email” strings.
string key = "**** SERVICE ACCOUNT KEY ****";
string email = "**** EMAIL ADDRESS ****";
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.infusionsoft.com/crm/xmlrpc/v1");
request.Headers.Add("X-Keap-API-Key", key);
string xml = $"""
<?xml version='1.0' encoding='UTF-8'?>
<methodCall>
<methodName>ContactService.findByEmail</methodName>
<params>
<param>
<value>
<string></string>
</value>
</param>
<param>
<value>
<string>{email}</string>
</value>
</param>
<param>
<value>
<array>
<data>
<value>
<string>Id</string>
</value>
<value>
<string>FirstName</string>
</value>
<value>
<string>LastName</string>
</value>
<value>
<string>Email</string>
</value>
</data>
</array>
</value>
</param>
</params>
</methodCall>
""";
request.Content = new StringContent(xml, null, "application/xml");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
@Doug_Carey I’m guessing that the XmlRpcClientProtocol class isn’t passing the additional header as intended because that custom header is outside of the XMLRPC standard.
I would guess that either you have to pass ALL of the headers, or use the long form HttpClient request.
((XmlRpcClientProtocol)proxy).Headers["X-Keap-API-Key"] = key;
((XmlRpcClientProtocol)proxy).Headers["Content-Type"] = "application/xml";
I added ((XmlRpcClientProtocol)proxy).Headers[“Content-Type”] = “application/xml”; and I get the same error. I was told by Keap that all of my existing code should work using the new key. But it’s not working. Is there an API team at Keap I can talk to about this?
You can submit a ticket here:
https://developer.infusionsoft.com/get-support/
@Doug_Carey, it looks like you are using Cook Computing XML-RPC Library for the XML requests.
If you follow the examples given, it should be a straight forward change to the URL and just add the Header for the submission. But, it looks like something else may need to be altered in your code to make it fully work.
Keap has recently updated their Legacy Key Migration with a C# example, but is a simple example of what needs to be changed.
https://developer.infusionsoft.com/legacy-key-migration/
Unfortunately there is a lack of C# developers around in this forum.