# Node.js SDK

A Node.js Bot SDK for SendingNetwork.

## 1. Add Dependency

Start by installing the SDK as a dependency in your Node.js project:

```shell
npm i sendingnetwork-bot-sdk
```

## 2. Prepare a Configuration File

Create a file named `bot.creds.json` and provide the following information:

```json
{
    "nodeUrl": "NODE_URL",
    "walletAddress": "YOUR_WALLET_ADDRESS",
    "privateKey": "YOUR_PRIVATE_KEY",
    "developerKey": "YOUR_DEV_KEY"
}
```

You can configure the `NODE_URL` with *<https://portal0101.sending.network>* to connect to a public Edge Node.

Make sure to replace `YOUR_WALLET_ADDRESS` , `YOUR_PRIVATE_KEY` and `YOUR_DEV_KEY` with the actual value.

## 3. Create an Instance of `SDNClient`

After setting up the configuration file, you can create an instance of the `SDNClient` class. This will allow you to interact with the SendingNetwork API.

You will need a developer key to access the Edge Network. For further insight into the mechanism and to request a developer key, please consult the provided guide [here](/sending.network/sdk-documentation/developer-key.md).

```typescript
// preLogin to get a message to sign
let auth = new SDNAuth(nodeUrl)
let loginMessage = await auth.didPreLogin(address)

let web3 = new Web3()
// sign with wallet account key
let walletSignature = web3.eth.accounts.sign(loginMessage["message"], key)
// sign with developer key (add '0x' prefix if necessary)
let developerKeySignature = web3.eth.accounts.sign(loginMessage["message"], developerKey)

// didLogin to get access token
let loginResp = await auth.didLoginWithAppToken(address,
    loginMessage["did"], loginMessage["message"], walletSignature["signature"], developerKeySignature["signature"],
    loginMessage["random_server"], loginMessage["updated"])
let accessToken = loginResp["access_token"]

// create SDNClient with a storage file
let storage = new SimpleFsStorageProvider("/path/to/bot/storage/file");
let client = new SDNClient(nodeUrl, accessToken, storage);

// register message listener
client.on("room.message", async (roomId: string, event: any) => {
    LogService.info("index", roomId, event);
});

// start syncing
await client.start();
```

## 4. Interact with the SDK

Once you have created an instance of `SDNClient` and started syncing with the SendingNetwork server, you can use the SDK to perform various actions. Here are a few examples:

**Create a new room**

```typescript
await client.createRoom({ name: 'My Room' });
```

**Invite a user to a room**

```typescript
await client.inviteUser('user_id', 'room_id');
```

**Send a message**

```typescript
await client.sendText('room_id', 'Hello, world!');
```

## Examples

For more examples and detailed use cases, you can refer to the `examples` directory in the SDK repository.


---

# Agent Instructions: 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://sending-network.gitbook.io/sending.network/sdk-documentation/node.js-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.
