Brine's Docupaper
AboutTrade
  • ABOUT
    • ๐Ÿ“’Brine Docupaper
    • ๐Ÿช™Tokenomics
  • ๐Ÿ—ž๏ธBrine Bytes
    • ๐ŸงตOctober Edition
  • TECH
    • ๐Ÿ‘ฉโ€๐Ÿ’ปTech Docupaper
      • ๐ŸจHigh level architecture
      • ๐Ÿค”Starkware logic
      • ๐ŸŒŠTransactional flow
        • Off-chain accounts
        • The deposit flow
        • The withdrawal flow
        • The trade flow
        • The transfer flow
        • Full withdrawal
    • ๐Ÿ”ŒAPI Documentation
      • ๐Ÿ˜‹Getting started
        • Test connectivity
      • ๐Ÿ“ˆMarket
        • 24hr Tickers Price
        • K-line/Candlestick data
        • Orderbook
        • Recent trades
      • โ†”๏ธTrading
        • Create order
        • Get order
        • List orders
        • Cancel order
        • List trades
      • ๐Ÿ”Account
        • Login
        • Profile Information
        • Balance details
        • Profit and loss details
      • ๐ŸŒWeb-socket stream
        • ๐Ÿ—๏ธPrivate web-socket connection
        • ๐Ÿ‘ฅPublic websocket connection
      • โœจInternal Transfer
        • Create Internal Transfer
        • Get Internal Transfer
        • Check User Existence
        • List Internal Transfers
      • ๐ŸงชSDK Reference
        • NodeJS SDK
  • FOR USERS
    • ๐Ÿ“’User Docuguide
      • ๐Ÿ’ปWeb App
        • Sign up through Metamask
        • Sign up through Wallet Connect
        • Place a Market Order Trade
        • Place a Limit Order Trade
        • Refer Brine to a Friend
        • Deposit with Metamask
        • Withdraw to Metamask
  • LEGAL
    • โ€ผ๏ธDisclaimer
Powered by GitBook
On this page
  • Installation
  • Usage
  • Quick examples:
  • REST API Example
  • Websocket Example
  • Github repo

Was this helpful?

  1. TECH
  2. API Documentation
  3. SDK Reference

NodeJS SDK

This wrapper facilitates the use of the Brine api.

PreviousSDK ReferenceNextUser Docuguide

Last updated 1 year ago

Was this helpful?

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 to find all the rest api methods, web-socket methods and examples.

Installation

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

Install from npm

npm i @brine-fi/brine-connector

Usage

Check out the 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 to find all the rest api methods, websocket methods and examples.

๐Ÿ”Œ
๐Ÿงช
here
Brine Website
example
here