Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Accessing 1Password items from the terminal#
I save things like API keys in 1Password. Today I figured out how to access those from macOS terminal scripts.
My initial goal was to make a Fly.io API key available in an environment variable, without copying and pasting it.
Claude pointed me to the op 1Password terminal app. Here are the official installation instructions, which boil down to:
1brew install 1password-cliThen open 1Password and find the Developer tab in settings and enable the 1Password CLI option:
Having done this, the op command is ready to use. To see a list of vaults:
1op vault listThis asked me for my Touch ID before running:
I only have one vault, so I got back this:
1ID NAME2db6xmelzrupwlyrfbiy5ltrnfy PersonalThere are a few ways to access items. One is to find the item ID using op items list and grep:
1op items list | grep 'Datasette Cloud Dev'This displayed:
1uv4maokwxaaymkmoxawwcyfeve Datasette Cloud Dev Simon Personal 4 minutes agoYou can then access the item using op item get and that ID:
1op item get uv4maokwxaaymkmoxawwcyfeveThis output what looked like YAML:
1ID: uv4maokwxaaymkmoxawwcyfeve2Title: Datasette Cloud Dev Simon3Vault: Personal (db6xmelzrupwlyrfbiy5ltrnfy)4Created: 27 minutes ago5Updated: 5 minutes ago by Simon Willison6Favorite: false7Version: 28Category: LOGIN9Fields:10 username: fly token11 password: FlyV1 fm2_...You can also use the direct title of the item, like this:
1op item get 'Datasette Cloud Dev Simon'We just want the password, to write into an environment variable. Using --fields password nearly gets us that:
1op item get 'Datasette Cloud Dev Simon' --fields passwordOutput:
1"FlyV1 fm2_..."This is wrapped in double quotes. The easiest way I found to strip those was to pipe it through jq -r - where the -r tells jq to output the raw value:
1op item get 'Datasette Cloud Dev Simon' --fields password | jq -rOutput:
1FlyV1 fm2_...Then I assigned it to an environment variable like this:
1export FLY_API_KEY=$(op item get 'Datasette Cloud Dev Simon' --field password | jq -r)And used that key in an API call like this:
1curl 'https://api.machines.dev/v1/apps?org_slug=datasette-cloud-dev' \2 -H "Authorization: $FLY_API_KEY"Which output:
1{"total_apps":0,"apps":[]}