You've successfully subscribed to Alpaca Learn - Developer API Trading Tutorials, Code Samples
Great! Next, complete checkout for full access to Alpaca Learn - Developer API Trading Tutorials, Code Samples
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.
Search
Broker API

How to Pass an Order on Behalf of the End-User by Broker API

Alpaca Team
Alpaca Team
How to Pass an Order on Behalf of the End-User by Broker API

This tutorial is a step-by-step demonstration of how to fund an account via the Broker API and is part of the Alpaca 101 Tutorial Series. We will move forward assuming that you’ve registered on the Alpaca website. If you haven’t please visit Alpaca and sign up. Feel free to check out the other tutorials in this series at Alpaca Resources.

Please note that this tutorial reflects a sandbox testing environment. If you decide to transition to a live production environment, features and entitlements may differ to ensure handling of clients’ live accounts are compliant with all applicable rules and regulations.


What is Broker API?

Alpaca is built for developers and traders. That being said we are incredibly excited to announce we have established our very own broker product that can evolve alongside the Alpaca platform. Alpaca users can develop in sandbox environments, allowing users to access all the functionality of an operational brokerage in parallel with becoming compliant with applicable rules and regulations should you decide to transition to a live production environment. We are releasing a series of tutorials to provide guidance on the core features of the Broker API, so make sure to check out Alpaca Resources. Let’s get started!

Alpaca Broker API - Launch your own commission-free trading app
Alpaca is a modern platform for trading. Alpaca’s API is the interface for your trading algorithms, bots, or applications to communicate with Alpaca’s brokerage and other services.

How to Pass an Order on Behalf of the End-User

Step 1: Broker Dashboard

Similar to the Algo Dashboard, the Broker Dashboard displays all of the information relevant to one’s broker account. The main page, shown below, includes an overview of the number of active accounts, assets under management (AUM) along recent signups, transactions, and orders. When logging in for the first time, you will notice there is not any information present in the dashboard as below

Broker Dashboard Overview

There are also subpages for the firm’s balance, active accounts, transactions (orders and ACH/Wire transfers), and documents.


Step 2: Live Testing

To set up your environment first navigate to the API/Devs page where there is a box in the right-hand corner called Seed Env. By clicking this, your environment will be populated with random test users. This allows you to start testing different API calls without needing to create user accounts in your environment. After this, your dashboard should contain randomly generated test signups and amounts under Assets Under Management (AUM).

Broker Dashboard with random test users

Now that the sandbox environment contains users, we can move on to testing out the API calls. Navigate first to the API/Devs page and then the Live Testing page which will look like this

Live Testing

This will be the medium in which we test the API call in the next section.


Step 3: Sending the Request Body

Let’s create a raw request to pass an order on behalf of the end-user below. In this tutorial, we will be placing a market order to buy 4 shares of AAPL with time_in_force = day. As we want to pass an order for one of our end users we need to specify the  account_id in our endpoint: v1/trading/accounts/{account_id}/orders

It is worth noting that the Trading API (for Broker API) works the same way as the public Trading API, but the path differs to include the account id, and the request is authenticated using the Broker API key.

API v2 - Documentation | Alpaca
Alpaca API lets you build and trade with real-time market data for free.
Alpaca Trading API

Find the JSON body for our request below

{
 "symbol": "AAPL",
 "qty": 4,
 "side": "buy",
 "type": "market",
 "time_in_force": "day"
}
Sample Request

Step 4: Response for a Successful Request

If all has been successful one will receive the following response object which includes the same properties as the order response object one receives from the Trading API along with the additional property of ‘commission. This allows the developer to add commissions if desired when testing the API. For more information please see our Broker Documentation.

{
 "id": "18845d16-bc8b-4ee4-8e85-7cf7fd57e892",
 "client_order_id": "3d2d73d2-7eee-4561-99aa-f5ab81c98723",
 "created_at": "2021-02-24T14:14:05.065835Z",
 "updated_at": "2021-02-24T14:14:05.065835Z",
 "submitted_at": "2021-02-24T14:14:05.040862Z",
 "filled_at": null,
 "expired_at": null,
 "canceled_at": null,
 "failed_at": null,
 "replaced_at": null,
 "replaced_by": null,
 "replaces": null,
 "asset_id": "93f58d0b-6c53-432d-b8ce-2bad264dbd94",
 "symbol": "AAPL",
 "asset_class": "us_equity",
 "qty": "4",
 "filled_qty": "0",
 "filled_avg_price": null,
 "order_class": "",
 "order_type": "market",
 "type": "market",
 "side": "buy",
 "time_in_force": "day",
 "limit_price": null,
 "stop_price": null,
 "status": "accepted",
 "extended_hours": false,
 "legs": null,
 "trail_percent": null,
 "trail_price": null,
 "hwm": null,
 "commission": "0"
}
Sample Successful Response 

Great! Now, moving forward here are some important points to remember:

  • Trading API for Broker API works the same way as the public Trading API
  • Trading API is extended for fractional shares trading, meaning you can pass fractional orders on behalf of end-user
  • To see the status of the order for your end-user you can GET /v1/assets/account_id with the particular account_id

Thank you for using Alpaca. Here are a couple more links that you might be interested in.

Alpaca GitHub repository

Alpaca
Alpaca builds an API for free stock trading. Alpaca has 42 repositories available. Follow their code on GitHub.

Alpaca Blog

Alpaca Blog | API for Stock Trading
Alpaca Blog for product updates, new integration, and more. Alpaca’s commission-free trading API is built for algorithmic trading and building apps

Alpaca Forum

Alpaca Community Forum
Alpaca user forum for FAQ, feature requests, many discussions around Alpaca stock trading API

Alpaca API documentation

Documentation | Alpaca
Alpaca API lets you build and trade with real-time market data for free.

Sign up for the weekly newsletter to keep up with the API updates and upcoming competitions,  job opportunities by clicking here.

You can also follow Alpaca and our weekly updates on our LinkedIn, Alpaca Community Slack, and @AlpacaHQ on Twitter!

Commission-Free trading means that there are no commission charges for Alpaca self-directed individual cash brokerage accounts that trade U.S. listed securities through an API. Relevant SEC and FINRA fees may apply.

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



Interested in learning more about Broker API?

Don't miss out on any updates about our Broker API suite of solutions! Share your details with us and be the first to know about our latest content.

Broker API

Alpaca Team

API-first stock brokerage. *Securities are offered through Alpaca Securities LLC* http://alpaca.markets/#disclosures