# Overview

## Installation

To get started, you will need to install MinChat Javascript SDK in your Javascript front-end project.

{% tabs %}
{% tab title="npm" %}

```bash
npm install @minchat/js
```

{% endtab %}

{% tab title="yarn" %}

```bash
yarn add @minchat/js
```

{% endtab %}

{% tab title="pnpm" %}

```bash
pnpm install @minchat/js
```

{% endtab %}
{% endtabs %}

The MinChat SDK needs to be initialized with the `MINCHAT_API_KEY` as a parameter as well as a [User](https://docs.minchat.io/reference/user) object representing the current user logged in. The API Key can be found on the [Minchat dashboard.](https://minchat.io/sdk)

{% code title="add to imports section" %}

```javascript
import MinChat from '@minchat/js'
```

{% endcode %}

<pre class="language-javascript"><code class="lang-javascript">const currentUser = {
        username: "micheal",
        name: "Micheal Saunders",
        avatar: "urltoavatar.com/avatar.jpg" //optional
}

<strong>const minchat = MinChat.getInstance(MINCHAT_API_KEY).connectUser(currentUser)
</strong></code></pre>

## Event Listeners

### Listen for conversations <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a new message is received in a conversation, new or old.

```javascript
minchat.onChat((chat) => {
    //do something with the chat
})
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="105">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>chat</code></td><td><a href="conversation-chat"><code>Chat</code></a></td><td>The chat object that has received a new message.</td></tr></tbody></table>

## Functions

### Create User

You should create users in your MinChat application to initiate conversations with. Users are at the core of all conversations. MinChat applications are made up of users who chat in either group chats or one-to-one conversations.

in MinChat, a [User](https://docs.minchat.io/reference/user) is a person that uses your app. Typically, you will have one MinChat [User](https://docs.minchat.io/reference/user) for each user in your own database.&#x20;

Usually, you would create users based on the data from your database. A [User](https://docs.minchat.io/reference/user) is represented by a JSON object.

```typescript
const otherUser = await minchat.createUser({ 
                                  username: "example-username", 
                                  name: "Example Name",
                                  avatar: "urltoavatar.com/avatar.jpg" //optional
                            })
                            
```

*You can view more details about the user parameters* [*here*](https://docs.minchat.io/reference/user)*.*

**note:** `createUser(...)` automatically creates a new user in the system, if a user with the same username already exists then it will reuse that user.

### Fetching a user

You can fetch an already existing user using either their username or their id.

<pre class="language-javascript"><code class="lang-javascript"><strong>
</strong><strong>const otherUser = await minchat.fetchUser("example-username")
</strong>
</code></pre>

```javascript

const otherUser = await minchat.fetchUserById("example-id")

```

### Updating a user

You can update an existing user's information

```javascript

const updatedUser = await minchat.updateUserById("example-user-id",
                              { 
                                  name: "Updated Name",
                                  avatar: "urltoavatar.com/updated-avatar.jpg" 
                              })
                            
```

*You can view more details about the user parameters* [*here*](https://docs.minchat.io/reference/user)*.*

### Get Conversations

You can get a list of the conversations of the current user. conversations retrieved are paginated to 25 messages at a time.

{% tabs %}
{% tab title="async/await" %}

```javascript

const { chats , page , totalChats , totalPages  } = await minchat.getChats(1 /** page to query **/ )

```

{% endtab %}

{% tab title="callback" %}

```javascript
minchat.getChats(1 /** page to query **/)
  .then(({ chats , page , totalChats , totalPages }) => {
 
       //do something with the list of chats
   
  })
```

{% endtab %}
{% endtabs %}

**Parameters**

<table><thead><tr><th width="97">Field</th><th width="100">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>page</code></td><td><code>number</code></td><td><em>optional</em></td><td>query a specific page of chats, each page contains 25 chats. if <code>null</code> or <code>undefined</code> then the most recent 25 chats are returned</td></tr></tbody></table>

**Response**

<table><thead><tr><th width="156.33333333333331">Field</th><th width="120">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>chats</code></td><td><a href="conversation-chat"><code>Chat[]</code></a></td><td>array of Chat objects</td></tr><tr><td><code>page</code></td><td><code>number</code></td><td>shows the current page of chats. each page contains 25 chats</td></tr><tr><td><code>totalChats</code></td><td><code>number</code></td><td>shows the total number of chats that exist</td></tr><tr><td><code>totalPages</code></td><td><code>number</code></td><td>shows the total number of pages of chats that exist. each page contains 25 chats</td></tr></tbody></table>

### Delete a user

Delete a user by username

```javascript
const success = await minchat.deleteUser(otherUser.username)
```

Or delete a user by user id

```javascript
const success = await minchat.deleteUserById(otherUser.id)
```

### Start One-to-One Conversation

```javascript
const chat = await minchat.chat(otherUser.username)
```

<table><thead><tr><th width="168">Field</th><th width="106">Type</th><th width="102">State</th><th>Description</th></tr></thead><tbody><tr><td><code>username</code></td><td><code>string</code></td><td><em>required</em></td><td>The username of the user to start a conversation with. (<strong>note</strong>: this user must already be created)</td></tr></tbody></table>

### Start Group Chat

<pre class="language-javascript"><code class="lang-javascript"><strong>
</strong><strong>const chat = await minchat.groupChat({
</strong>                 name: "Epic Group Chat",
<strong>                 memberUsernames: [ otherUser.username , thirdUser.username ],
</strong><strong>                 avatar: "exampleavatar.com" 
</strong><strong> })
</strong><strong>
</strong></code></pre>

<table><thead><tr><th width="208">Field</th><th width="174">Type</th><th width="111">State</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>string</code></td><td><em>optional</em></td><td>The name of the group chat.</td></tr><tr><td><code>memberUsernames</code></td><td><code>string[]</code></td><td><em>required</em></td><td>username list of participants of the group chat.</td></tr><tr><td>avatar</td><td><code>string | file</code></td><td>optional</td><td>the avatar of the group chat</td></tr></tbody></table>
