Skip to content
Bitwarden Logo

Ruby SDK

The Ruby language wrapper for interacting with the Bitwarden Secrets Manager. The SDK, like the Secrets Manager CLI built on-top of it, can be used to execute the following operations:

  • Authenticate using an access token.
  • Retrieve a single secret or all secrets in a project.
  • List all secrets, secrets in a project, or projects.

Requirements

Dependencies

  • Ruby version 3.0 or newer

  • Install gem

Terminal window
gem install bitwarden-sdk-secrets
  • Import it: require 'bitwarden-sdk-secrets'

GitHub Repository

Locate the Ruby GitHub repository here.

Local Build

To interact with the client, you must first obtain an access token from Bitwarden. Client will be initialized with default client settings if they are not provided from env variables.

require 'bitwarden-sdk-secrets'
# then you can initialize BitwardenSettings:
bitwarden_settings = BitwardenSDK::BitwardenSettings.new(
'https://api.bitwarden.com',
'https://identity.bitwarden.com'
)
# By passing these setting you can initialize BitwardenClient
bw_client = BitwardenSDK::BitwardenClient.new(bitwarden_settings)
response = bw_client.access_token_login(token)
puts response

Once the Secrets Manager session has been authorized, you may interact with the client.

Secrets Manager operations

Once the Bitwarden client has been created and authorized, Secrets Manager CLI commands can be passed into the client.

Projects

The project command is used to access, manipulate, and create projects. The scope of access assigned to your machine account will determine what actions can be completed with the project command.

create project

project_name = 'Test project 1'
response = bw_client.project_client.create_project(project_name, organization_id)
puts response
project_id = response['id']

get project

response = bw_client.project_client.get(project_id)
puts response

list project

response = bw_client.project_client.get(project_id)
puts response

list projects

response = bw_client.project_client.list_projects(organization_id)
puts response

update project

name = 'Updated test project 1'
response = bw_client.project_client.update_project(project_id, name, organization_id)
puts response

delete project

response = bw_client.project_client.delete_projects([project_id])
puts response

Secrets

The secret command is used to access, manipulate and create secrets. As with all commands, secrets and projects outside your access token’s scope of access cannot be read or written-to.

create project

key = 'AWS-SES'
note = 'Private account'
value = '8t27.dfj;'
response = bw_client.secrets_client.create(key, note, organization_id, [project_id], value)
puts response
secret_id = response['id']

get secret

response = bw_client.secrets_client.get(secret_id)
puts response

get secrets by ids

response = bw_client.secrets_client.get_by_ids([secret_id])
puts response

list secrets

response = bw_client.secrets_client.list(organization_id)
puts response

update secret

note = 'updated password'
value = '7I.ert10AjK'
response = bw_client.secrets_client.update(secret_id, key, note,organization_id, [project_id], value)
puts response

delete secret

response = bw_client.secrets_client.delete_secret([secret_id])
puts response

Development

Prerequisites:

  • Ruby version 3.0 or newer installed

  • Generate schemas:

Terminal window
npm run schemas
  1. Navigate to the Ruby language folder
Terminal window
cd languages/ruby
  1. Create the binary folder if it does not already existing
Terminal window
mkdir -p ./bitwarden_sdk_secrets/lib/macos-arm64
  1. Build and copy the bitwarden-c library
Terminal window
cargo build --package bitwarden-c
cp ../../target/debug/libbitwarden_c.dylib ./bitwarden_sdk_secrets/lib/macos-arm64/libbitwarden_c.dylib
  1. Install Ruby dependencies:
Terminal window
cd ./bitwarden_sdk_secrets
bundle install
  1. Install the gem
Terminal window
bundle exec rake install

Run example tests:

Terminal window
cd ..
export ACCESS_TOKEN=""
export ORGANIZATION_ID=""
export API_URL=http://localhost:4000
export IDENTITY_URL=http://localhost:33656
ruby examples/example.rb