PostGrid Node.js Client

0.11.0 · active · verified Tue Apr 21

The `postgrid-node-client` library provides a Node.js and TypeScript client for interacting with the PostGrid Business API, facilitating postal delivery services (Print-Mail) and address verification/autocomplete. Currently at version 0.11.0, this package offers a convenient, typed interface over PostGrid's dual REST API structure. A key differentiator is its robust handling of two distinct API keys required by PostGrid: one for Print-Mail and another for Address Verification, allowing developers to instantiate a single client that intelligently routes requests. While a specific release cadence isn't published, its pre-1.0 versioning implies active development. The client simplifies common tasks such as creating contacts, sending documents, and verifying addresses, abstracting away direct HTTP requests and JSON parsing, and supports webhook configuration for event notifications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the PostGrid client with both Mail and Address Verification API keys (retrieved from environment variables) and then perform basic operations: creating a contact and verifying an address. It also shows optional webhook configuration.

import { PostGrid } from 'postgrid-node-client';

// For demonstration, use environment variables for API keys.
// In a real application, consider a secure configuration manager.
const mailApiKey = process.env.POSTGRID_MAIL_API_KEY ?? 'YOUR_MAIL_API_KEY';
const addrApiKey = process.env.POSTGRID_ADDR_API_KEY ?? 'YOUR_ADDR_API_KEY';

// Instantiate the client with both Mail and Address Verification API keys.
// It's not necessary to supply both if you only use one service.
const client = new PostGrid({
  mail: mailApiKey,
  addr: addrApiKey
}, {
  // Optional: Configure webhook details if your application processes PostGrid webhooks
  webhookUrl: 'https://my.service.com/postgrid/callback',
  webhookSecret: 'abc123456the-tall-brown-bear',
  webhookEvents: ['letter.created', 'letter.updated']
});

async function runExample() {
  try {
    console.log('Attempting to create a contact...');
    const newContact = await client.contacts.create({
      first_name: 'John',
      last_name: 'Doe',
      address: {
        address_line1: '123 Main St',
        city: 'Anytown',
        state: 'CA',
        zip: '90210',
        country: 'US'
      },
      email: 'john.doe@example.com',
      phone_number: '+15551234567'
    });
    console.log('Contact created successfully:', newContact.id);

    // Example of Address Verification (requires addr API key)
    console.log('\nAttempting Address Verification...');
    const verifiedAddress = await client.addresses.verify({
      address_line1: '123 Main St',
      city: 'Anytown',
      state: 'CA',
      zip: '90210',
      country: 'US'
    });
    console.log('Address Verification result:', verifiedAddress.success ? 'Verified' : 'Failed');

  } catch (error: any) {
    console.error('Error interacting with PostGrid:', error.message);
    if (error.response?.data) {
      console.error('API Response Data:', error.response.data);
    }
  }
}

runExample();

view raw JSON →