Connecting to Pardot using Web Services

Code

Here is the source code for the Pardot example. To use it:

  1. Create a new channel and copy paste the code below into main().
  2. Create a new shared module called “node” and copy paste in the code below.
  3. Add the code require(‘urlcode’) and require(‘node’) at the top of the main module.
  4. Follow this urlcode example to link in the urlcode module.
  5. Test using sample data.

Code for main:

require("urlcode")
require("node")

function main(Data)
   local msg, name = hl7.parse({vmd = 'demo.vmd', data = Data})
   local A={email='----------------------------',  -- your email in Pardot
      password='----------',                       -- your Pardot password
      user_key='--------------------------------'} -- your Pardot user_key
   local B = urlcode.encodeTable(A)

   -- retrieve an "API key"
   local R,C,H = net.http.post{
      live=true, -- for testing
      url='https://pi.pardot.com/api/login/version/3',
      body=B}
   --parameters=A} -- parameters also work
   local X=xml.parse{data=R}
   local ApiKey=X.rsp.api_key[1]:nodeValue()

   -- read prospects using POST as recommended by Pardot
   A = {api_key=ApiKey, user_key='--------------------------------',-- your Pardot user_key
      assigned_to_user='----------------------------',limit=10}     -- your email in Pardot
   B = urlcode.encodeTable(A)
   local D=net.http.post{
      live=true,
      url='https://pi.pardot.com/api/prospect/version/3/do/query',
      body=B}
      --parameters=A}-- parameters also work
   local X=xml.parse{data=D}

   -- you can also read by using GET
   A = {api_key=ApiKey, user_key='--------------------------------',-- your Pardot user_key
      assigned_to_user='----------------------------',limit=10}     -- your email in Pardot
   local D=net.http.get{
      live=true,
      url='https://pi.pardot.com/api/prospect/version/3/do/query',
      parameters=A}
   local X=xml.parse{data=D}

   -- create a new prospect in Pardot
   local firstName=msg.PID[5][1][2]:nodeValue()
   local lastName=msg.PID[5][1][1][1]:nodeValue()
   local Email=msg.PID[13][1][4]:nodeValue()
    A = {api_key=ApiKey, user_key='-------------------------------',-- your Pardot user_key
      email=Email, -- required, must not exist in Pardot
      first_name=firstName,last_name=lastName} -- optional
   B = urlcode.encodeTable(A)
   local D=net.http.post{
      live=true,
      url='https://pi.pardot.com/api/prospect/version/3/do/create',
      body=B}
--      parameters=A}-- parameters also work
   local X=xml.parse{data=D}
end

Code for node module:

function node.S(ANode)
   return tostring(ANode)
end

Possible modifications interesting notes etc:

  • You need to change the login details (“——————–” in main), get the user_key from MySettings in Pardot.
  • Gotcha: I limited the the rows returned to 10 for a quick response (note: the default of 200 causes http timeout errors).
  • Pardot recommends passing parameters in the POST message body (see authentication and using the API), this requires url encoding. If you want to avoid the extra step of encoding you can use GET/POST parameters.
  • Pardot hides useful examples at the bottom of the using pages for Prospects, Users etc – so be sure to scroll down…