-
Notifications
You must be signed in to change notification settings - Fork 6
Vault Management API in Python
This guide details the steps to set up a Python script for interacting with the Bitwarden Vault Management API to retrieve items from a user's vault.
It assumes you have a Bitwarden account and are familiar with basic Python programming.
- Bitwarden account.
- Python preinstalled on your system.
- An API key from Bitwarden for authenticating API requests.
-
bw
cli tool is preinstalled on your system.
- Download the zipped file from the Bitwarden download page.
- Install unzip with
sudo apt-get install unzip -y
. - Unzip the downloaded file with unzip
bw-linux-X.zip
(where X is the release num ber). - Give the binary executable permissions with
chmod u+x bw
. - Move the executable to a directory in your path with
sudo mv bw /usr/local/bin
.
You'll now need to log in to your Bitwarden account with the command:
bw login
You'll be prompted for your Bitwarden email address, master password, and (if applicable) the two-step login code. Once you've successfully logged in, you'll be greeted with You are now logged in!
.
Don't forget to export your session before the next step:
export BW_SESSION="2BroUblBCqw4M5hx1MMG4rZ+zU2/p588AsIanBYbynRCBlGW4111Yl38EQ1TJ3XbckQVWI3pq4dzlCKmOH7VMA=="
The serve command starts a local express web server that can be used to take all actions accessible from the CLI in the form of RESTful API calls from an HTTP interface.
By default, serve will start the web server at port 8087
however you can specify an alternate port with the --port
option.
bw serve
Retrieve a list of existing items in your vault. By default, this will return a list of all existing items in your vault.
import requests
URL = 'http://localhost:8087/list/object/items'
def list_vault_items():
response = requests.get(URL, headers={})
if response.status_code == 200:
resp = response.json()['data']
for item in resp['data']:
print(item['name'])
else:
print(f"Failed to retrieve items: {response.text}")
if __name__ == "__main__":
list_vault_items()
Simply execute your python script from the same terminal you exported BW_SESSION
environment variable:
$ python3 list_vault_items.py