### Install KOA and Webhook Libraries Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Install the KOA framework and necessary libraries for handling webhooks. ```bash npm install koa @types/koa npm install @lob/webhook --save-dev ``` -------------------------------- ### Initialize Lob SDK and Prepare Postcard Data in Java Source: https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window This Java snippet shows the initial setup for using the Lob SDK, including API key initialization and preparing data structures for creating a postcard with a custom send date. This is the starting point for programmatic postcard creation in Java. ```java package com.company.app; // HashMap for Java 8 and below // import java.util.HashMap; import java.util.Map; import com.lob.Lob; import com.lob.model.Address; import com.lob.model.Postcard; import com.lob.net.LobResponse; public class App { public static void main( String[] args ) { Lob.init(""); ``` -------------------------------- ### PHP Postcard Creation Setup Source: https://help.lob.com/developer-docs/api-quickstart-guide This PHP snippet initializes the Lob client with your API key. It serves as the starting point for creating postcards via the API in PHP. Replace '' with your actual Lob Test API Key. ```php ``` -------------------------------- ### Install Lob PHP SDK via Composer Source: https://help.lob.com/developer-docs/sdks-and-libraries Install the Lob PHP client using Composer. This is the recommended method for PHP projects. ```bash composer require lob/lob-php ``` -------------------------------- ### Install Lob Python Package Source: https://help.lob.com/developer-docs/api-quickstart-guide Install the Lob Python library from PyPI into your activated virtual environment. ```bash pip install lob_python ``` -------------------------------- ### GET Request to Query Documentation Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Use this GET request to ask specific questions about the documentation. The response includes direct answers and relevant excerpts. ```bash GET https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks.md?ask= ``` -------------------------------- ### Install Lob Python SDK via Pip Source: https://help.lob.com/developer-docs/sdks-and-libraries Install the Lob Python SDK using pip. This is the standard method for Python projects. ```bash pip install lob ``` -------------------------------- ### Install Lob C#/.NET SDK Source: https://help.lob.com/developer-docs/api-quickstart-guide Add the Lob .NET SDK package to your C# project using the .NET CLI. ```bash dotnet add package lob.dotnet ``` -------------------------------- ### Create Postcard with Custom Send Date in PHP Source: https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window This PHP example shows how to create a postcard and set a specific send date. Ensure you have the Lob PHP SDK installed via Composer and your API key is provided. ```php 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" => "Front HTML for {{name}}", "back" => "Back HTML for {{name}}", "merge_variables[name]" => "Harry", "send_date" => "2022-07-01T14:00:00" )); print_r($postcard); ?> ``` -------------------------------- ### Install Lob Ruby Gem Manually Source: https://help.lob.com/developer-docs/sdks-and-libraries Manually install the Lob Ruby gem using the 'gem install' command. ```bash $ gem install lob ``` -------------------------------- ### Install Lob TypeScript SDK via NPM Source: https://help.lob.com/developer-docs/sdks-and-libraries Install the Lob TypeScript SDK using NPM. This is the standard method for adding the SDK to your project. ```bash $ npm i @lob/lob-typescript-sdk ``` -------------------------------- ### Build and Install Lob TypeScript SDK from Source Source: https://help.lob.com/developer-docs/sdks-and-libraries Clone the Lob TypeScript SDK repository and install dependencies from the latest source. Use this for development or to access the newest features. ```bash $ git clone git@github.com:lob/lob-typescript-sdk.git $ npm install ``` -------------------------------- ### Install React Address Autocomplete Source: https://help.lob.com/developer-docs/sdks-and-libraries Install the react-address-autocomplete package using npm. This component simplifies adding search autocomplete functionality using the Lob Autocomplete API. ```bash npm install --save @lob/react-address-autocomplete ``` -------------------------------- ### Install Lob Ruby Gem Source: https://help.lob.com/developer-docs/api-quickstart-guide Install the Lob Ruby gem using the RubyGems package manager. ```bash gem install lob ``` -------------------------------- ### Create Postcard with Metadata (PHP) Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup This PHP example illustrates creating a postcard and setting metadata with a 'batch_id'. Ensure the Lob SDK is initialized with your API key. ```php $lob = new \Lob\Lob('test_XXXXXX'); $postcard = $lob->postcards()->create(array( "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]" => "185 BERRY STREET", "from[address_line2]" => "SUITE 6100", "from[address_city]" => "SAN FRANCISCO", "from[address_state]" => "CA", "from[address_zip]" => "94107", "front" => "Front HTML", "back" => "Back HTML", "metadata[batch_id]" => "NEWYORK2022" )); ``` -------------------------------- ### Create Postcard with Metadata (Python) Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup This Python example demonstrates creating a postcard and assigning a 'batch_id' in the metadata. Ensure your Lob API key is set. ```python import lob lob.api_key = "test_XXXXXXXXX" postcard = lob.Postcard.create( to_address = { "name": "HARRY ZHANG", "address_line1": "210 KING STREET", "address_city": "SAN FRANCISCO", "address_state": "CA", "address_zip": "94107" }, from_address = { "name": "LEORE AVIDAR", "address_line1": "185 BERRY STREET", "address_line2": "SUITE 6100", "address_city": "SAN FRANCISCO", "address_state": "CA", "address_zip": "94107", "address_country": "US" }, front = "Front HTML", back = "Back HTML", metadata = { "batch_id": "NEWYORK2022" } ) ``` -------------------------------- ### Querying Documentation with `ask` Parameter Source: https://help.lob.com/developer-docs/use-case-guides/ncoa-responses Perform an HTTP GET request to the current page URL with the `ask` query parameter to dynamically query the documentation. The question should be specific and self-contained. ```HTTP GET https://help.lob.com/developer-docs/use-case-guides/ncoa-responses.md?ask= ``` -------------------------------- ### Install Latest LTS Node.js Version Source: https://help.lob.com/developer-docs/api-quickstart-guide Use this command to install the latest Long Term Support (LTS) release of Node.js via nvm. ```bash nvm install --lts ``` -------------------------------- ### Ask a Question via Documentation Index Source: https://help.lob.com/developer-docs/%7B%7B%20revealButtonHref%20%7D%7D Perform an HTTP GET request to the documentation index with the 'ask' parameter to get a direct answer to a specific question. The question should be self-contained and in natural language. ```http GET https://help.lob.com/developer-docs/error-reference.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://help.lob.com/developer-docs/use-case-guides Perform an HTTP GET request with the `ask` query parameter to retrieve specific information not explicitly present on a page. The question should be specific and in natural language. ```bash GET https://help.lob.com/developer-docs/use-case-guides.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://help.lob.com/developer-docs To get specific information not found directly on a page, make an HTTP GET request to the page URL with the 'ask' query parameter. The question should be clear and self-contained. ```HTTP GET https://help.lob.com/developer-docs.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window Use the 'ask' query parameter to get dynamic answers from the Lob documentation. This is useful when the answer is not explicitly present on the page, or you need clarification. ```http GET https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window.md?ask= ``` -------------------------------- ### Create a Postcard in Java Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup This Java example shows how to create a postcard using the Lob Java SDK. Ensure you have initialized Lob with your API key before making the call. ```java import java.util.Map; import com.lob.Lob; import com.lob.model.Address; import com.lob.model.Postcard; import com.lob.net.LobResponse; public class App { public static void main( String[] args ) { Lob.init("test_XXXXXXXX"); try { LobResponse response = new Postcard.RequestBuilder() .setTo( new Address.RequestBuilder() .setName("HARRY ZHANG") .setLine1("210 KING STREET") .setCity("SAN FRANCISCO") .setState("CA") .setZip("94107") ) .setFrom( new Address.RequestBuilder() .setName("LEORE AVIDAR") .setLine1("210 KING STREET") .setCity("SAN FRANCISCO") .setState("CA") .setZip("94107") ) .setFront("Front HTML") .setBack("Back HTML") .create(); /* Save to your database with the postcard.id and a unique group id */ } catch (Exception err) { System.out.println("Error on postcard creation: " + err); } } } ``` -------------------------------- ### Add Node.js Type Definitions Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Install Node.js type definitions as a dev dependency to avoid TypeScript compiler errors. ```bash npm install @types/node --save-dev ``` -------------------------------- ### Include Composer Autoloader in PHP Source: https://help.lob.com/developer-docs/sdks-and-libraries After installing the Lob PHP client with Composer, require the autoloader to use the library. ```php require 'vendor/autoload.php'; ``` -------------------------------- ### Create Postcard in PHP Source: https://help.lob.com/developer-docs/api-quickstart-guide Use this snippet to create a postcard with HTML front and back content. Ensure you have the Lob PHP client installed. ```php $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" => "Front HTML for {{name}}", "back" => "Back HTML for {{name}}", "merge_variables[name]" => "Harry" )); print_r($postcard); ?> ``` -------------------------------- ### Create Check with Address Details (Java - Partial) Source: https://help.lob.com/developer-docs/use-case-guides/ncoa-responses This Java snippet demonstrates the initial steps for creating a check with recipient and sender address details using the Lob Java SDK. Note: This is a partial example. ```java import com.lob.Lob; import com.lob.model.Address; import com.lob.model.Check; import com.lob.net.LobResponse; public class App { public static void main( String[] args ) { Lob.init("test_XXXXXXXX"); try { LobResponse response = new Check.RequestBuilder() .setDescription("Remimburse $1") .setAmount("1.00") .setTo( new Address.RequestBuilder() .setName("Harry Zhang") .setLine1("185 Berry Street") .setLine2("Ste 6100") .setCity("San Francisco") .setState("CA") ``` -------------------------------- ### Create a Postcard in Ruby Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup Use this snippet to create a new postcard. Ensure you have the Lob Ruby gem installed and your API key configured. ```ruby require 'lob.rb' require 'pp' lob = Lob::Client.new(api_key: "test_XXXXXX") newPostcard = lob.postcards.create( 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: "185 BERRY ST", address_line2: "SUITE 6100", address_city: "SAN FRANCISCO", address_state: "CA", address_zip: "94107" }, front: "Front HTML", back: "Back HTML" ) //Save to your database with the postcard.id and a unique group id ``` -------------------------------- ### Create Postcard in C#/.NET Source: https://help.lob.com/developer-docs/api-quickstart-guide This C# code snippet shows how to create a postcard using the Lob API. Remember to replace '' with your Lob Test API Key and ensure you have the lob.dotnet SDK installed. ```csharp using System; using System.Collections.Generic; using System.Diagnostics; using lob.dotnet.Api; using lob.dotnet.Client; using lob.dotnet.Model; namespace test_dotnet { class Program { static void Main(string[] args) { Configuration config = new Configuration(); config.BasePath = "https://api.lob.com/v1"; // Configure HTTP basic authorization: basicAuth config.Username = ""; Dictionary mergeVariables = new Dictionary(); mergeVariables.Add("name", "Harry"); PostcardsApi apiInstance = new PostcardsApi(config); AddressEditable to = new AddressEditable( "210 King St", // addressLine1 null, // addressLine2 "San Francisco", // addressCity "CA", // addressState "94107", // addressZip CountryExtended.US, // addressCounty ``` -------------------------------- ### Create Postcard in Python Source: https://help.lob.com/developer-docs/api-quickstart-guide This Python code creates a postcard using HTML for the front and back, with merge variables for personalization. Replace '' with your Lob Test API Key. Ensure the 'lob-python' library is installed. ```python import lob_python from lob_python.exceptions import ApiException from lob_python.model.postcard_editable import PostcardEditable from lob_python.model.address_editable import AddressEditable from lob_python.model.merge_variables import MergeVariables from lob_python.model.country_extended import CountryExtended from lob_python.api.postcards_api import PostcardsApi configuration = lob_python.Configuration( username = "" ) postcard_editable = PostcardEditable( description = "First Postcard", front = "Front HTML for {{name}}", back = "Back HTML for {{name}}", 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) ``` -------------------------------- ### Create Postcard with Custom Send Date in Python Source: https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window This Python code demonstrates creating a postcard with a specified send date. Ensure the Lob Python library is installed and your API key is set. ```python import lob lob.api_key = "" postcard = lob.Postcard.create( description = "First Postcard", to_address = { "name": "Harry Zhang", "address_line1": "210 King Street", "address_city": "San Francisco", "address_state": "CA", "address_zip": "94107" }, from_address = { "name": "Leore Avidar", "address_line1": "210 King Street", "address_city": "San Francisco", "address_state": "CA", "address_zip": "94107", "address_country": "US" }, front = "Front HTML for {{name}}", back = "Back HTML for {{name}}", merge_variables = { "name": "Harry" }, send_date = "2022-07-01T14:00:00" ) print(postcard) ``` -------------------------------- ### Create Postcard with Custom Send Date in Ruby Source: https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window Use this Ruby script to schedule a postcard for a future send date. Make sure the 'lob.rb' gem is installed and your API key is configured. ```ruby require 'lob.rb' require 'pp' lob = Lob::Client.new(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: "Front HTML for {{name}}", back: "Back HTML for {{name}}", send_date: "2022-07-01T14:00:00" ) ``` -------------------------------- ### Create Postcard with Custom Send Date in TypeScript Source: https://help.lob.com/developer-docs/use-case-guides/override-cancellation-window Use this snippet to create a postcard and specify a future send date for production. Ensure the Lob TypeScript SDK is installed and configured with your API key. ```typescript import {Configuration, Postcard, PostcardsApi, PostcardEditable, CountryExtended } from "@lob/lob-typescript-sdk" async function demo() { const config: Configuration = new Configuration({ username: "" }) const postcardData : PostcardEditable = { 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", address_country: CountryExtended.Us }, front: "https://s3-us-west-2.amazonaws.com/public.lob.com/assets/templates/4x6_pc_template.pdf", back: "https://s3-us-west-2.amazonaws.com/public.lob.com/assets/templates/4x6_pc_template.pdf", send_date: "2022-07-01T14:00:00" } try { const result : Postcard = await new PostcardsApi(config).create(postcardData) return result } catch (err: any) { console.error(err) } } demo().then((result)=> console.log(result)).catch() ``` -------------------------------- ### Create and Cancel Check if Recipient Moved (Ruby) Source: https://help.lob.com/developer-docs/use-case-guides/ncoa-responses This Ruby example shows how to create a check and cancel it if the recipient's address has changed, indicated by the 'recipient_moved' key in the response. Make sure the 'lob.rb' gem is included. ```ruby require 'lob.rb' require 'pp' @lob = Lob::Client.new(api_key: "test_XXXXXXXX") newCheck = @lob.checks.create( description: 'Reimbursement $1', to: { name: 'Harry Zhang', address_line1: '185 Berry St', address_line2: '# 6100', address_city: 'San Francisco', address_state: 'CA', address_zip: '94107' }, from: { name: 'Leore Avidar', address_line1: '210 King St', address_city: 'San Francisco', address_state: 'CA', address_zip: '94107' }, bank_account: 'bank_XXXXXXXX', amount: 1.00 ) if(newCheck['to']['recipient_moved']) @lob.checks.destroy(newCheck.id) pp 'Recipient has moved - cancel' end ``` -------------------------------- ### Initialize TypeScript Project Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Set up a new directory and initialize a Node.js project with TypeScript support. ```bash mkdir webhook-ts-example && cd webhook-ts-example npm init -y npx tsc --init ``` -------------------------------- ### Create C#/.NET Console Project Source: https://help.lob.com/developer-docs/api-quickstart-guide Initialize a new C# console application project using the .NET CLI. ```bash dotnet new console ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://help.lob.com/developer-docs/api-quickstart-guide Set up a Python virtual environment for your project. This isolates project dependencies. ```bash python3 -m venv venv ``` ```bash source venv/bin/activate ``` -------------------------------- ### Create Project Directories Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Create directories for TypeScript source files and compiled JavaScript output. ```bash mkdir src mkdir dist ``` -------------------------------- ### Initialize Lob Python SDK Source: https://help.lob.com/developer-docs/sdks-and-libraries Import the lob library and set your API key to initialize the Lob Python SDK. ```python import lob lob.api_key = 'your-api-key' ``` -------------------------------- ### Add Lob Ruby Gem to Gemfile Source: https://help.lob.com/developer-docs/sdks-and-libraries Add the Lob Ruby gem to your application's Gemfile for installation via Bundler. ```ruby gem 'lob' ``` -------------------------------- ### Expose Local Server with ngrok Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Use ngrok to create a public URL for your local webhook endpoint. ```bash ./ngrok http 3000 ``` -------------------------------- ### Create Java Maven Project Source: https://help.lob.com/developer-docs/api-quickstart-guide Generate a new Maven project for your Java application. This sets up the basic project structure. ```bash mvn archetype:generate -DgroupId=com.company.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false ``` -------------------------------- ### Create Postcard with Metadata (Java) Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup This Java snippet shows how to create a postcard with metadata, including a 'batch_id'. Initialize the Lob SDK and use the RequestBuilder for address and postcard creation. ```java import java.util.Map; import com.lob.Lob; import com.lob.model.Address; import com.lob.model.Postcard; import com.lob.net.LobResponse; public class App { public static void main( String[] args ) { Lob.init("test_XXXXXXXX"); Map metaData = Map.of( "batch_id", "NEWYORK2022" ); try { LobResponse response = new Postcard.RequestBuilder() .setTo( new Address.RequestBuilder() .setName("HARRY ZHANG") .setLine1("210 KING STREET") .setCity("SAN FRANCISCO") .setState("CA") .setZip("94107") ) .setFrom( new Address.RequestBuilder() .setName("LEORE AVIDAR") .setLine1("210 KING STREET") .setCity("SAN FRANCISCO") ``` -------------------------------- ### cURL Request to Lob API Addresses Source: https://help.lob.com/developer-docs/postman-and-similar-tools This snippet shows how to use cURL to make a GET request to the Lob API's addresses endpoint. Ensure your API key is included for authentication. ```bash curl https://api.lob.com/v1/addresses \ -u test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc: ``` -------------------------------- ### Update package.json Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Configure package.json to specify the main entry point for the compiled JavaScript. ```json { "name": "webhook-ts-example", "version": "1.0.0", "description": "", "main": "./dist/index.js", "scripts": { "build": "tsc", "start": "node dist/index.js" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@lob/webhook": "^1.0.0", "@types/koa": "^2.13.5", "@types/node": "^18.0.0", "typescript": "^4.7.4" }, "dependencies": { "koa": "^2.13.4" } } ``` -------------------------------- ### Create and Cancel Check if Recipient Moved (Python) Source: https://help.lob.com/developer-docs/use-case-guides/ncoa-responses This Python snippet demonstrates creating a check and subsequently canceling it if the recipient has moved, identified by the 'recipient_moved' attribute. Ensure the Lob Python library is installed. ```python import lob lob.api_key = "test_XXXXXXXX" newCheck = lob.Check.create( description = 'Reimbursement $1', to_address = { 'name': 'Harry Zhang', 'address_line1': '185 Berry St', 'address_line2': '# 6100', 'address_city': 'San Francisco', 'address_state': 'CA', 'address_zip': '94107' }, from_address = { 'name': 'Leore Avidar', 'address_line1': '210 King St', 'address_city': 'San Francisco', 'address_state': 'CA', 'address_zip': '94107' }, bank_account = 'bank_XXXXXXXX', amount = 1.00, ) if('recipient_moved' in newCheck.to_address.__dict__): lob.Check.delete(newCheck.id) print("Recipient has moved - cancel") ``` -------------------------------- ### Create and Cancel Check if Recipient Moved (TypeScript) Source: https://help.lob.com/developer-docs/use-case-guides/ncoa-responses Use this snippet to create a check and then cancel it if the recipient's address has changed, as indicated by the 'recipient_moved' flag. Ensure you have the Lob TypeScript SDK installed. ```typescript import { Configuration, ChecksApi, CheckEditable, CountryExtended, AddressEditable } from "@lob/lob-typescript-sdk"; async function demo() { const config: Configuration = new Configuration({ username: "test_XXXXXXXX", }); const checkToCreate = new CheckEditable({ description: "Reimbursement $1", to: new AddressEditable({ name: "HARRY ZHANG", address_line1: "185 BERRY ST", address_line2: "SUITE 6100", address_city: "SAN FRANCISCO", address_state: "CA", address_zip: "94107" }), from: new AddressEditable({ name: "LEORE AVIDAR", address_line1: "210 King Street", address_city: "SAN FRANCISCO", address_state: "CA", address_zip: "94107", address_country: CountryExtended.Us, }), bank_account: "bank_XXXXXXXX", amount: 1, }); const checksApi = new ChecksApi(config); try { const newCheck = await checksApi.create(checkToCreate); if (newCheck.to?.recipient_moved) { console.log("Recipient has moved - cancel ") await checksApi.cancel(newCheck.id); } } catch (err) { console.error(err); } } demo().then().catch(); ``` -------------------------------- ### Create Postcard with HTML Content (Python) Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup This Python snippet demonstrates creating a postcard with specified front and back HTML content. It requires your Lob API key to be set. ```python import lob lob.api_key = "test_XXXXXXXXX" postcard = lob.Postcard.create( to_address = { "name": "HARRY ZHANG", "address_line1": "210 KING STREET", "address_city": "SAN FRANCISCO", "address_state": "CA", "address_zip": "94107" }, from_address = { "name": "LEORE AVIDAR", "address_line1": "185 BERRY ST", "address_line2": "SUITE 6100", "address_city": "SAN FRANCISCO", "address_state": "CA", "address_zip": "94107", "address_country": "US" }, front = "Front HTML for Postcard", back = "Back HTML for Postcard" ) ``` -------------------------------- ### Create Postcard with Metadata (Ruby) Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup This Ruby snippet shows how to create a postcard with metadata, using a 'batch_id' for batch identification. Initialize the Lob client with your API key. ```ruby require 'lob.rb' require 'pp' lob = Lob::Client.new(api_key: "test_XXXXXX") newPostcard = lob.postcards.create( 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: "185 BERRY STREET", address_line2: "SUITE 6100", address_city: "SAN FRANCISCO", address_state: "CA", address_zip: "94107" }, front: "Front HTML", back: "Back HTML", metadata: { "batch_id": "NEWYORK2022" } ) ``` -------------------------------- ### Create Postcard via API Source: https://help.lob.com/developer-docs/api-quickstart-guide This C# code snippet demonstrates how to create a postcard object with recipient and sender addresses, HTML content for the front and back, and then send it using the API. It includes basic error handling for API exceptions. ```csharp using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using Org.OpenAPITools.Client; using Org.OpenAPITools.Model; namespace Org.OpenAPITools.Api { public class PostcardsApiExample { public static void Main(string[] args) { Configuration.Default.AccessToken = "YOUR_API_KEY"; var apiInstance = new PostcardsApi(); var mergeVariables = new Dictionary(); mergeVariables.Add("name", "Harry Zhang"); AddressEditable to = new AddressEditable( "210 King St", // addressLine1 null, // addressLine2 "San Francisco", // addressCity "CA", // addressState "94107", // addressZip CountryExtended.US, // addressCounty null, // description ``` ```csharp AddressEditable from = new AddressEditable( "210 King St", // addressLine1 null, // addressLine2 "San Francisco", // addressCity "CA", // addressState "94107", // addressZip CountryExtended.US, // addressCounty null, // description ``` ```csharp PostcardEditable postcardEditable = new PostcardEditable( to.ToJson(), // to from.ToJson(), // from default(PostcardSize), // size ``` ```csharp try { // create Postcard 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); } } } } ``` -------------------------------- ### Configure TypeScript Compiler Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Create a tsconfig.json file to configure the TypeScript compiler settings. ```json { "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] } ``` -------------------------------- ### Run TypeScript Code Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Compile the TypeScript code to JavaScript and run the server. ```bash npm run build npm start ``` -------------------------------- ### Create Webhook Endpoint Source: https://help.lob.com/developer-docs/use-case-guides/ingesting-tracking-events-with-webhooks Implement the webhook endpoint using KOA to handle incoming tracking events. ```typescript import Koa from 'koa'; import bodyParser from 'koa-bodyparser'; import { verifyLob } from '@lob/webhook'; const app = new Koa(); app.use(bodyParser()); app.use(async (ctx, next) => { // Verify the signature const signature = ctx.request.headers['x-lob-signature']; const payload = ctx.request.body; if (!signature || !verifyLob(process.env.WEBHOOK_SECRET, payload, signature)) { ctx.status = 401; ctx.body = 'Invalid signature'; return; } // Process the event console.log('Received Lob event:', payload.event.type); console.log(payload); ctx.status = 200; await next(); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); }); ``` -------------------------------- ### Create Postcard Source: https://help.lob.com/developer-docs/api-quickstart-guide This snippet demonstrates how to create a postcard using the Lob API. It includes details for the 'to' and 'from' addresses, and the HTML content for the front and back of the postcard. ```APIDOC ## Create Postcard ### Description This endpoint creates a new postcard object. You can specify recipient and sender details, as well as the HTML content for the front and back of the postcard. ### Method POST ### Endpoint /postcards ### Parameters #### Request Body - **description** (string) - Optional - An optional description for the postcard. - **to[name]** (string) - Required - The name of the recipient. - **to[address_line1]** (string) - Required - The first line of the recipient's street address. - **to[address_city]** (string) - Required - The recipient's city. - **to[address_state]** (string) - Required - The recipient's state. - **to[address_zip]** (string) - Required - The recipient's ZIP code. - **from[name]** (string) - Required - The name of the sender. - **from[address_line1]** (string) - Required - The first line of the sender's street address. - **from[address_city]** (string) - Required - The sender's city. - **from[address_state]** (string) - Required - The sender's state. - **from[address_zip]** (string) - Required - The sender's ZIP code. - **front** (string) - Required - HTML content for the front of the postcard. - **back** (string) - Optional - HTML content for the back of the postcard. - **merge_variables[name]** (string) - Optional - A merge variable to be used in the HTML content. ``` -------------------------------- ### Create Postcard in Java Source: https://help.lob.com/developer-docs/api-quickstart-guide This Java code demonstrates how to create a postcard using the Lob API. Replace '' with your actual API key. Ensure you have the Lob Java SDK and Gson library added to your Maven project. ```java package com.company.app; import com.lob.api.ApiClient; import com.lob.api.ApiException; import com.lob.api.Configuration; import com.lob.api.auth.*; import com.lob.model.*; import com.lob.api.client.PostcardsApi; public class App { public static void main( String[] args ) { ApiClient lobClient = Configuration.getDefaultApiClient(); // Configure HTTP basic authorization: basicAuth HttpBasicAuth basicAuth = (HttpBasicAuth) lobClient.getAuthentication("basicAuth"); basicAuth.setUsername(""); PostcardsApi apiInstance = new PostcardsApi(lobClient); PostcardEditable postcardEditable = new PostcardEditable(); AddressEditable to = new AddressEditable(); to.setName("MORTICIA ADDAMS"); to.setAddressLine1("1313 CEMETERY LN"); to.setAddressCity("WESTFIELD"); to.setAddressState("NJ"); to.setAddressZip("07091"); AddressEditable to = new AddressEditable(); to.setName("MORTICIA ADDAMS"); to.setAddressLine1("1313 CEMETERY LN"); to.setAddressCity("WESTFIELD"); to.setAddressState("NJ"); to.setAddressZip("07091"); AddressEditable from = new AddressEditable(); from.setName("FESTER"); from.setAddressLine1("001 CEMETERY LN"); from.setAddressLine2("SUITE 666"); from.setAddressCity("WESTFIELD"); from.setAddressState("NJ"); from.setAddressZip("07091"); Gson gson = new Gson(); postcardEditable.setTo(gson.toJson(to)); postcardEditable.setFrom(gson.toJson(from)); postcardEditable.setFront("https://s3-us-west-2.amazonaws.com/public.lob.com/assets/templates/4x6_pc_template.pdf"); postcardEditable.setBack("https://s3-us-west-2.amazonaws.com/public.lob.com/assets/templates/4x6_pc_template.pdf"); try { Postcard result = apiInstance.create(postcardEditable); System.out.println(result); } catch (ApiException e) { System.err.println("Exception when calling PostcardsApi#postcardCreate"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Create Postcard with Metadata (TypeScript) Source: https://help.lob.com/developer-docs/use-case-guides/mass-deletion-setup Use this snippet to create a single postcard and include metadata with a unique batch identifier. Ensure you have the Lob SDK configured. ```typescript const config: Configuration = new Configuration({ username: "test_XXXXXXXXX", }); const postCardExample: PostcardEditable = { to: { company: "HARRY ZHANG", address_line1: "210 KING STREET", address_city: "SAN FRANCISCO", address_state: "CA", address_zip: "94107", address_country: CountryExtended.Us, }, from: { company: "LEORE AVIDAR", address_line1: "185 BERRY ST", address_line2: "SUITE 6100", address_city: "SAN FRANCISCO", address_state: "CA", address_zip: "94107", address_country: CountryExtended.Us, }, front = "Front HTML", back = "Back HTML", metadata: { batch_id: "NEWYORK2022" } }; const postcardsApi = new PostcardsApi(config); const postcard = await postcardsApi.create(postCardExample); ``` -------------------------------- ### Create Postcard in Ruby Source: https://help.lob.com/developer-docs/api-quickstart-guide This Ruby script creates a postcard with specified recipient and sender details, merge variables, and HTML content for front and back. Replace '' with your Lob Test API Key. ```ruby require 'lob.rb' require 'pp' lob = Lob::Client.new(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: "Front HTML for {{name}}", back: "Back HTML for {{name}}" ) ``` -------------------------------- ### Track Address Changes in Python Source: https://help.lob.com/developer-docs/use-case-guides/visibility-of-address-changes Compares the input 'to' address with the response 'to' address when creating a postcard to identify any changes made by Lob's services. ```Python from lob import Lob lob = Lob('YOUR_LOB_API_KEY') def track_address_changes(): try: postcard = lob.postcards.create( description='Customer Service Postcard', to_address={ 'name': 'Lob.com', 'address_line1': '123 Main St', 'address_city': 'San Francisco', 'address_state': 'CA', 'address_zip': '94107', 'address_country': 'US' }, from_address={ 'name': 'Lob.com', 'address_line1': '123 Main St', 'address_city': 'San Francisco', 'address_state': 'CA', 'address_zip': '94107', 'address_country': 'US' }, message='Hello, World!', metadata={'campaign_id': 'test-campaign'} ) original_address = postcard.to corrected_address = postcard.to # Compare original_address and corrected_address to find differences print(f'Original Address: {original_address}') print(f'Corrected Address: {corrected_address}') # Example: Simple comparison for demonstration if original_address != corrected_address: print('Address was changed by Lob.') # Implement custom logic here to log or display the changes else: print('Address remained the same.') except Exception as e: print(f'Error creating postcard: {e}') track_address_changes() ``` -------------------------------- ### Track Address Changes in PHP Source: https://help.lob.com/developer-docs/use-case-guides/visibility-of-address-changes Compares the input 'to' address with the response 'to' address when creating a postcard to identify any changes made by Lob's services. ```PHP postcards()->create([ 'description' => 'Customer Service Postcard', 'to' => [ 'name' => 'Lob.com', 'address_line1' => '123 Main St', 'address_city' => 'San Francisco', 'address_state' => 'CA', 'address_zip' => '94107', 'address_country' => 'US' ], 'from' => [ 'name' => 'Lob.com', 'address_line1' => '123 Main St', 'address_city' => 'San Francisco', 'address_state' => 'CA', 'address_zip' => '94107', 'address_country' => 'US' ], 'message' => 'Hello, World!', 'metadata' => ['campaign_id' => 'test-campaign'] ]); $originalAddress = $postcard->to; $correctedAddress = $postcard->to; // Compare originalAddress and correctedAddress to find differences echo 'Original Address: ' . print_r($originalAddress, true) . "\n"; echo 'Corrected Address: ' . print_r($correctedAddress, true) . "\n"; // Example: Simple comparison for demonstration if (json_encode($originalAddress) !== json_encode($correctedAddress)) { echo 'Address was changed by Lob.\n'; // Implement custom logic here to log or display the changes } else { echo 'Address remained the same.\n'; } } catch ( Lob\Exception\LobException $e) { echo 'Error creating postcard: ' . $e->getMessage() . "\n"; } ?> ```