A beginner's guide to using the Lob API

If you're looking to integrate postcard, letter, and check printing into your application, the Lob API is a great place to start! With a variety of RESTful endpoints to choose from, it's likely that you'll find everything you need to build a powerful tool.

In this blog post, we'll walk through some API examples in JavaScript so you can get started with the Lob API.

Getting started with the Lob API

Before you can start using the Lob API, you'll need to sign up for an account. You can create an account for free on the Lob website.

Once you've created your account, you'll need to head over to the API dashboard to create a new API key.

With your API key in hand, you can start making requests to the Lob API.

Sending a postcard

One of the most popular use cases for the Lob API is to send postcards. Here's an example of how you might do that using JavaScript:

const Lob = require('lob');

// Initialize the client with your API key
const lob = new Lob('YOUR_API_KEY');

// Create the address object
const address = {
  name: 'John Smith',
  address_line1: '123 Main St',
  address_city: 'Mountain View',
  address_state: 'CA',
  address_zip: '94041',
  address_country: 'US',
};

// Create the postcard object
const postcard = {
  to: address,
  from: {
    name: 'Your Name',
    address_line1: '456 Another St',
    address_city: 'Portland',
    address_state: 'OR',
    address_zip: '97209',
    address_country: 'US',
  },
  front: '<html style="padding: 1in;">Front of postcard</html>',
  back: '<html style="padding: 1in;">Back of postcard</html>',
};

// Send the postcard
lob.postcards.create(postcard, function (err, res) {
  if (err) {
    console.log(err);
  } else {
    console.log(res);
  }
});

This code uses the postcards.create endpoint to send a postcard. You simply need to provide the to and from address, as well as the HTML for the front and back of the postcard.

Printing a letter

If you need to send a more formal message, you might opt to print and send a letter instead of a postcard. Here's an example of how you might use the Lob API to print and send a letter:

const Lob = require('lob');

// Initialize the client with your API key
const lob = new Lob('YOUR_API_KEY');

// Create the address and letter objects
const address = {
  name: 'John Smith',
  address_line1: '123 Main St',
  address_city: 'Mountain View',
  address_state: 'CA',
  address_zip: '94041',
  address_country: 'US',
};

const letter = {
  to: address,
  from: {
    name: 'Your Name',
    address_line1: '456 Another St',
    address_city: 'Portland',
    address_state: 'OR',
    address_zip: '97209',
    address_country: 'US',
  },
  file: '<html>Letter contents</html>',
  color: true,
  double_sided: true,
};

// Print and send the letter
lob.letters.create(letter, function (err, res) {
  if (err) {
    console.log(err);
  } else {
    console.log(res);
  }
});

This code uses the letters.create endpoint to print and send a letter. You supply the to and from address, as well as the HTML for the letter contents.

Depositing a check

Finally, the Lob API can also be used to deposit a check. Here's an example of how you might send a check and then deposit it:

const Lob = require('lob');

// Initialize the client with your API key
const lob = new Lob('YOUR_API_KEY');

// Create the check object
const check = {
  bank_account: 'bank_00000000000000',
  to: {
    name: 'John Smith',
    address_line1: '123 Main St',
    address_city: 'Mountain View',
    address_state: 'CA',
    address_zip: '94041',
    address_country: 'US',
  },
  amount: 200,
  memo: 'Test check',
  message: 'This is only a test',
};

// Send the check
lob.checks.create(check, function (err, res) {
  if (err) {
    console.log(err);
  } else {
    console.log(res);

    // Deposit the check
    lob.checks.createDeposit(res.id, function (err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res);
      }
    });
  }
});

This code uses the checks.create endpoint to send a check. After the check is sent, the checks.createDeposit endpoint is used to deposit the check.

Wrapping up

Using the Lob API is a great way to add postcard, letter, and check printing to your application. With a little bit of JavaScript, you can send postcards, print letters, and even deposit checks! Be sure to check out the Lob API documentation for additional endpoints and options.

Related APIs