# Using Nodejs Library



Connect with CAYC Exchange using Hollaex kit Nodejs Library

### Requirements

* Knowledge of basic usage of nodejs and npm
* Nodejs version >= 12
* Install [hollaex-node-lib](https://www.npmjs.com/package/hollaex-node-lib) by running `npm i hollaex-node-lib` or `yarn add hollaex-node-lib` in your project

### Usage

> Connecting with any Hollaex Kit Enabled Exchange is pretty much same, with minor modifications you can connect to all the exchanges that uses similar Hollaex Kit backends.

#### Import Library

```js
const kit = require('hollaex-node-lib');

const client = new kit({
	apiURL: 'https://api.cayc.exchange',
	baseURL: '/v2',
	apiKey: '<MY_API_KEY>',
	apiSecret: '<MY_API_SECRET>'
});
```

Put **Api Key** for `<MY_API_KEY>` & **Api Secret** for `<MY_API_SECRET>` in above code that you can get from [the guide](https://docs.cayc.exchange/docs/api_docs/introduction/#how-to-create-an-api-key).

#### Request structure

```js
// Initialize Client using above example...

client
	.getTicker('xht-usdt')
	.then((res) => {
		console.log('The volume is: ', res.volume);
	})
	.catch((err) => {
		console.log(err);
	});

client
	.getTrade({ symbol: 'xht-usdt' })
	.then((res) => {
		console.log('Public trades: ', res);
	})
	.catch((err) => {
		console.log(err);
	});
```

#### Available methods

* **Optional parameters are all contained within an object parameter called `opts`**

| Command             | Parameters                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | Description                                                                                                                                                            |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `getKit`            |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | <p>Get exchange information e.g. name, valid languages, description, etc.<br><br></p>                                                                                  |
| `getConstants`      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | <p>Tick size, min price, max price, min size and max size of each symbol pair and coin<br><br></p>                                                                     |
| `getTicker`         | **symbol**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | <p>Last, high, low, open and close price and volume within the last 24 hours<br><br></p>                                                                               |
| `getTickers`        |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | <p>Last, high, low, open and close price and volume within the last 24 hours for all symbols<br><br></p>                                                               |
| `getOrderbook`      | **symbol**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | <p>Orderbook containing list of bids and asks<br><br></p>                                                                                                              |
| `getOrderbooks`     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | <p>Orderbook containing list of bids and asks for all symbols<br><br></p>                                                                                              |
| `getTrade`          | **symbol** (_optional_)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | List of last trades                                                                                                                                                    |
| `getUser`           |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | User’s personal information                                                                                                                                            |
| `getBalance`        |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | User’s wallet balance                                                                                                                                                  |
| `getDeposits`       | <p><strong>currency</strong> (<em>optional</em>), <strong>limit</strong> (<em>optional</em>, <em>default</em>=<code>50</code>, <em>max</em>=<code>100</code>), <strong>page</strong> (<em>optional</em>, <em>default</em>=<code>1</code>), <strong>orderBy</strong> (<em>optional</em>, <em>default</em>=<code>id</code>), <strong>order</strong> (<em>optional</em>, <em>default</em>=<code>asc</code>, <code>asc</code> or <code>desc</code>), <strong>startDate</strong> (<em>optional</em>, <em>default</em>=<code>0</code>, <em>format</em>=<code>ISO8601</code>), <strong>endDate</strong> (<em>optional</em>, <em>default</em>=<code>NOW</code>, <em>format</em>=<code>ISO8601</code>)<br><br></p>            | User’s list of all deposits                                                                                                                                            |
| `getWithdrawals`    | **currency** (_optional_), **limit** (_optional_, _default_=`50`, _max_=`100`), **page** (_optional_, _default_=`1`), **orderBy** (_optional_, _default_=`id`), **order** (_optional_, _default_=`asc`, `asc` or `desc`), **startDate** (_optional_, _default_=`0`, _format_=`ISO8601`), **endDate** (_optional_, _default_=`NOW`, _format_=`ISO8601`)                                                                                                                                                                                                                                                                                                                                                               | User’s list of all withdrawals                                                                                                                                         |
| `requestWithdrawal` | **currency**, **amount**, **address** (_receipient’s_)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | Create a new withdrawal request. **Disable Two-Factor Authentication to be able to use this function. Must confirm within 5 minutes via email to complete withdrawal** |
| `getUserTrades`     | **symbol** (_optional_), **limit** (_optional_, _default_=`50`, _max_=`100`), **page** (_optional_, _default_=`1`), **orderBy** (_optional_, _default_=`id`), **order** (_optional_, _default_=`desc`, `asc` or `desc`), **startDate** (_optional_, _default_=`0`, _format_=`ISO8601`), **endDate** (_optional_, _default_=`NOW`, _format_=`ISO8601`)                                                                                                                                                                                                                                                                                                                                                                | User’s list of all trades                                                                                                                                              |
| `getOrder`          | **orderId**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Get specific information about a certain order                                                                                                                         |
| `getOrders`         | <p><strong>symbol</strong> (<em>optional</em>), <strong>limit</strong> (<em>optional</em>, <em>default</em>=<code>50</code>, <em>max</em>=<code>100</code>), <strong>page</strong> (<em>optional</em>, <em>default</em>=<code>1</code>), <strong>orderBy</strong> (<em>optional</em>, <em>default</em>=<code>id</code>), <strong>order</strong> (<em>optional</em>, <em>default</em>=<code>desc</code>, <em>enum</em>=<code>asc</code>, <code>desc</code>), <strong>startDate</strong> (<em>optional</em>, <em>default</em>=<code>0</code>, <em>format</em>=<code>ISO8601</code>), <strong>endDate</strong> (<em>optional</em>, <em>default</em>=<code>NOW</code>, <em>format</em>=<code>ISO8601</code>)<br><br></p> | Get the list of all user orders. It can be filter by passing the symbol                                                                                                |
| `createOrder`       | **symbol**, **side** (`buy` or `sell`), **size**, **type** (`market` or `limit`), **price**, **stop** (_optional_), **meta** (_optional_, object with optional properties e.g. `post_only`)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Create a new order                                                                                                                                                     |
| `cancelOrder`       | **orderId**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | Cancel a specific order with its ID                                                                                                                                    |
| `cancelAllOrders`   | **symbol** (_optional_)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | Cancel all open order. It can be filter by passing the symbol                                                                                                          |
| <p><br><br></p>     |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |                                                                                                                                                                        |

> CAYC Exchange is based on the Hollaex Kit Enabled Exchange so you can read about more for API usage, instructions & examples from [Github Repo](https://github.com/bitholla/hollaex-node-lib/tree/2.0)\
> \
> \
>

### Websocket client

> To create custom websocket client, Please visit [the guide](https://docs.cayc.exchange/docs/api_docs/custom_ws.md), This guide uses `hollaex-node-lib` and assumes that you have already configured your code to assign `client` variable/constant (as shown above) before trying this.

To make a connection:

```js
var new_connetion = client.connect(['orderbook', 'trade']);
```

To disconnect it:

```js
new_connetion.disconnect();
```

#### Channel subscriptions

You can subscribe to following channels

* orderbook
* trades
* order (Only available with authentication. Receive order updates)
* wallet (Only available with authentication. Receive balance updates)

#### Listen to events

After connection, You can listen for these events from the server by using on method, e.g.

* message
* open
* close
* error
* unexpected-response etc.

**Example**

```js
// Initialize Client using above example...
const socket1 = client.connect('trades:xht-usdt');

socket1.on('trades', (data) => {
	console.log(data);
});

const socket2 = client.connect('all');

socket2.on('orderbook', (data) => {
	console.log(data);
});

// You have to use a token to use these  otherwise the socket disconnects
socket2.on('userInfo', (data) => {
	console.log(data);
});


setTimeout(() => {
	socket2.disconnect();
}, 5);
```

### Resources

* Node Library Usage instructions: [https://github.com/bitholla/hollaex-node-lib/tree/2.0#usage](https://github.com/bitholla/hollaex-node-lib/tree/2.0#usage)
* Using Websockets from node library to connect: [https://github.com/bitholla/hollaex-node-lib/tree/2.0#websocket](https://github.com/bitholla/hollaex-node-lib/tree/2.0#websocket)
* Create custom websocket client: [https://github.com/bitholla/hollaex-kit/tree/documentation/server#usage](https://github.com/bitholla/hollaex-kit/tree/documentation/server#usage)
