Skip to content
FastBoundPublic

About

PowerShell, Python and bash scripts for automating FastBound through its API: bulk imports, item updates, contact merges, external IDs and bound book downloads.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

FastBound Scripts

PowerShell commands, plus Python and bash alternatives for bound book downloads, for working with a FastBound account through the FastBound API: importing items and contacts in bulk, updating items, merging duplicate FFL contacts, setting external IDs, cleaning UPCs before an import, and downloading your A&D bound book on a schedule. Every command that changes data in FastBound records the change against a named user in the account's audit history, keeps a results file as it works, and can be stopped and continued without repeating work.

What's here

Command What it does
Update-FastBoundItem Updates the fields you name on existing items, from a CSV such as the one from Items > Download Results.
Import-FastBoundItem Imports firearms from a CSV as committed acquisitions (and dispositions, where a row has a disposition date), creating any contacts that are missing.
Import-FastBoundContact Creates contacts (FFLs, organizations and individuals) from a CSV.
Merge-FastBoundContact Merges duplicate FFL contacts for the same licensee into the contact with the newest valid license.
Update-FastBoundExternalId Sets or clears the external ID on existing items from a CSV, and can number the CSV first.
Repair-FastBoundUpc Blanks UPC values in a CSV that are not 11 to 18 digits. Works offline; never contacts FastBound.
Save-FastBoundBoundBook Downloads the latest A&D bound book PDF for one account.
Save-FastBoundBoundBookAll Downloads the latest bound book PDF for every account kept in a PowerShell Secret Vault.
download-boundbook.py Python alternative to Save-FastBoundBoundBook.
download-boundbook.sh bash and cURL alternative to Save-FastBoundBoundBook.

The bound book downloaders, including scheduling instructions, are described in Bound book downloads below.

Requirements

  • PowerShell 7.5 or later (pwsh) for every PowerShell command, on Windows, macOS or Linux. Windows PowerShell 5.1 cannot run them. Install it for Windows, macOS or Linux.
  • Save-FastBoundBoundBookAll also uses the Microsoft.PowerShell.SecretManagement and Microsoft.PowerShell.SecretStore modules. On every run it installs them from the PowerShell Gallery, without prompting, if they are missing, so the first run needs access to the Gallery.
  • Python 3.6 or later for download-boundbook.py. It uses only the standard library.
  • bash, curl, base64, grep, sed and mktemp for download-boundbook.sh. With bash older than 5.0 (such as the bash that ships with macOS), it uses perl, if installed, for a finer clock; without perl it still works, waiting a full second before each request after the first.

Getting the scripts

Nothing needs installing. Each PowerShell command is a single .ps1 file at the root of this repository that you run directly. Download the ones you need, or clone the repository and run them from its root:

git clone https://github.com/FastBound/scripts.git
cd scripts
./Update-FastBoundItem.ps1 -InputFile items.csv -Account 12345 -ApiKey '<your API key>' -AuditUser someone@example.com -Field price -WhatIf
./Repair-FastBoundUpc.ps1 -InputFile items.csv -OutputFile items-clean.csv

The scripts are not code-signed. download-boundbook.py and download-boundbook.sh are at the root of the repository too.

If you downloaded a zip file on Windows

Windows marks files extracted from a downloaded zip as coming from the internet. Under the RemoteSigned execution policy (the default on Windows), unsigned files with that mark will not run. In the extracted folder, remove the mark with:

Get-ChildItem -Recurse | Unblock-File

How the commands behave

Names and parameters

Every PowerShell command has a Verb-Noun name with the FastBound prefix, and the same parameter means the same thing wherever it appears:

Command -Account -ApiKey -AuditUser -InputFile Output -Resume -WhatIf
Update-FastBoundItem yes yes yes yes results file beside the input yes yes
Import-FastBoundItem yes yes yes yes results file beside the input; -ContactsFile yes
Import-FastBoundContact yes yes yes yes results file beside the input yes
Merge-FastBoundContact yes yes yes -OutputFile (merge log) yes yes
Update-FastBoundExternalId yes yes yes yes results file beside the input yes
Repair-FastBoundUpc yes -OutputFile
Save-FastBoundBoundBook yes yes yes -OutputFile
Save-FastBoundBoundBookAll (-Add) yes yes -OutputDirectory

Every command that contacts FastBound also takes -Server, which defaults to https://cloud.fastbound.com. Update-FastBoundExternalId needs -Account, -ApiKey and -AuditUser only when it sends to FastBound. Save-FastBoundBoundBookAll takes an account number with -Add, and reads the accounts and keys from its vault when it downloads.

Before anything is sent

Update-FastBoundItem, Import-FastBoundContact and Update-FastBoundExternalId check every row of the input file before the first request; if anything is wrong they list the problems and send nothing. Import-FastBoundItem checks that the required columns are present before it sends anything, and leaves the values in each row for FastBound to check.

Results, and continuing an interrupted run

