Local NodeJS script - use item from 1password personal vault
How can I use an item stored in 1Password personal vault from NodeJS script running locally? I see connect-sdk-js requires "1Password Connect API hosted on your infrastructure". Is it possible to do - op item get "Itemname" from NodeJS?
1Password Version: 8.10
Extension Version: Not Provided
OS Version: macOS 13.2 Ventura
Browser:_ Not Provided
Comments
-
Hi @vineet1passwd:
Great question! If you're looking to run your Node.js program locally, what may work best for you is the
run
command in 1Password CLI.run | 1Password CLI
Using this, it would be possible to set an environment variable (either in your shell, or using the
.env
file) to a secret reference, like:op://vault/item/field
. Next, use 1Password CLI to run the command:op run -- node main.js
. 1Password CLI will process the environment variables and replace the secret reference in the environment variable with the value of the secret. At that point, you can use standard Node.js functions to get the value of the environment variable, which now contains the environment variable.Let me know if that helps, or if you'd like me to dig in a bit further with you!
Jack
0 -
Thanks @Jack.P_1P. I was able to create .env file with entries like
USERNAME=op://<VaultName>/<ItemId>/username
. I then referenced the environment variable from nodejs script.jsconst userId = process.env.USERNAME;
. I was then able to run the nodejs script by specifying in package.json -"scripts": { "scriptname": "op run --no-masking --env-file=\"./.env\" -- node ./script.js" }
0 -
Hi @vineet1passwd:
Glad I could help! Let me know if there's anything else I can help you with.
Jack
0