The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Password::OnePassword::OPCLI - get items out of 1Password with the "op" CLI


version 0.001


  my $one_pw = Password::OnePassword::OPCLI->new;

  # Get the string found in one field in your 1Password storage:
  my $string = $one_pw->get_field("op://Private/PAUSE API/credential");

  # Get the complete document for an item, as a hashref:
  my $pw_item = $one_pw->get_item("op://Work/GitHub");


This module should work on any version of perl still receiving updates from the Perl 5 Porters. This means it should work on any version of perl released in the last two to three years. (That is, if the most recently released version is v5.40, then this module should work on both v5.40 and v5.38.)

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.



  my $one_pw = Password::OnePassword::OPCLI->new;

This is a do-almost-nothing constructor. It's only here so that methods are instance methods, not class methods. Someday, there may be more arguments to this, but for now, there are not.


  my $hashref = $one_pw->get_item($item_str, \%arg);

This looks up an item in 1Password, using the op item get command. The locator $item_str can be either the item id or two-part op:// URL. The way the URL works is like this: If you use the "Copy Secret Reference" feature of 1Password, you'll end up with a string like this on your clipboard:

  op://Private/Super Mario Fan Club/password

This refers to a single field in the vault item. (You can get that field's value with get_field, below.) You can't presently use a URL like this with the op command, but this library fakes it for you. If you provide only the first two path parts of the URL above, like this:

  op://Private/Super Mario Fan Club

…then get_item will get the "Super Mario Fan Club" item out of the "Private" vault.

The reference to a %arg hash is optional. If given, it can contain a vault entry, giving the name of the vault to look in. This is only useful when giving an item id, rather than a URL.

The method returns a reference to a hash in 1Password's documented internal format. For more information, consult the 1Password developer tools documentation. Alternatively, use this method and pretty-print the results.

If the item can't be found, or the op command doesn't exit zero, or in any case other than the best case, this method will throw an exception.


  my $str = $one_pw->get_field($field_ref_str);

This looks up an item in 1Password, using the op read command. The locator $field_ref_str should be an op:// URL, like you'd get using the "Copy Secret Reference" feature of 1Password.

It will return the string form of whatever is stored in that field. If it can't find the field, if it can't authenticate, or in any case other than "everything worked", it will raise an exception.


Ricardo Signes <>


This software is copyright (c) 2024 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.