The commands that change data (Update-FastBoundItem, Import-FastBoundItem, Import-FastBoundContact, Merge-FastBoundContact and Update-FastBoundExternalId) write each outcome to a results file as soon as it is known (per item, row, merge or batch), so a run that is interrupted still leaves an accurate record of what was done. Save-FastBoundBoundBookAll does the same per account, in a dated results file that each run on the same day adds to.

If the results file from an earlier run already exists, those five commands stop before sending anything. Move the file aside to start over, or add -Resume to continue: work already recorded as done is skipped, and new rows are added to the same file. Each command's help says exactly what it skips.

Rate limits and retries

FastBound limits how many API requests an account can make in a period. Every command and script that contacts FastBound:

  • starts at most one request per second;
  • on HTTP 429 (Too Many Requests), waits for the time in the Retry-After header, or until the rate-limit window resets, or 60 seconds if neither is given, then tries again, up to 5 attempts;
  • on a server error (5xx), or when FastBound cannot be reached, tries again after 1 second and then 2 seconds, up to 3 attempts;
  • does not retry any other error (400, 401, 404 and so on), because the same request would fail the same way;
  • announces every wait as it starts, and never waits longer than one hour.

Import-FastBoundItem is the one exception to the server-error retry: it does not resend a request that creates a contact, acquisition or disposition, because FastBound may already have committed it. It leaves that row AcquirePending or DisposePending in the results file, saying it may have been committed, and -Resume stops on it until you have checked it in FastBound and recorded what you found.

The five commands that change data also read FastBound's X-RateLimit-* response headers, and pause until the window resets when the requests remaining run low, rather than waiting to be refused.

Progress

The commands that change data, Repair-FastBoundUpc (for files over 1,000 rows) and Save-FastBoundBoundBookAll print a progress line as they go, with the time elapsed and an estimate of when the run will finish. The single-account bound book downloaders print each step with the time elapsed.

Files that are replaced

Repair-FastBoundUpc and the bound book downloaders write their output to a new temporary file beside it, named after the output file with a random part and .partial added, and replace the output file only once it is complete, so a failed or interrupted run leaves the previous file exactly as it was. A run that is killed outright can leave that .partial file behind; it is safe to delete, and the next run removes it.

Two other files are written in place: the contacts file Import-FastBoundItem saves at the start of each run, and the input CSV when Update-FastBoundExternalId generates external IDs into it.

Exit codes

Every command and script exits with 0 when everything it set out to do succeeded, and 1 otherwise (download-boundbook.sh stopped with Ctrl+C exits with 130), so a scheduled task or another script can tell when a run needs attention. Each command's help lists exactly what counts as success.

Previewing with -WhatIf

Update-FastBoundItem -WhatIf reads and compares every item and writes what it would change to a preview file, without changing anything; it also supports -Confirm, to ask before each update. Merge-FastBoundContact -WhatIf prints the planned merges without merging anything. The other commands do not support -WhatIf; try Import-FastBoundItem and Import-FastBoundContact on a file of a few rows first, because what they create cannot be undone by the script.

Bound book downloads

These download your A&D bound book PDF, as ATF Ruling 2016-1 allows. Most users should use FastBound's built-in Dropbox or NAS backups instead; these are for setups that need more control, such as custom storage locations or an existing backup system. If you have a Synology or QNAP NAS, it can download the bound book on a schedule without any of these:

Script Platform Use
Save-FastBoundBoundBookAll PowerShell 7.5+ Multiple accounts, keys kept in a Secret Vault
Save-FastBoundBoundBook PowerShell 7.5+ One account
download-boundbook.sh macOS or Linux (bash and cURL) One account
download-boundbook.py Any (Python 3.6+) One account, standard library only

They are intentionally simple; if you write code, they are easy to port to another language.

What they do

For each account, each one:

  1. sends POST /{account}/api/Downloads/BoundBook to https://cloud.fastbound.com, with the account's API key and the X-AuditUser header, which FastBound requires on this request;
  2. downloads the temporary, time-limited link FastBound answers with (no credentials are sent with it) and saves the PDF.

Things to know before you rely on them:

  • They only read. Nothing in your FastBound account is created, changed or deleted.
  • Each run replaces the previous PDF. The file name is the same every day (ACCOUNT.pdf by default), so only the latest copy is kept. If you need earlier copies, have your backup system or another scheduled step move or rename the file after each run. A failed run never damages the previous PDF (see Files that are replaced).
  • The PDF is sensitive. It is your complete A&D record, including the names and addresses of the people and businesses you dealt with. Store it where only authorized people can read it.
  • No bound book yet. If FastBound has no bound book PDF ready, it answers with an empty response (HTTP 204). The script reports that the bound book is not ready, keeps any previous PDF, exits with 1, and does not ask again in the same run. Try again later.
  • The downloaded file is not checked, so confirm that your scheduled runs are producing PDFs.
  • download-boundbook.sh reads Retry-After only as a number of seconds; otherwise all four follow Rate limits and retries.

One account

