home pageabout moiagroupour servicesour workhow we workcontact usstuff we like

Using the NGP API to add email addresses

Many of our political clients use NGP to manage their email lists, member databases, and other data. NGP’s documentation is pretty limited, so I’m posting this demo of a very simple way to add email and zip code to a mailing list. Credit to New Media Campaigns, whose php demo for NGP’s contribution processing interface got me over the hump. You can see the NGP service interface here:


And you can download their docs here:


I’m using NuSoap here since that seems to be the easiest.

Update: Where’s my API code and all that? Just log into the MyNGP dashboard, click File –> Preferences –> API Preferences.

Client Code:



class NgpEmailSignupClient {

 public $credentialString;
 public $mainCode;
 public $campaignID;

 function NgpEmailSignupClient($credentialString, $mainCode, $campaignID) {
 $this->debug = false;
 $this->credentialString = $credentialString;
 $this->mainCode = $mainCode;
 $this->campaignID = $campaignID;

 function addEmail($email, $zip, $firstName = '', $lastName = '') {

 if (!$zip) $errs[] = "Zip code is required.";
 if (!$email) $errs[] = "Email is required.";
 if ( count($errs) ) {
 echo '<h1>Error</h1><ul>';
 foreach ($errs as $err) {
 echo '<li>'.$err.'</li>';
 echo '</ul>';
 // Create the XML string containing all our data
 // http://www.myngp.com/ngpapi/transactions/Email/ContactWithEmailSet.xsd
 $xml = '<?xml version="1.0" encoding="utf-8"?>';
 $xml .= "<contactWithEmailSet>";
 $xml .= "<contact>";
 $xml .= ( isset( $lastName ) && !empty( $lastName ) ) ? "<lastName>{$lastName}</lastName>" : '<lastName />';
 $xml .= ( isset( $firstName ) && !empty( $firstName ) ) ? "<firstName>{$firstName}</firstName>" : '<firstName />';
 $xml .= ( isset( $zip ) && !empty( $zip ) ) ? "<zip>{$zip}</zip>" : '<zip />';
 $xml .= ( isset( $email ) && !empty( $email ) ) ? "<email>{$email}</email>" : '<email />';
 $xml .= "</contact>";
 $xml .= "<mainCode>{$this->mainCode}</mainCode>";
 $xml .= "<optIn>1</optIn>";
 $xml .= "<campaignID>{$this->campaignID}</campaignID>";
 $xml .= "</contactWithEmailSet>";

 $client = new nusoap_client('http://www.myngp.com/ngpapi/APIService.asmx?wsdl', true) or
     die("Error instantiating SOAP client");

 // Do the call
 $params = array(
 'RequestXML' => $xml,
 'transType' => 'contactWithEmailSet',
 'credentialString' => $this->credentialString
 $result = $client->call('processRequestWithCreds', $params);

 if ($client->fault) {
 //echo $xml . "\n\n";
 return $err;
 } else {
 // Check for errors
 $err = $client->getError();
 if ($err) {
 //echo $xml . "\n\n";
 //echo $err;
 return $err;
 } else {
 return true;

Demonstration Usage

So you see in the part below where it says “THIS_IS_WHERE_YOUR_STRING_GOES”? You need to change this, and you need to change the next two lines after it too. You should be able to get the credentials you need from your NGP contact. Don’t try and use what you see below without adding your own strings!

// Each of these three values needs to be customized for your specific application.
$credentialString = "THIS_IS_WHERE_YOUR_STRING_GOES";
$mainCode = "WebEMAIL";
$campaignID = 1234;
// These values will presumably come from script or form input
// They should be validated before reaching this point
$email = 'sample@email.com';
$zip = '12345';
$ngpClient = new NgpEmailSignupClient($credentialString, $mainCode, $campaignID);
if (true === $ngpClient->addEmail($email, $zip)) {
 echo "Success!";
} else {
 echo "Failure!";


Download as a single zip file: Simple PHP Email Signup Demo

One Response to “Using the NGP API to add email addresses”

  1. Gravity Forms + NGP API for Payment Processing » MoiaGroup, Simply Great Web Design and Development in Tucson, Arizona Says:

    [...] Using the NGP API to add email addresses [...]