# NodeJS / JS SDK doesn't work at all

**URL:** <https://community.bitwarden.com/t/nodejs-js-sdk-doesnt-work-at-all/69765>\
**Category:** Secrets Manager\
**Tags:** beta\
**Created:** [July 12, 2024, 12:39am UTC](https://community.bitwarden.com/t/nodejs-js-sdk-doesnt-work-at-all/69765 "2024-07-12T00:39:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![th3-sh0w3r](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@th3-sh0w3r](https://community.bitwarden.com/u/th3-sh0w3r)\
**Post date:** [July 12, 2024, 12:39am UTC](https://community.bitwarden.com/t/nodejs-js-sdk-doesnt-work-at-all/69765/1 "2024-07-12T00:39:46Z")

</div>

I’m trying to use the SDK for Javascript but it doesn’t work, no mattere what I try.  
Here is my code:

```auto
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.

```auto
connect()

```

works fine and returns true. But everytime I try to use getValue() for example I get this (same with list()):

```auto
{
  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?

---

<div class="post-metadata">

**Author:** ![th3-sh0w3r](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@th3-sh0w3r](https://community.bitwarden.com/u/th3-sh0w3r)\
**Post date:** [July 12, 2024, 12:43am UTC](https://community.bitwarden.com/t/nodejs-js-sdk-doesnt-work-at-all/69765/2 "2024-07-12T00:43:45Z")

</div>

Edit: After calling the code like 20 times it works once, but then again throws this mentioned error message…

---

<div class="post-metadata">

**Author:** ![th3-sh0w3r](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@th3-sh0w3r](https://community.bitwarden.com/u/th3-sh0w3r)\
**Post date:** [July 12, 2024, 11:17pm UTC](https://community.bitwarden.com/t/nodejs-js-sdk-doesnt-work-at-all/69765/3 "2024-07-12T23:17:43Z")

</div>

Ok I finally found a solution. I had to call `connect()` every time I call a method of `secrets()` and not just one when I initiliazed the class. Which means you have to `loginWithAccessToken` for every request… Seems weird but it works.

---

<div class="post-metadata">

**Author:** ![Nail1684](https://sea1.discourse-cdn.com/flex001/user_avatar/community.bitwarden.com/nail1684/32/47626_2.png) [@Nail1684](https://community.bitwarden.com/u/Nail1684)\
**Post date:** [January 12, 2025, 2:02am UTC](https://community.bitwarden.com/t/nodejs-js-sdk-doesnt-work-at-all/69765/4 "2025-01-12T02:02:52Z")

</div>

Closed on January 12, 2025, 2:02am UTC


