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

@quadient/switchmail-api-client

v0.5.0

Published

Node.js API client for switchmail.com, enables sending and tracking physical mails / letters. Uses axios client, exposes typescript definitions.

Downloads

2

Readme

Node.js API Client for Switchmail.com

This is a Node.js TypeScript package that provides an API client for Switchmail.com. It enables you to send and track physical mails / letters through the Switchmail.com platform.

Installation

To install this package, you can use npm:

npm install @quadient/switchmail-api-client

Usage

To use this API client, you need to have a Switchmail.com account and API key. You can sign up for an account at https://switchmail.com/.

Below is a TypeScript example of how to use this package (e.g. via npx ts-node example-code.ts):

import * as fs from "fs";
import * as path from "path";
import axios from "axios";
import { SwitchmailApi, Types } from '@quadient/switchmail-api-client';

const email = "YOUR_EMAIL";
const apiMasterKey = "YOUR_API_MASTER_KEY";
const baseURL = "https://api-test.switchmail.com"; // test environment / sandbox
// const baseURL = "https://api.switchmail.com"; // production
const documentFile = path.join(__dirname, "YOUR_FILE.pdf");

async function run() {
  const switchmailApi = new SwitchmailApi({ baseURL });

  console.log("- getting authorization token via API master key");
  const tokenResponse = await switchmailApi.auth.tokenCreate({ email }, { headers: { "x-api-key": apiMasterKey } });

  console.log("- creating document entry in AWS bucket");
  const uniqueDocumentName = `document-${new Date().getTime()}.pdf`;
  const documentsResponse = await switchmailApi.documents.documentsCreate(
    { documents: [{ fileName: uniqueDocumentName, fileType: "application/pdf" }] },
    { headers: { Authorization: tokenResponse.authorization } }
  );
  const documentEntry = (documentsResponse.documents as any as Types.DocumentForm[])[0];

  console.log("- uploading document content from file");
  await axios.postForm(documentEntry.form.url, {
    ...documentEntry.form.fields,
    file: fs.readFileSync(documentFile),
  });

  console.log("- creating letter(s) batch");
  const lettersResponse = await switchmailApi.letters.lettersCreate(
    {
      email,
      printing: { printColor: false, duplex: true, envelopSize: "Autoselect" } satisfies Types.PrintingOption,
      delivery: { emailService: "FIRSTCLASS", eReceipt: email } satisfies Types.DeliveryOption,
      sender: {
        firstName: "John",
        lastName: "DC",
        postalAddress: { address1: "RM-2244", city: "West Lake Hills", state: "TX", postcode: "78746" },
      } satisfies Types.Sender,
      recipients: [
        {
          documents: [{ documentId: documentEntry.documentId }],
          firstName: "Roger",
          lastName: "Fox",
          postalAddress: { address1: "47 Northside Ave", city: "Albany", postcode: "12084", state: "NY" },
        },
      ] satisfies Types.Recipient[],
    },
    { headers: { Authorization: tokenResponse.authorization, "X-Invocation-Type": "RequestResponse" } }
  );

  console.log("- confirming letter(s) batch sending");
  const { id, trackingId, transaction } = lettersResponse;
  await switchmailApi.letters.confirmCreate(
    lettersResponse.id,
    { id, trackingId, transaction },
    { headers: { Authorization: tokenResponse.authorization, "X-Invocation-Type": "RequestResponse" } }
  );

  console.log("[COMPLETED]");
}
run();

Pure JavaScript / Node.js execution can be performed by removing the types and modifying the imports in following manner.

const fs = require("fs");
const path = require("path");
const axios = require("axios");
const SwitchmailApi = require("@quadient/switchmail-api-client").SwitchmailApi;

// ... rest of the example code

API

This package provides the API methods which are described in more details on the following pages:

  • https://apidocs.switchmail.com/ (general overview and concepts)
  • https://developer.switchmail.com/apidoc/index.html (API reference)
  • https://developer.switchmail.com/apidoc/swagger.json (Swagger/OpenAPI specification)

Customizations

This client is mainly generated using the https://github.com/acacode/swagger-typescript-api, called via:

npm run generateApi

Feel free to customize and/or generate your own API client.

License

Licensed under the MIT License.