Browse the docs

Keychain password

Reads a password from your login keychain into a variable, or saves one to it.

op the keychain item service

Keyboard Maestro: SetVariableToKeychainPassword, SetKeychainPasswordToText

Details #

Uses the macOS login keychain, with generic passwords. get reads through the security command; set writes through the system Keychain API, so the password never appears on a command line. A generic password is found by its service (its name) and, optionally, an account. Use it to keep secrets such as API tokens out of your macros.

  • get (the default) reads the password and puts it in the variable named by into. Trailing newlines are removed. The step's trace says the password was read, never what it was.

  • set saves value as the password for that service (and account). If one already exists it is updated.

Parameters #

ParameterTypeRequiredValuesWhat it does
servicetextYesThe password's name. Required. (name is another spelling.)
accounttextThe account the password is stored under. Optional; if you leave it out, any account of that service matches.
actionone ofget setAnother spelling of op.
intotextFor get: the variable that receives the password.
nametextAnother spelling of service.
opone ofget setget or set. The default is get. Any other value fails. (action is another spelling.)
texttextAnother spelling of value.
valuetextFor set: the password text. Required for set. (text is another spelling.)

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

This one only asks the keychain for an item that very probably does not exist, and the dry run simulates it, so it is safe to try. For real it fails with a clear message if the item is missing:

Read an API token from the keychain
Do
  1. Get the keychain item my-api-token
  2. Show “Token loaded”
Open in Switchboard to try it

Gotchas #

  • get fails with no keychain password named "X" (adding for ACCOUNT when you gave one) and the security exit code. The first read of an item may also show a macOS prompt asking whether to allow access.

  • set fails with "keychain set needs a value" without a value, and with "couldn't save the keychain password" plus the Security error number otherwise. The trace shows the value of a set as bullets, never the password.

  • The password is held in a variable for the rest of the run and may show in any step that prints it, such as Show message. Don't print it.

  • Only the login keychain's generic passwords are covered; internet passwords and certificates are not.

  • Steps that touch the Mac are simulated in a dry run, so nothing is read or saved when you use Try it.

See also #

Set variable, Send HTTP request, Run shell script