New API Key is not working for Add Contact

I’m using my new API key. It’s very simple C# code. It never adds the contact. No error message either. Anybody know what might be wrong here? See the code below.

string TableName = “UserDetails”;
string FirstName = this.FirstNameTextBox.Text;
string LastName = this.LastNameTextBox.Text;
string _HeardAboutUs = iHeardAboutUsThrough;
string CompanyName = this.txtCompanyName.Text;
string SearchEngineValue = SearchEngineTerm;
string NumberOfAdvisors = this.drpAdviserTeamCount.Text;
string Email = UserName;
string PhoneNum = “None”;
string _AdvisorOrIndividual = UserVersion;
string _IsFreeTrial = “Yes”;
//string IPAddress = IPAddress;
DateTime tempdate = DateTime.Now;
string stringdate = DateTime.Now.ToString(“yyyyMMdd”);

            var xmlContent = "<?xml version='1.0' encoding='UTF-8'?>\r\n<methodCall>\r\n  "
                //+"<methodName>ContactService.addToGroup</methodName>\r\n  "
                                + "<methodName>ContactService.add</methodName>\\r\\n  "
                                + "<params>\r\n   "
                                + " <param>\r\n      <value><string>" + key + "</string></value>\r\n    </param>\r\n   "
                                + " <param>\r\n      <value><int>" + TableName + "</int></value>\r\n    </param>\r\n   "
                                + "<param>\r\n      <value><struct>\r\n   "
                                + "<member><name>FirstName</name>\r\n <value><string>" + FirstName + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>LastName</name>\r\n <value><string>" + LastName + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_HeardAboutUs</name>\r\n <value><string>" + _HeardAboutUs + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>Company</name>\r\n <value><string>" + CompanyName + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_SearchEngineTerm</name>\r\n <value><string>" + SearchEngineValue + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_NumberOfAdvisors</name>\r\n <value><string>" + NumberOfAdvisors + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>Email</name>\r\n <value><string>" + Email + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>Phone1</name>\r\n <value><string>" + PhoneNumber + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_AdvisorOrIndividual0</name>\r\n <value><string>" + _AdvisorOrIndividual + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_IsFreeTrial4</name>\r\n <value><string>" + _IsFreeTrial + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_IPAddress2</name>\r\n <value><string>" + IPAddress + "</string></value>\r\n  </member>\r\n  "
                                + "<member><name>_TrialStartDate1</name>\r\n <value><string>" + stringdate + "</string></value>\r\n  </member>\r\n  "
                                + " \r\n </struct></value>  </param>\r\n "
                                + "</params>\r\n</methodCall>\r\n\r\n";

            var content = new StringContent(xmlContent, null, "application/xml");

            request.Content = content;

            var task = Task.Run(async () =>
            {
                var response = await client.SendAsync(request);

                response.EnsureSuccessStatusCode();

                string responseContent = await response.Content.ReadAsStringAsync();

                Console.WriteLine(responseContent);

            });
            task.GetAwaiter().GetResult(); // Blocking call to wait for task completion

Here is an example for your guidance.

Just to explain a few things, there is a “Trim()” function at the end of the XML string. If there is any spaces before the “<?xml” entry point, the API will raise a parsing error.

The “{FirstName}”, “{LastName}”, and “{Email}” entries in the XML will require the matching String variables.
The other fields can easily be added if you follow the patten.

See how you go with the Date, as that may need to be formatted different if it does not appear correctly.

string xml = $"""
    <?xml version='1.0' encoding='UTF-8'?>
    <methodCall>
        <methodName>ContactService.add</methodName>
        <params>
            <param>
                <value>
                    <string></string>
                </value>
            </param>
            <param>
                <value>
                    <struct>
                        <member>
                            <name>FirstName</name>
                            <value>
                                <string>{FirstName}</string>
                            </value>
                        </member>
                        <member>
                            <name>LastName</name>
                            <value>
                                <string>{LastName}</string>
                            </value>
                        </member>
                        <member>
                            <name>Email</name>
                            <value>
                                <string>{Email}</string>
                            </value>
                        </member>
                    </struct>
                </value>
            </param>
        </params>
    </methodCall>
""".Trim();

Thanks, we got it to work finally. There was an xml error. I am now simply trying to set a tag. It doesn’t work. My code is below. Any idea why this wouldn’t work?

            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, "https://api.infusionsoft.com/crm/xmlrpc");

            key = "My Key";
            request.Headers.Add("X-Keap-API-Key", key);

int TestContactID = 127430;

                xmlContent = "<?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>" + key + "</string></value>\r\n" +
                 "    </param>\r\n" +
                 "    <param>\r\n" +
                 "      <value><struct>\r\n" +
                 "        <member><name>contactIDNumber</name>\r\n" +
                 "          <value><int>" + TestContactID + "</int></value>\r\n" +
                 "        </member>\r\n" +
                 "        </member>\r\n" +
                "         <member><name>tagIDNumber</name>\r\n" +
                "          <value><int>" + FreeTrialTagID + "</int></value>\r\n" +
                "        </member>\r\n" +
                 "      </struct></value>\r\n" +
                 "    </param>\r\n" +
                 "  </params>\r\n" +
                 "</methodCall>\r\n";

                content = new StringContent(xmlContent, null, "application/xml");

                request.Content = content;
                response = client.SendAsync(request).GetAwaiter().GetResult();

                response.EnsureSuccessStatusCode();

                responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

There is “”, “”, and “” elements in your request which should not be there.

Documentation: xml-rpc - Keap Developer Portal

You will find my version below.

string xml = $"""
    <?xml version='1.0' encoding='UTF-8'?>
    <methodCall>
        <methodName>ContactService.addToGroup</methodName>
        <params>
            <param>
                <value>
                    <string></string>
                </value>
            </param>
            <param>
                <value><int>{ContactID}</int></value>
            </param>
            <param>
                <value><int>{TagID}</int></value>
            </param>    
        </params>
    </methodCall>
""".Trim();