I’m trying to use the SDK for Javascript but it doesn’t work, no mattere what I try.
Here is my code:
import {
BitwardenClient,
ClientSettings,
DeviceType,
ResponseForSecretIdentifiersResponse,
ResponseForSecretResponse,
} from "@bitwarden/sdk-napi";
import { LogLevel } from "@bitwarden/sdk-napi/binding";
export class SecretsManager {
settings: ClientSettings = {
apiUrl: "https://api.bitwarden.com",
identityUrl: "https://identity.bitwarden.com",
userAgent: "Bitwarden SDK",
deviceType: DeviceType.SDK,
};
accessToken: string | undefined;
client: BitwardenClient | undefined;
organizationID: string | undefined;
constructor() {
this.accessToken = Bun.env.BITWARDEN_ACCESS_TOKEN;
this.organizationID = Bun.env.BITWARDEN_ORGANIZATION_ID;
}
async connect(): Promise<boolean> {
if (!this.accessToken) {
return false;
}
this.client = new BitwardenClient(this.settings, LogLevel.Trace);
const result = await this.client.loginWithAccessToken(this.accessToken);
return result.success;
}
async list(): Promise<ResponseForSecretIdentifiersResponse | undefined> {
if (!this.organizationID || !this.client) {
return undefined;
}
return await this.client.secrets().list(this.organizationID);
}
async get(id: string): Promise<ResponseForSecretResponse | undefined> {
if (!this.client || !id) {
return undefined;
}
return await this.client.secrets().get(id);
}
async getValue(id: string): Promise<string | undefined> {
const value = await this.get(id);
if (!value) {
return undefined;
}
return value?.data?.value;
}
}
I checked my organization ID and access token multiple times, they are fine. I even created new tokens.
connect()
works fine and returns true. But everytime I try to use getValue() for example I get this (same with list()):
{
data: null,
errorMessage: "Received error message from server: [401 Unauthorized] ",
success: false,
}
No idea what I’m doing wrong.
Even if I call this.client.secrets().create(…)
I get something like “Your vault needs to be unlocked first”
What else can I try?