These scripts were made in order to "re-send" postcards that originally failed preflight for RealMail (but can be used for any account to resend postcards). After grabbing the original PDFs from the 'content' S3 Bucket in Lob-Production, you will need to flatten transparencies, preflight, and perform other fixes on the PDFs to make them print-ready.
Once the PDFs are print ready, you can run these scripts to get the original payloads of the postcards, then create new postcards using the the payload data and preflighted PDFs.
This repository contains two scripts:
- fetch_postcard_data.js - Fetches postcard data from the Lob API and extracts the original POST body fields to create new CSV files
- create_postcards.js - Creates new postcards using CSV data with original POST body fields
-
Install dependencies:
npm install
-
Create a
.envfile in the project directory with your Lob API key:LOB_API_KEY=your_lob_api_key_here -
Make the script executable:
chmod +x fetch_postcard_data.js
Run the fetch script with a CSV file containing postcard IDs:
node fetch_postcard_data.js 3900_navaho_st_sw_postcards.csvor
node fetch_postcard_data.js 7079_southwood_dr_jenison_postcards.csvThis will output a new CSV containing the original payload data of each of the original postcards, named like original_file_name + original_data.
Example:
node fetch_postcard_data.js 7079_southwood_dr_jenison_postcards.csvwill create 7079_southwood_dr_jenison_postcards_original_data.csv in the root directory.
The _original_data.csv will contain the original payload that will be used to create new postcards.
Since the responses from the API don't include 'front' and 'back', it's necessary to paste those values into the corresponding columns in the CSV.
The PDFs to use should be located in the root directory. Paste their filename into the 'front' and 'back' columns.
Example:
id,front,back
psc_a65e310cdfc6a06b,front_original_preflighted.pdf,back_original_preflighted.pdfThis can be easily done by highlighting all rows, then using the shift + alt + i shortcut in VSCode to put a cursor at the end of each selected line. Then move the cursor to the appropriate comma-separated-value and paste the filename. Repeat this process for both front and back values.
TODO: Automate this by standardizing the .csv filename and the .pdf filenames. If the PDFs already exist in the directory, we can find them based on filename and automatically append them.
Run the create script with a CSV file containing original postcard data:
node create_postcards.js 174_bradley_drive_postcards_original_data.csvor
node create_postcards.js 3900_navaho_st_sw_postcards_original_data.csvThe fetch script creates a new CSV file with the original data fields:
- Input:
3900_navaho_st_sw_postcards.csv - Output:
3900_navaho_st_sw_postcards_original_data.csv
The output CSV contains the following columns:
id: Postcard IDmail_type: Mail type (e.g., "usps_standard")from_name,from_address_line1,from_address_country,from_address_state,from_address_city,from_address_zip: From address fieldsto_name,to_address_line1,to_address_country,to_address_state,to_address_city,to_address_zip: To address fieldsdescription: Postcard descriptionmetadata_user,metadata_campaign_id,metadata_environment,metadata_client: Metadata fieldssize: Postcard sizefront,back: These will be empty. Here you will need to paste the filenames of the PDFs to use (they should be in the same root directory as this script)
The create script generates two CSV files:
Success Log (*_success.csv):
original_id: Original postcard ID from input CSVnew_postcard_id: New postcard ID created by Lob APIstatus: Postcard status (processed, etc.)mail_type,size,carrier: Postcard specificationsfrom_name,from_address_line1,from_address_city,from_address_state,from_address_zip,from_address_country: From address detailsto_name,to_address_line1,to_address_city,to_address_state,to_address_zip,to_address_country: To address detailsdescription: Postcard descriptionmetadata_user,metadata_campaign_id,metadata_environment,metadata_client: Metadata fieldsdate_created,date_modified,send_date,expected_delivery_date,completed_at: Timestampsurl,raw_url: Postcard PDF URLsuse_type,fsc,lob_credits_funding_status,is_creative_proof: Additional postcard detailsresponse_data: Full API response JSON
Error Log (*_errors.csv):
original_id: Original postcard ID from input CSVstatus_code: HTTP status codeerror_message,error_details: Error informationrequest_payload: Original request data that failedraw_response: Raw API error responsefrom_name,to_name,description: Postcard details for reference
- Handles API rate limiting with delays between requests (commented out, this script is honestly kind of slow and doesn't need rate limiting)
- Error handling for failed API requests
- Maintains row order even when some requests fail
- Proper CSV escaping for special characters
- Progress logging during processing
- Node.js 14.0.0 or higher
- Valid Lob API key
- Input CSV files with
idcolumn containing postcard IDs