Print and Mail a Letter with Ruby

NOTE: This example was created on an outdated version of the Lob API. Please see the latest Lob documentation for the most up-to-date information on integrating with Lob.

This is a quickstart guide on how to use Lob’s Letter API to print and mail a physical letter in Ruby. I’ll be showing you the bare minimum needed to get up and running quickly, so let’s get started.

Grab an API Key

First things first, go ahead and Register for an API key. Its free to sign up and use our APIs in sandbox mode.

Once you are ready to start sending real letters, just add a payment method and use your Live API Key. Check out the letter pricing for more details on how much things cost.

Install the Ruby Gem

We’ve created a Ruby Gem to get you up and running quickly.

For more details on the Lob Ruby Gem, make sure to check out the GitHub Repository: https://github.com/lob/lob-ruby

Include and Initialize

Make sure to update test_0000000000000000000000000 with your own API Key. You can find yours in the Dashboard

Create a From/To Address

This is fairly straight forward, just use the Lob Gem to create a From and a To address. Check out the Documentation for more information on creating addresses.

Create the Letter

All our APIs accept content in the form of PDF, JPEG, PNG, and HTML/CSS. By far the most powerfull option is to use HTML/CSS so that you can use template variables and create dynamic content. Notice the {{name}} variable in the letter template below:

Makes sure to read the docs for more letter template examples.

Send the Letter

Last, but not least, put it all together to send the letter. Take a look at the quick descriptions of the fields below, but make sure to read the full letter documentation for all the Letter API options.

  • description: Used for identification purposes in the dashboard. This does not get printed on the letter.
  • to: ID of the TO address
  • from: ID of the FROM address
  • file: HTML string from our letter template
  • data: Template variables passed as a keyed array
  • metadata: Tag your own meta data to easily track and search letters
  • color: Designate if the letter is Color or B&W

Check the Dashboard

Login to the Dashboard and check to make sure that your letter shows up. From here you can see things like the letter preview, expected delivery date, and progress through the mail.

Next Steps

Here are some more resources to help get you started: