---
# System prepended metadata

title: SendingNetwork DotNet SDK

---

# SendingNetwork DotNet SDK

A DotNet SDK for SendingNetwork.

## Supported Platforms

* [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) or greater

## Installation

Add dependency
```shell
dotnet add package SDN.Sdk
```

## Use the SDK in your app
### 1. Create 
Use `SDNClientFactory` to create an instance of `SDNClient`
```csharp
var factory = new SDNClientFactory();
ISDNClient client = factory.Create();
```

### 2.Login
Currently, SDNClient supports only wallet account login
```csharp
// pre login
var preLoginResponse = await client.PreLoginAsync(new Uri(nodeUrl), walletAddress);

// sign message (or prompt user to sign with a wallet app)
var signer = new EthereumMessageSigner();
var signature = signer.EncodeUTF8AndSign(preLoginResponse.Message, new EthECKey(privateKey));

// login did
var loginResponse = await client.LoginAsync(preLoginResponse.Did, preLoginResponse.RandomServer, preLoginResponse.Updated, preLoginResponse.Message, signature);
                userId = loginResponse.UserId;
                accessToken = loginResponse.AccessToken;

// set client credential
client.SetCredential(new Uri(nodeUrl), loginResponse.UserId, loginResponse.AccessToken);
```

### 3. Start listening for incomming events
To listen for the incoming room events you need to subscribe to `OnSDNRoomEventsReceived;`
```csharp
// add event listener
client.OnSDNRoomEventsReceived += (sender, eventArgs) =>
{
    foreach (var roomEvent in eventArgs.SDNRoomEvents)
    {
        if (roomEvent is TextMessageEvent textMessageEvent)
        {
            Console.WriteLine($"RoomId: {textMessageEvent.RoomId} received event from {textMessageEvent.SenderUserId}: {textMessageEvent.Message}.");
        }
    }
};

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

### 4. Call API functions
```csharp
// create new room
await client.CreateRoomAsync(roomName, null);

// invite user to the room
await client.InviteUserAsync(roomId, userId);

// send room message
await client.SendMessageAsync(roomId, "hello");
```

## Examples
For a complete example, refer to `Sample.cs`.You can also clone this repository and run `SDN.Sdk.Sample.Console`.


## Client API Doc

### DID PreLogin

```csharp
Task<PreLoginResponse> PreLoginAsync(Uri nodeUri, string walletAddress)
```
input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| nodeUri | string | uri of node server    | true     |
| walletAddress | string | user wallet address    | true     |

output params：
| Name     | Type        | Description | Required |
| :------- | :---------- | :---------- | :------- |
| PreLoginResponse.Did | string | user did | true     |
| PreLoginResponse.Message | string | message to be signed | true     |
| PreLoginResponse.RandomServer | string | random nonce | true     |
| PreLoginResponse.Updated | string | update time of did document | true     |

### DID Login
```csharp
Task<LoginResponse> LoginAsync(string did, string nonce, string updateTime, string message, string signature)
```

input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| did | string | user did | true     |
| nonce | string | random nonce | true     |
| updateTime | string | update time of did document | true     |
| message | string | message to be signed | true     |
| signature | string | signature of the message | true     |

output params：

| Name     | Type        | Description | Required |
| :------- | :---------- | :---------- | :------- |
| LoginResponse.UserId | string | user did | true     |
| LoginResponse.AccessToken | string | access token | true     |
| LoginResponse.DeviceId | string | device id | true     |


### Create Room

```csharp
Task<CreateRoomResponse> CreateRoomAsync(string name, string[]? invitedUserIds)
```

input params：RoomCreateOptions

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| name | string | room name    | true     |
| invitedUserIds | string array | array of user ids to invite | false |

output params:

| Name     | Type        | Description | Required |
| :------- | :---------- | :---------- | :------- |
| CreateRoomResponse.RoomId | string | id of the new room | true     |


### Invite User
```csharp
Task InviteUserAsync(string roomId, string userId)
```
input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id    | true     |
| userId | string | user id  | true    |

output params: none


### Kick User
```csharp
Task KickUserAsync(string roomId, string userId)
```
input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id    | true     |
| userId | string | user id  | true    |

return values: none


### Join Room
```csharp
Task<JoinRoomResponse> JoinRoomAsync(string roomId)
```
input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id    | true     |

output params:
| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| JoinRoomResponse.RoomId | string | room id    | true     |


### Leave Room
```csharp
Task LeaveRoomAsync(string roomId)
```

input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id    | true     |

output params: none


### Get Room Memebers
```csharp
Task<RoomMembersResponse> GetRoomMembersAsync(string roomId)
```

input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id    | true     |


output params:

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| RoomMembersResponse.joined | array of MemberInfo | room members | true     |

MemberInfo:
| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| MemberInfo.DisplayName | string | member display name | true     |
| MemberInfo.AvatarUrl | string | member avatar url | true     |



### Send Message
```csharp
Task<string> SendMessageAsync(string roomId, string message)
```

input params：

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| roomId | string | room id    | true     |
| message | string | message to send    | true     |

output params:

| Name    | Type   | Description | Required |
| :------ | :----- | :---------- | :------- |
| eventId | string | event id    | true     |