./Save-FastBoundBoundBook.ps1 -Account 12345 -ApiKey '<your API key>' -AuditUser someone@example.com
./Save-FastBoundBoundBook.ps1 -Account 12345 -ApiKey '<your API key>' -AuditUser someone@example.com -OutputFile C:\Books\12345.pdf
./download-boundbook.sh -a 12345 -k '<your API key>' -u someone@example.com -o ~/Books/12345.pdf
./download-boundbook.py -a 12345 -k '<your API key>' -u someone@example.com -o ~/Books/12345.pdf

These take the API key on the command line, where other users of a shared computer can see it. On a shared or multi-user computer, use Save-FastBoundBoundBookAll instead.

Many accounts

Save-FastBoundBoundBookAll keeps each account in a PowerShell Secret Vault as one secret, named with the account number, whose value is the account's API key. The vault should hold nothing else, because -Download treats every secret in it as an account. It registers a vault named FastBound (or the name given with -SecretVault) if it does not exist.

# Add an account (run this interactively the first time: the Secret Store asks you to set a
# password when it is first used)
./Save-FastBoundBoundBookAll.ps1 -Add 12345 -ApiKey '<your API key>'

# List the stored accounts (account numbers only; keys are never shown)
./Save-FastBoundBoundBookAll.ps1 -List

# Download every account's bound book to a folder
./Save-FastBoundBoundBookAll.ps1 -Download -AuditUser someone@example.com -OutputDirectory /path/to/BoundBooks

# Remove an account
./Save-FastBoundBoundBookAll.ps1 -Remove 12345

Each -Download run records every account, as soon as it is finished, in Save-FastBoundBoundBookAll-YYYY-MM-DD.results.csv in the output folder, dated with the day the run started. The columns are Time, Account, Status (Downloaded, NotReady or Failed), File and Message. Runs on the same day add to the same file. An account that fails, even after its retries, is recorded as Failed and the run moves on to the next account.

If your Secret Store requires a password, give it with -VaultPassword. A password given this way is visible to other users of the computer in the process list, and is saved in a scheduled task's definition. Or turn the password off for automated use, as the user the scheduled task runs as (anyone who can run programs as that user can then read the stored keys):

Set-SecretStoreConfiguration -Authentication None -Interaction None -Confirm:$false

Scheduling

A scheduled task usually does not start in the folder you expect, so always give absolute paths.

Windows (Task Scheduler): create a basic task that runs daily and starts the program pwsh.exe with the arguments -File "C:\path\to\Save-FastBoundBoundBookAll.ps1" -Download -AuditUser someone@example.com -OutputDirectory "C:\BoundBooks".

macOS (launchd): create ~/Library/LaunchAgents/com.fastbound.download.plist. launchd does not search your shell's PATH, so give the full path to pwsh (which pwsh shows it; typically /usr/local/bin/pwsh, or /opt/homebrew/bin/pwsh on Apple silicon with Homebrew):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.fastbound.download</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/pwsh</string>
        <string>-File</string>
        <string>/path/to/Save-FastBoundBoundBookAll.ps1</string>
        <string>-Download</string>
        <string>-AuditUser</string>
        <string>someone@example.com</string>
        <string>-OutputDirectory</string>
        <string>/path/to/BoundBooks</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>12</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

Load it with launchctl load ~/Library/LaunchAgents/com.fastbound.download.plist.

Linux (cron): run crontab -e and add a line such as the one below (cron's PATH is short, so give the full path to pwsh if it is not in /usr/bin):

0 12 * * * pwsh -File /path/to/Save-FastBoundBoundBookAll.ps1 -Download -AuditUser someone@example.com -OutputDirectory /path/to/BoundBooks

Authentication

The commands use the account's API key, together with the email address of an active FastBound user with access to the account. The email address is sent in the X-AuditUser header, which FastBound requires on every write made with an API key, and everything a command changes is recorded in the audit history against that user.

-Account is your FastBound account number (for example 12345), not a name. The API key is sent only to FastBound, in the Authorization header, and is never written to a results file or printed. Treat it like a password: on a shared computer, other users can see command-line arguments, so for scheduled bound book downloads use Save-FastBoundBoundBookAll, which keeps the keys in a PowerShell Secret Vault.

./Merge-FastBoundContact.ps1 -Account 12345 -ApiKey '<your API key>' -AuditUser someone@example.com -WhatIf

Getting help

Each PowerShell command has full built-in help, with every parameter, the input file format, the results file columns and examples:

Get-Help ./Update-FastBoundItem.ps1 -Full

For download-boundbook.py and download-boundbook.sh, run them with -h.

For anything else, see fastbound.help or email support@fastbound.com.

License

The contents of this repository are provided completely free of charge, with no attribution required. You are free to use, modify, and distribute any of the materials in this repository however you see fit. The materials are provided without any warranty whatsoever, express or implied.

Copyright © FastBound Inc. All rights reserved.

About

PowerShell, Python and bash scripts for automating FastBound through its API: bulk imports, item updates, contact merges, external IDs and bound book downloads.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages