Manually Trading Stocks Using Postman and the Alpaca API

How to get started and test the Alpaca trading API

Want to trade with Alpaca, but you aren’t fully sure how the Alpaca API works yet? Here’s a step by step tutorial on testing the Alpaca API by trading manually using the Postman interface.

Before You Begin…

  • Retrieve your Alpaca Keys
    If you don’t have one, go here to sign up. Alpaca provides commission free trading. You can also test out Alpaca’s features with unlimited paper trading. No real money necessary!

  • Download Postman and sign up for an account
    Postman is a popular API testing and development tool. It’s free to sign up and use.

  • Take a look at Alpaca’s API Documentation and have it open for reference

Getting your API Keys

Once you’ve signed up for Alpaca, you can generate your API key on the homepage. Be sure to take note of your paper trading Key ID and Secret Key and save it somewhere private. Once you navigate away from this page or refresh, your Secret Key will disappear. (You can always regenerate your keys. This will cause your old keys to no longer work, so be careful!)

Alpaca’s Paper Trading allows you to test various trading strategies or integrations without using real money! Every Alpaca account comes with unlimited paper trading so you can get started without worrying about starting capital.

Setting Up Postman

Now that you have your keys ready, open up the Postman application.

  • To initiate your first API request, click the + tab in the Postman UI.
  • Add the following request URL:
    https://paper-api.alpaca.markets/v2/account
  • Add your API keys
    Click the Headers section and add two keys called APCA-API-KEY-ID and APCA-API-SECRET-KEY . Enter in your key values in the VALUE column
  • Hit the Send button

You should be able to see various account information from this API call. Now you’re ready to trade!

Getting Asset Information

Now that you’ve made your first API call, you’re ready to make your first paper trade. In the example below, I will be buying and selling 1 share of GOOGL .

First, I’ll use the v2/assets/{symbol} endpoint to checkout the GOOGL asset (see image below). It’s active and tradable, so I’m good to initiate my trade.

Place your first (Paper) trade!

Next, I’ll be placing a market order for 1 share of GOOGL with time in force = day. To do this, I am changing the request to POST , changing the endpoint to be /v2/order , and entering the following JSON body:

{
"symbol": "GOOGL",
"qty": 1,
"side": "buy",
"type": "market",
"time_in_force": "day"
}

Make sure that you enter your order information through a JSON message body. (The API does not accept orders through query params.)

And there we go! My first order through Alpaca API. I can confirm that the order went through on my Alpaca dashboard. (Although I lost $0.50 on this order :stuck_out_tongue_winking_eye:. Keep in mind that this is an example and is for illustrative purposes only. Past performance is not indicative of future results. )

I can also check my position through API, by using the GET v2/positions endpoint.

Or, I can visit my positions page on my Alpaca dashboard to check all of my positions.

Closing My Position

Now that I’ve tested opening a position (buying 1 share of GOOGL ), I’m ready to sell it to close my position.

To do this, I am going to use the DELETE v2/positions/GOOGL endpoint. Because I’m holding a long position on GOOGL , calling this endpoint will place a market sell order.

After I hit send, the following information is displayed, showing that my order has been placed, but not yet filled.

Moments later, I can see that my market sell order has been filled.

This is what my dashboard looks like. I may have lost $0.08 in fake money, but that was a fun learning experience! :tada: (You should test a strategy yourself in paper trading to see if and how it works before trying it in a live brokerage account.)

Now What?

Now that I’ve tested that I can buy and sell through the Alpaca API, I can explore different ways to use it. I can buy and sell real stocks by signing up for a live brokerage account, switching the API end point to api.alpaca.markets , and entering my live account API keys.

You can also checkout some of the other cool ways people are using the Alpaca API on Alpaca’s Forum or Alpaca’s Medium Publication.

Manage Your Stocks from Google Spreadsheet Using API
You might think API trading is only for hard-core HFT programmers. That’s not true! Use a spreadsheet app with some… medium.com

Building a Bot to Tweet my Trades
How I utilized Alpaca’s V2 API with Twython to live tweet my trades medium.com

The world is your oyster!


Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC, member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.

1 Like