TM
Blog

Using the Alpaca API with Node.js

Hoss@Hossapp /

Using the Alpaca API with Node.js

In this tutorial, we’ll show you how to get started with the Alpaca API when developing in Node.js. We’ll cover downloading the important libraries, integrating the Hoss SDK, and executing trades.

Installing and Importing Libraries

Before getting started with the API, you will need to install a few libraries. This can be done via the npm package manager - so run ‘npm install --save @alpacahq/alpaca-trade-api' in your terminal. The requests library enables us to make HTTP requests to use the API. If you want to sign up for Hoss - which provides you with deep visibility into API performance, alerts you of errors before your customers notice, and reduces the amount of time spent debugging integrations - you can do so here. To install Hoss, you should run ‘npm install hoss’. We are going to be using Hoss to make sure that calls are being made correctly, and to better analyze the information we send in as well as the response coming back from the API.

const hoss = require('hoss')
const Alpaca = require('@alpacahq/alpaca-trade-api')

Integrate the Hoss SDK

Here, we integrated the Hoss SDK. It's as simple as writing these two lines!

You can find your Hoss keys in your settings here. There are three keys for development, production, and staging respectively. We are using our development key here.

hoss("<HOSS API KEY>")

Getting Started With Alpaca

Now, go over to your Alpaca account, and note down your Alpaca API public and private keys. If you haven’t done so already, you will need to create an Alpaca Developer account. You can find that once you have logged in to your Alpaca account here. We’ll be working in the paper trading environment, as we are not deploying anything to production - so keep that in mind.

Be sure to define some constants to avoid retyping or repasting lengthy credentials and URLs. These constants will be used throughout the tutorial to make requests. We'll create

const alpaca = new Alpaca({
  keyId: 'YOUR_KEY_ID',
  secretKey: 'YOUR_SECRET_KEY',
  paper: true,
  usePolygon: false
})

Placing an Order

You can place an order with this endpoint.

createOrder({
  symbol: TICKER_SYMBOL,
  qty: NUMBER_OF_SHARES,
  side: 'buy' | 'sell',
  type: 'market' | 'LIMIT' | 'stop' | 'STOP_LIMIT' | 'trailing_stop',
  time_in_force: 'day' | 'gtc' | 'opg' | 'ioc',
  limit_price: number, //optional,
  stop_price: number, //optional,
  client_order_id: string, //optional,
  extended_hours: boolean, //optional,
  order_class: string, //optional,
  take_profit: object, //optional,
  stop_loss: object, //optional,
  trail_price: string, //optional,
  trail_percent: string //optional,
}) => Promise<Order>

The response is as follows.

{
  "activity_type": "ACTIVITY_TYPE",
  "cum_qty": "CUM_QTY",
  "id": "ID",
  "leaves_qty": "LEAVES_QTY",
  "price": "PRICE",
  "qty": "QTY",
  "side": "OPERATION",
  "symbol": "SYMBOL",
  "transaction_time": "TIME",
  "order_id": "ORDER_ID",
  "type": "FILL"
}

View your Request History in Hoss

On the toolbar on the left side of the panel, you can select which API you want to view and then select “Requests”. On this page, you can also view important metrics about API performance, such as latency. We can see the API requests - clicking on the individual requests allows us to examine the headers, payloads, and other relevant information about the request and response.

Set Up Alarms

You can set up alarms to monitor API status, failed calls, and latency. We integrate with Slack and Pagerduty - allowing you to send notifications directly to the folks responsible for working with these API integrations.

Masking Fields

It should be noted that when developing with the Square API, you will receive personally identifiable information with the API responses. If you’re using the Hoss app to monitor your API usage, you may want to read more about masking here.

Next Steps

That’s it! You can access the full Alpaca API documentation here and get started building your application!

Subscribe to Hoss ModeA weekly newsletter with curated articles, community discussions, news and trends.

Using the Alpaca API with Python

Hoss
Read more
In this tutorial, we’ll show you how to get started with the Alpaca API when using Python. when We’ll go over installing libraries, integrating the Hoss SDK, and executing trades.

Using the Kucoin API with Python

Hoss
Read more
Kucoin provides an API to obtain data from financial institutions for clients, advisors, and other entities - making it easier to connect bank accounts to applications. It was acquired by Plaid in 2019.
TM

Copyright © Hoss Technologies, Inc. 2020 - All rights reserved. Terms of Service & Privacy Policy