NodeJS SDK

This wrapper facilitates the use of the Brine api.

Brine Connector includes utility/connector functions which can be used to interact with the Brine API. It uses axios internally to handle all requests. It includes interceptors for handling setting JWT and re-login on token expiry.

Please check out the full implementation here to find all the rest api methods, web-socket methods and examples.

Installation

First go to the Brine Website and create an account with your wallet.

Install from npm

npm i @brine-fi/brine-connector

Usage

Check out the example files to see the complete workflow.

Quick examples:

REST API Example

// rest_api.ts
import { AxiosError } from 'axios';
import * as dotenv from 'dotenv';
import {
  Client,
  isAuthenticationError,
  CreateOrderNonceBody,
  Response,
} from '@brine-fi/brine-connector';

dotenv.config();

const main = async () => {
  // load your privateKey and walletAddress
  const privateKey = process.env.PRIVATE_KEY;
  const ethAddress = process.env.ETH_ADDRESS;

  if (privateKey && ethAddress) {
    // handle in try catch block
    try {
      // create a rest client instance (you can pass baseUrl if it has changed)
      const client = new Client();

      //you can use public endpoints right away
      const test = await client.testConnection();
      const candleStick = await client.getCandlestick({
        market: 'ethusdc',
        period: 120,
      });

      // login to use private endpoints
      const loginRes = await client.completeLogin(ethAddress, privateKey);

      // create an order nonce
      const nonceBody: CreateOrderNonceBody = {
        market: 'btcusdt',
        ord_type: 'market',
        price: 29580.51,
        side: 'buy',
        volume: 0.0001,
      };

      // create order (private)
      const order = await client.createCompleteOrder(nonceBody, privateKey);
      const orders = await client.listOrders();
      console.log(orders.payload[0]);

      // get profile info (private)
      const profile = await client.getProfileInfo();
      console.log(profile.payload.username);
    } catch (e) {
      // Error: AuthenticationError | AxiosError
      if (isAuthenticationError(e)) {
        console.log(e);
      } else {
        console.log(e as AxiosError<Response<string>>);
      }
    }
  }
};

main();

Websocket Example

// websocket.ts
import { Client, WsClient } from '../src';
import * as dotenv from 'dotenv';
import { isAuthenticationError } from '@brine-fi/brine-connector';
dotenv.config();

const main = async () => {
  try {
    const privateKey = process.env.PRIVATE_KEY;
    const ethAddress = process.env.ETH_ADDRESS;
    // create a rest client instance if you need to create a private websocket
    const client = new Client();
    if (privateKey && ethAddress) {
      // create a public websocket instance
      const wsClient = new WsClient('public');
      // check to see if connected
      await wsClient.connect();
      // subscribe to streams
      await wsClient.subscribe([
        'btcusdc.trades',
        'btcusdc.ob-inc',
        'btcusdc.kline-5m',
      ]);
      // unsubscribe to streams
      await wsClient.unsubscribe(['btcusdc.trades']);
      // operate on ws member
      wsClient.ws.on('message', (data) => {
        console.log(data.toString());
      });
      await wsClient.disconnect();

      // login to get jwt access token
      const loginRes = await client.completeLogin(ethAddress, privateKey);
      // create a private websocket instance
      const wsClientPrivate = new WsClient('private', loginRes.token.access);
      // check if connected
      await wsClientPrivate.connect();
      // subscribe to streams
      await wsClientPrivate.subscribe(['trade', 'order']);
      // operate on ws member
      wsClientPrivate.ws.on('message', (data) => {
        console.log(data.toString());
      });
    }
  } catch (e) {
    if (isAuthenticationError(e)) {
      console.log(e);
    } else {
      console.log(e);
    }
  }
};
main();

Github repo

Please check out the full implementation here to find all the rest api methods, websocket methods and examples.

Last updated