1.0.4 • Published 11 months ago

@monarkmarkets/api-client v1.0.4

Weekly downloads
-
License
-
Repository
github
Last release
11 months ago

@monarkmarkets/api-client

This package provides a TypeScript client for interacting with the Monark Markets API.

Installation

npm install @monarkmarkets/api-client

Usage

To use this client, you'll need to set up a simple proxy server that forwards requests to the Monark API with the appropriate authentication.

Important: API Proxy Server Requirement

This client requires a server running under the /api path that forwards requests to the Monark API with authentication. This proxy server:

  1. Injects authentication credentials
  2. Forwards all requests to the Monark API
  3. Returns the responses to the client

Setting up a Proxy Server

Below is an example of a minimal Express.js server that forwards requests to the Monark API:

// server.js
import express from 'express';
import axios from "axios";
import * as dotenv from 'dotenv';
dotenv.config();

const app = express();

const API_BASE_URL = process.env.API_BASE_URL || 'https://demo-api.monark-markets.com';
const AUTH_TOKEN = process.env.AUTH_TOKEN;

// Parse JSON payloads
app.use(express.json());

// API router for forwarding requests
const apiRouter = express.Router();

apiRouter.use('*', async (req, res) => {
  try {
    // Build the target URL by combining base URL and original path
    let targetUrl = `${API_BASE_URL}${req.originalUrl}`;
    targetUrl = targetUrl.replace("/api/", "/");

    // Forward the request to the target API
    const response = await axios({
      method: req.method,
      url: targetUrl,
      data: req.body,
      headers: {
        // Add the authorization header
        'Authorization': `Bearer ${AUTH_TOKEN}`
      },
      // Forward query parameters
      params: req.query,
      // Handle binary responses
      responseType: 'arraybuffer'
    });

    // Set response headers
    Object.entries(response.headers).forEach(([key, value]) => {
      // Skip Transfer-Encoding header to prevent conflicts with Express
      if (key.toLowerCase() !== 'transfer-encoding') {
        res.set(key, value);
      }
    });

    // Send response
    res.status(response.status).send(response.data);

  } catch (error) {
    // Handle errors from the target API
    if (error.response) {
      res.status(error.response.status).send(error.response.data);
    } else {
      res.status(500).json({
        error: 'Internal Server Error',
        message: error.message
      });
    }
  }
});

// Mount API router - all API routes will be prefixed with /api
app.use('/api', apiRouter);

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`API proxy server running on port ${PORT}`);
});

Environment Variables

Create a .env file in your project root:

API_BASE_URL=https://demo-api.monark-markets.com
AUTH_TOKEN=your_auth_token_here

Using the Client

import { Client } from '@monarkmarkets/api-client';

// Initialize the client with the proxy API URL
const client = new Client('/api');

// Example: Get all PreIPOCompanies
async function getAllPreIPOCompanies() {
  try {
    // You can add optional parameters for search, pagination, etc.
    const result = await client.preIpoCompany2(
      undefined, // searchTerm (optional)
      undefined, // sortOrder (optional)
      1,         // page
      10         // pageSize
    );
    
    console.log(`Found ${result.pagination?.totalRecords || 0} companies`);
    console.log('Companies:', result.items);
    
    return result;
  } catch (error) {
    console.error('Error fetching PreIPO companies:', error);
    throw error;
  }
}

// Call the function
getAllPreIPOCompanies();

Authentication

The API client itself doesn't handle authentication. Instead, authentication happens at the proxy server level by including the AUTH_TOKEN in the forwarded requests.

API Methods

The client provides methods for all API endpoints in the Monark API. Some examples include:

  • preIpoCompany2() - Get all PreIPO companies
  • preIpoCompany(id) - Get a PreIPO company by ID
  • indicationOfInterestPOST(body) - Create an indication of interest
  • investorGET(id) - Get investor information
  • And many more...

For the full list of available methods, refer to the TypeScript definitions in the client code.

The full API documentation can be found here: https://api-docs.monark-markets.com/

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago