Hello Bronto!

Years (or perhaps months) ago, you might remember writing your first program and having it print out “Hello World!”. In a similar fashion, this section’s goal is to help you write your first client to the Bronto API. These simple example clients connect to the API, authenticate, add an example contact, add an example list, place the contact on the list, and then read back to the content for display to verify the operation.

Before you begin, remember that you will need an API token set up in your account with both reading and writing permissions. More information on API tokens can be found here.


Code Samples

<?php
 
/*  
This example script will connect to the API, authenticate a session, 
add a new contact, add a new list, update the contact to be on the 
list, and then read the contact to verify that they are on the list. 
Be sure to replace the "ADD YOUR API TOKEN HERE" text with a working 
API Token.
*/
 
ini_set("soap.wsdl_cache_enabled", "0");
date_default_timezone_set('America/New_York');
 
$wsdl = "https://api.bronto.com/v4?wsdl";
$url = "https://api.bronto.com/v4";
 
$client = new SoapClient($wsdl, array('trace' => 1, 'encoding' => 'UTF-8'));
$client->__setLocation($url);
 
// Login
$token = "ADD YOUR API TOKEN HERE";
$sessionId = $client->login(array("apiToken" => $token))->return;
$client->__setSoapHeaders(array(new SoapHeader("http://api.bronto.com/v4", 
                                               'sessionHeader',
                                               array('sessionId' => $sessionId))));
 
// Add our new contact
$contact = array(
  array("email" => "john.doe@example.com")
  );
$result = $client->addContacts(array("contacts" => $contact))->return->results;
if ($result->isError) {
  echo "Unable to add new contact!\n";
  return;
}
echo "Added new contact.\n";
 
// Add our new list
$list = array(
    array("name" => "My first list", "label" => "Hello Bronto List")
  );
$result = $client->addLists(array("lists" => $list))->return->results;
if ($result->isError) {
  echo "Unable to add new list!\n";
  return;
}
$listId = $result->id;
echo "Added new list.\n";
 
 
// Update the contact to put him on the list
$contact = array(
    array("email" => "john.doe@example.com", "listIds" => array($listId))
  );
$result = $client->updateContacts(array("contacts" => $contact))->return->results;
if ($result->isError) {
  echo "Unable to update contact!\n";
  return;
}
echo "Added contact to list.\n";
 
 
// Verify the contact is on the list
$filter = array(
  "email" => array(array("operator" => "EqualTo", "value" => "john.doe@example.com"))
);
 
$result = $client->readContacts(array(
    "filter" => $filter, 
    "includeLists" => true,
    "fields" => null, 
    "pageNumber" => 1
    ))->return;
if ($result->listIds != $listId) {
  echo "Contact is not on the list!\n";
  return;
}
 
echo "Contact is on the list.\n";
?>

import sys
import logging
from suds.client import Client
from suds import WebFault
 
"""
This example script completes the following tasks:
1. Login to the API
2. Obtain a session ID
3. Add a contact
4. Add a list
5. Update the contact you just created and add them
   to the list you just created.
6. Read back the contact's data to ensure they were
   properly updated.
 
BE SURE TO REPLACE ALL PLACEHOLDER TEXT
 
Tested with Python 2.6.1 and suds soap library version 0.4
 
See suds home page:
https://fedorahosted.org/suds/
 
"""
 
# Bronto API WSDL
BRONTO_WSDL = 'https://api.bronto.com/v4?wsdl'
 
# Start up basic logging
logging.basicConfig()
 
# Replace the placeholder text with a valid
# API token
TOKEN = "ADD TOKEN HERE"
 
 
# Login using the token to obtain a session ID
bApi = Client( BRONTO_WSDL )
 
try:
  session_id = bApi.service.login(TOKEN)
# Just exit if something goes wrong
except WebFault, e:
   print '\nERROR MESSAGE:'
   print e
   sys.exit()
 
# Set up the soap headers using the
# session_id obtained from login()
session_header = bApi.factory.create("sessionHeader")
session_header.sessionId = session_id
bApi.set_options(soapheaders=session_header)
 
# Adding a contact
# Be sure to replace the placeholder text with a
# real email address!
 
contact = bApi.factory.create('contactObject')
contact.email = 'SOME EMAIL ADDRESS'
 
try:
  add_contact = bApi.service.addContacts(contact)
  if add_contact.results[0].isError == True:
    print 'There was an error with your request:'
    print add_contact.results[0]
    sys.exit()
  else:
    print 'A contact has been added with the id: ' + add_contact.results[0].id
except WebFault, e:
  print '\nERROR MESSAGE:'
  print e
  sys.exit()
 
# Add a list
# Be sure to replace the placeholder text with a
# real list name and label!
 
list = bApi.factory.create('mailListObject')
list.name = 'example_list_name'
list.label = 'Example List'
 
try:
  add_list = bApi.service.addLists(list)
  if add_list.results[0].isError == True:
    print 'There was an error with your request:'
    print add_list.results[0]
    sys.exit()
  else:
    print 'A list has been created with an id of: ' + add_list.results[0].id
except WebFault, e:
  print '\nERROR MESSAGE:'
  print e
  sys.exit()
 
# Update the contact you just created contact to be 
# on the list you just created list
 
update_contact = bApi.factory.create('contactObject')
update_contact.id = add_contact.results[0].id
update_contact.listIds = add_list.results[0].id
 
try:
  updated_contact = bApi.service.updateContacts(update_contact)
  if updated_contact.results[0].isError == True:
    print 'There was an error with your request:'
    print updated_contact.results[0]
    sys.exit()
  else:
    print 'The contact with an id of: ' + updated_contact.results[0].id + ' has been updated.'  
except WebFault, e:
  print '\nERROR MESSAGE:'
  print e
  sys.exit()
 
# Read back the contact data
 
filter = bApi.factory.create('contactFilter')
 
filterType = bApi.factory.create('filterType')
 
filter.id = add_contact.results[0].id
filter.type = filterType.AND
 
# Call readContacts. Return all the data for the contact
try:
  read_contact = bApi.service.readContacts(filter, includeLists = True, pageNumber = 1)
  for contact in read_contact:
    print contact
except WebFault, e:
  print '\nERROR MESSAGE:'
  print e
  sys.exit()


Was this article helpful?

Not HelpfulHelpful (+2 rating, 4 votes)
Loading...

We welcome your feedback!

Please let us know how this article could be improved.
*Name and Email are optional fields.