Your API keys (you can find these in your Dashboard under Settings → API Keys)
Language setup guides
These brief sections are aimed at developers who are less familiar with the languages with which they will be working with Lob API. Currently, Lob provides client libraries in the following languages:
To make switching between Node versions easy, install nvm on your local machine, following the instructions in the official Node Version Manager GitHub repository. To get the latest LTS release, run:
// nvm install --lts
Alternatively, Windows users should install the latest release of nvm-windows by following the instructions in its home repository.
Run ruby -v to check whether it’s pre-installed. If not, run brew install ruby (make sure to install Homebrew first if you haven’t already). Add the path for your version of Ruby to your environment’s PATH variable.
If not, runbrew install php(make sure to install Homebrew first if you haven’t already). Add the path for your version of PHP to your environment’s PATH variable.
This Quickstart guide provides a code example of one of Lob’s core services: creating a postcard through our Print & Mail API. Make sure you are in the correct directory before running the code samples (creating one for the purpose of running these is advised, but not required).
Create a new directory and switch to it from your terminal app with these commands.
mkdir lob-qs
cd lob-qs
The next step is to install the version of Lob API which matches the language you are working in:
Now that a Lob API wrapper has been installed on your local machine, we can begin by sending a request to the API with the data necessary for postcard creation.
Open the text editor of your choice (if you need to download one, we recommend Visual Studio Code) and paste the code in the language of your choice: (As a note, if the address in question has a suite or apartment number, you can add in a data point for it as such: -d "to[address_line2]=<OPTIONAL SECONDARY INFORMATION>")
In your working directory:
Create a new file app.ts
Copy and paste the code below into app.ts
Replace <YOUR_TEST_KEY> with your Lob Test API Key in the code.
Replace <YOUR_TEST_KEY> with your Lob Test API Key in the code.
import lob_pythonfrom lob_python.exceptions import ApiExceptionfrom lob_python.model.postcard_editable import PostcardEditablefrom lob_python.model.address_editable import AddressEditablefrom lob_python.model.merge_variables import MergeVariablesfrom lob_python.model.country_extended import CountryExtendedfrom lob_python.api.postcards_api import PostcardsApiconfiguration = lob_python.Configuration( username ="<YOUR_TEST_KEY>")postcard_editable =PostcardEditable( description ="First Postcard", front ="<html style='padding: 1in; font-size: 50;'>Front HTML for {{name}}</html>", back ="<html style='padding: 1in; font-size: 20;'>Back HTML for {{name}}</html>", to =AddressEditable( name ="Harry Zhang", address_line1 ="210 King Street",address_city ="San Francisco",address_state ="CA",address_zip ="94107", ), _from =AddressEditable( name ="Leore Avidar", address_line1 ="210 King Street", address_city ="San Francisco", address_state ="CA", address_zip ="94107", address_country =CountryExtended("US") ), merge_variables =MergeVariables( name ="Harry", ),)with lob_python.ApiClient(configuration)as api_client: api =PostcardsApi(api_client)try: created_postcard = api.create(postcard_editable)print(created_postcard)except ApiException as e:print(e)
In your working directory:
Run your app:
python app.py
In your working directory:
Create a new file app.rb
Copy and paste the code below into app.rb
Replace <YOUR_TEST_KEY> with your Lob Test API Key in the code.
require'lob.rb'require'pp'lob =Lob::Client.new(api_key: "<TEST_API_KEY>")pp lob.postcards.create( description: "First Postcard", to: { name: "Harry Zhang", address_line1: "210 King Street", address_city: "San Francisco", address_state: "CA", address_zip: "94107" }, from: { name: "Leore Avidar", address_line1: "210 King Street", address_city: "San Francisco", address_state: "CA", address_zip: "94107" }, merge_variables: { name: "Harry" }, front: "<html style='padding: 1in; font-size: 50;'>Front HTML for {{name}}</html>", back: "<html style='padding: 1in; font-size: 20;'>Back HTML for {{name}}</html>")
In your working directory:
Run your app:
ruby app.rb
In your working directory:
Create a new file app.php
Copy and paste the code below into app.php
Replace <YOUR_TEST_KEY> with your Lob Test API Key in the code.
<?php require__DIR__.'/vendor/autoload.php';$lob =new\Lob\Lob('<TEST_API_KEY');$postcard = $lob->postcards()->create(array("description"=>"First Postcard","to[name]"=>"HARRY ZHANG","to[address_line1]"=>"210 KING STREET","to[address_city]"=>"SAN FRANCISCO","to[address_state]"=>"CA","to[address_zip]"=>"94107","from[name]"=>"LEORE AVIDAR","from[address_line1]"=>"210 KING STREET","from[address_city]"=>"SAN FRANCISCO","from[address_state]"=>"CA","from[address_zip]"=>"94107","front"=>"<html style='padding: 1in; font-size: 50;'>Front HTML for {{name}}</html>","back"=>"<html style='padding: 1in; font-size: 20;'>Back HTML for {{name}}</html>","merge_variables[name]"=>"Harry" ));print_r($postcard);?>
In your working directory:
Run your app:
php app.php
In your new maven project:
Locate the App.java file in main/java/com/company/app
Copy and paste the code below into App.java
Replace <YOUR_TEST_KEY> with your Lob Test API Key in the code.
In your working directory, you should have a file named Program.cs from running the dotnet new console command.
Copy and paste the code below into Program.cs.
Replace <YOUR_TEST_KEY> with your Lob Test API Key in the code.
usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usinglob.dotnet.Api;usinglob.dotnet.Client;usinglob.dotnet.Model;namespacetest_dotnet{classProgram {staticvoidMain(string[] args) {Configuration config =newConfiguration();config.BasePath="https://api.lob.com/v1"; // Configure HTTP basic authorization: basicAuthconfig.Username="<YOUR_TEST_KEY>";Dictionary<string,string> mergeVariables =newDictionary<string,string>();mergeVariables.Add("name","Harry");PostcardsApi apiInstance =newPostcardsApi(config);AddressEditable to =newAddressEditable("210 King St",// addressLine1null,// addressLine2"San Francisco",// addressCity"CA",// addressState"94107",// addressZipCountryExtended.US,// addressCounty"First Postcard",// description"Harry Zhang"// name );AddressEditable from =newAddressEditable("210 King St",// addressLine1null,// addressLine2"San Francisco",// addressCity"CA",// addressState"94107",// addressZipCountryExtended.US,// addressCountynull,// description"Leore Avidar"// name );PostcardEditable postcardEditable =newPostcardEditable(to.ToJson(),// tofrom.ToJson(),// fromdefault(PostcardSize),// size"First Postcard",// descriptionnull,// metadatadefault(MailType),// mailType mergeVariables,// mergeVariablesdefault(DateTime),// sendDate"<html style='padding: 1in; font-size: 50;'>Front HTML for {{name}}</html>",// front"<html style='padding: 1in; font-size: 20;'>Back HTML for {{name}}</html>"// back );try { // createPostcard result =apiInstance.create(postcardEditable);Console.WriteLine(result.ToString()); }catch (ApiException e) {Console.WriteLine("Exception when calling PostcardsApi.create: "+e.Message );Console.WriteLine("Status Code: "+e.ErrorCode);Console.WriteLine(e.StackTrace); } } }}
In your working directory:
Run your app:
dotnet run
Data formatting
To ensure your requests are processed efficiently and without error, it's crucial to submit data in the precise format outlined in our documentation. Adhering to these specifications guarantees the continued reliability and performance of our API services.
For fields that are intended to accept only string values, submitting JSON objects in string format (stringified JSON objects) is not supported. Our system automatically parses and validates incoming data according to their expected types. As a result, providing a stringified JSON object in a field designated for string input can cause parsing errors or lead to unexpected validation results.
Understanding the response
This is a sample of the postcard object you will receive upon sending a creation request. Some information breakdowns:
As you can see, the to and from address objects have been expanded with more detail, including optional fields, creation and modification dates, and sanitized, verified address lines.
The postcard object also includes a signed URL to a digital copy of the created postcard, as well as thumbnails in three different sizes of the postcard’s front and back.
front_template_id and back_template_id are null in this particular object because we used inline HTML in the templates, rather than referencing a previously-saved HTML template.
Because we did not specify a size, the postcard is the default size of 4x6".
Both the send_date and the expected_delivery_date of the postcard are provided for transparency in the mailing process.
To see the postcards you have created, log into your Lob dashboard account and navigate to the “Postcards” tab on the left-side navigation menu. You can toggle between the ones you have created using either your test or live key:
More resources
Github
For more comprehensive code examples for the different languages supported by Lob, see the individual GitHub repositories for each language: