npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pardot-client

v1.1.0

Published

A client for Pardot API v3 and v4, with support for Salesforce SSO

Downloads

2,286

Readme

pardot-client

pardot-client is a NodeJS library for Pardot API v3 and v4, using Salesforce single-sign on, implemented in Typescript.

https://developer.salesforce.com/docs/marketing/pardot/overview

Installation

npm install pardot-client

Preparation

Before using this library, you must create a connected app in Salesforce and gather the necessary information, as documented at https://developer.salesforce.com/docs/marketing/pardot/guide/getting-started.html

Usage

Instantiate Client

Instantiate a new PardotClient object.

import PardotClient from 'pardot-client';

const pardotClientProps = {
  // OAuth client id (required)
  clientId: '',

  // OAuth client secret (required)
  clientSecret: '',

  // OAuth redirect URI (required)
  redirectUri: '',

  // Business unit id for the Pardot account (required)
  businessUnitId: '',

  token: {
    // OAuth token for the Pardot account (optional)
    access_token: '',
    refresh_token: '',
  },

  // Pardot API url (optional; default = 'pi.pardot.com')
  baseUrl: '',

  // Pardot API version (3 or 4) (optional; default = 4)
  apiVersion: 4,

  // Callback called when access token is refreshed (optional)
  refreshCallback: (token) => {},
};

const pardotClient = new PardotClient(pardotClientProps);

Implement OAuth flow

  1. Generate an authorize URL and direct the user to it.
  2. After the user signs in and allows access, they will be redirected to the redirect URI with a code parameter.
  3. Pass the code to getAccessToken() to request an access token.
const authorizeUrlProps = {
  // OAuth scope or scopes (optional)
  scope: [''],

  // OAuth state; will be passed back to the redirect URI (optional)
  state: '',
};

const authorizeUrl = pardotClient.authorizeUrl(authorizeUrlProps);

// direct user to authorize URL

// handle request to redirect URI with code parameter

const accessToken = pardotClient.getAccessToken(code);

// store access token locally for later reuse

Once the Pardot client object has an access token, either from calling getAccessToken() or from passing token to new, you are ready to make requests to the API!

Token refresh

The Pardot client object will automatically refresh the token when it expires. Use the refresh callback to store the updated token locally.

const pardotClient = new PardotClient({
  refreshCallback: async (token) => {
    // store access token locally
  },
});

Requests and Responses

All methods return a Promise that resolves to the object returned by the API.

const response = await pardotClient.prospects.query();

console.log(`Number of prospects: ${response.result.total_results}`);

For a successful request, the Pardot API returns a JSON response with the following structure, where the actual key property and the structure of ResponseData depend on the request:

interface SuccessResponse {
  '@attributes': {
    status: 'ok';
    version: number;
  };
  [key: string]: ResponseData;
}

For a failed request, the Pardot API returns a JSON response with the following structure:

interface ErrorResponse {
  '@attributes': {
    err_code: number;
    stat: 'fail';
    version: number;
  };
  err: string;
}

Currently, the Pardot client simply returns that error response. In the future, this may be changed to throw an error.

Requests are made using Axios, which throws an error if an HTTP error occurs.

Additional Usage Notes

Lists in Responses

Some responses may contain a list of items. If there would only be one item in the list, the Pardot API returns the item on its own not in a list. For example:

const responseMany = {
  result: {
    // an array of items
    prospect: [{ id: 1 }, { id: 2 }],
  },
};

const responseOne = {
  result: {
    // a single item, not in an array
    prospect: { id: 1 },
  },
};

This can occur at the top level of a query response, or nested within a response, such as for opportunity.opportunity_activities.visitor_activity. Currently, the Pardot client returns the response unchanged. In the future, this may be changed to wrap a single item in a list, for ease of use.

Querying dates

Some query methods allow querying based on a date, such as by specifying a value for created_before. The value can be one of the fixed strings 'today', 'yesterday', 'last_7_days', 'this_month' or 'last_month', or a date formatted using GNU Date Input Syntax.

Pardot Objects

The Pardot client has a sub-object for each type of Pardot object. For example, to perform operations on prospects, you would call methods on pardotClient.prospects. Refer to the documentation for each object for details.

License

MIT