> For the complete documentation index, see [llms.txt](https://edgar-p-yan.gitbook.io/authembed-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://edgar-p-yan.gitbook.io/authembed-docs/admin-api/node.js-admin-sdk.md).

# Node.js Admin SDK

### Installation

```bash
npm install --save @authembed/admin
# or
yarn add @authembed/admin
```

### Usage

```typescript
import { AuthembedAdminSDK, AuthembedUser } from '@authembed/admin';

const authembedAdminSdk = new AuthembedAdminSDK({
    url: 'https://authembed.example.com',
    username: 'main_backend',
    password: '...',
});

// ...

const user: AuthembedUser = await authembedAdminSdk.getUserByToken(request.headers.authorization);

console.log(user);
// { _id: '...', name: 'John Doe', email: 'john@example.com', ... }

```

### Get users by IDs

```typescript
const usersByIds =
  await authembedAdminSdk.getUsersByIds(["609eeba19c7f152a53676191", ...]);
  
console.log(usersByIds);
// { "609eeba19c7f152a53676191": { _id: "...", name: "..." }, ... }
```

### Update a user

```typescript
const updatedUser =
  await authembedAdminSdk.updateUser({
    _id: "609eeba19c7f152a53676191",
    name: "New name",
  });
```

### Usage with Nest.js framework

The SDK also has built in support for Nest.js framework.

{% code title="app.module.ts" %}

```typescript
import { AuthembedAdminSDKModule } from '@authembed/admin/nestjs';
 
@Module({
  imports: [
    AuthembedAdminSDKModule.forRoot({ url: '...', username: '...', password: '...'})
  ]
})
export class AppModule {}
```

{% endcode %}

{% code title="app.module.ts" %}

```typescript
import { AuthembedAdminSDKModule } from '@authembed/admin/nestjs';
 
@Module({
  imports: [
    AuthembedAdminSDKModule.forRootAsync({
      useFactory: (config: ConfigService) => ({
        url: config.get('AUTHEMBED_URL'),
        ...
      }),
      inject: [ConfigService],
    }),
  ]
})
export class AppModule {}
```

{% endcode %}

{% code title="app.service.ts" %}

```typescript
import { InjectAuthembedAdminSDK } from '@authembed/admin/nestjs';
import { AuthembedAdminSDK } from '@authembed/admin';
 
@Injectable()
export class AppService {
  constructor(
    @InjectAuthembedAdminSDK()
    private readonly authembedAdminSdk: AuthembedAdminSDK,
  ) {}
 
  async authenticateUserByToken(token: string): Promise<void> {
    await this.authembedAdminSdk.getUserByToken(token);
  }
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://edgar-p-yan.gitbook.io/authembed-docs/admin-api/node.js-admin-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
