Skip to content
Jono edited this page Jul 23, 2018 · 18 revisions

Welcome to the (Unofficial) Robinhood Java Api!

Introduction

If you're considering using this wrapper, It's probably safe to assume that you're looking to build a system using the Robinhood investing service in some way or another, you're also likely interested in the stock market & programming, so we won't dwaddle on explaining terminology in detail. Most of the details pertaining to API responses come from the Unofficial Robinhood API and as such we will refer to their documentation when possible.

Thank you to Conrad Weiser for making the original project and Sanko for making the Unnoficial API Docs.

Thank you to all contributors, present & future; contributions are greatly appreciated.

We have a Discord server for the Robninhood API community. Feel free to join and ask questions or chat about coding or whatever.

O.K. Let's begin!



Getting Started

Installation

To install & use the Robinhood API, you can either install the latest release , download the latest source code & build the project yourself, or use JitPack to add the repository to your gradle, maven, sbt, or leiningen. You can download the latest stable build here.

The Robinhood API Depends on these projects & packages

Sample Gradle build:

plugins {
    id 'java'
    id 'application'
}
sourceCompatibility = 1.8

group 'my.group'
version '1.0'

repositories {
    jcenter()
    mavenCentral()
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.github.AquaticMasteryProductions:Robinhood-Api-Java:v0.8.3-alpha'
    compile group: 'io.github.openunirest', name: 'unirest-java', version: '2.2.04'
    compile group: 'commons-io', name: 'commons-io', version: '2.6'
    compile 'com.google.code.gson:gson:2.8.5'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
}

Basics of Using the API

To access any data from Robinhood, you must first create an instance of the RobinhoodApi class; this can be done in one of two ways: with or without login.

//Without login
RobinhoodApi api = new RobinhoodApi();
//With Login (THIS DOSE NOT WORK WITH MULTI/TWO-FACTOR AUTHORIZATION)
RobinhoodApi api = new RobinhoodApi("email", "password");

Each RobinhoodApi instance contains its own Configuration; when logged in, the Configuration holds information about the user account & authorization token -- used to access private data from Robinhood. If an unauthorized (not logged in) API instance makes a request for private account data (i.e. any account data), a RobinhoodNotLoggedInException is thrown. An API instance can be logged in at anytime after initialization with RobinhoodApi.login(email, password).

Click here for more information on logging in, Configurations, & Multi/Two-Factor Authentication accounts

A small note: neither the password or the email can be encrypted here in anyway, it just wont work, so make sure you use SSL/TLS (i.e. HTTP S) if you have users enter their password and send it to your server or whatnot.

Making Requests for Data

Quick Note: PaginatedIteration

Data returned from the Robinhood API is often paginated, meaning that additional requests are needed to obtain the entire list. To abstract from this, we made then PaginatedIterator and the method RobinhoodApi#buildIterable to automatically make those requests for you and make iterating through paginated lists a lot more straight forward. If you would like to learn more about our Paginated Iteration, read this. Now, let's continue.

Making Requests for Public Data*

Here is an example of how to use an API instance to get public market data from Robinhood. We will use Microsoft (MSFT) as an example, because...ya know... technology

//Instantiate an API instance, no need to log in
RobinhoodApi api = new RobinhoodApi();
//Get a market quote from Robinhood for Microsoft (MSFT)
TickerQuote msft = api.getQuoteByTicker("MSFT"); 
//Print out some details 
System.out.println( msft.getSymbol() + "\n" + msft.getAskPrice() );

This will print out:

MSFT
102.11

Woohoo We have connected to Robinhood successfully! Now let's move on to getting private data

* Note: Methods that do not require authorization are likely to be converted to static methods after v0.8.3 (unreleased).

Making Requests for Private Data

Making requests for private (account) data is much much harder...just kidding it's literally one extra step -- not even a new line of code! Let's get a user's name and purchased stocks.

//Instantiate an API instance AND login to the account
RobinhoodApi api = new RobinhoodApi("coolgal@email.ninja", "superSecretePassword");

//Get the user's basic info (name & url links to other information)
BasicUserInfo basicInfo = api.getBasicUserInfo();
//Then get the user's Positions (help stocks)
List<Position> positions = api.getAccountPositions(); 

//Then print out the user's first name and all their help stocks
System.out.println(basicInfo.getFirstName() + "'s owned Stocks:");
positions.forEach(position -> {  
	String stockName = positionElement.getInstrumentElement().getSimpleName();  
	System.out.println(stockName + " : " + positionElement.quantity);
});

Here is what the output might look like:

Coolgal's owned stocks:
Microsoft : 5
Bank of America : 10
Amazon : 20,000

Wow... Coolgal is ballin... -- anyway, you are now ready to dive into the Robinhood Java API all on your own! Good luck!

Exceptions & Errors

Name Desctription
RobinhoodApiException A generic exception thrown when an error occurrs within the API wrapper
RobinhoodNotLoggedInException Thrown at RunTime if an unauthorized instance tries to get account data (see login)
TickerNotFoundException Thrown if the requested ticker was not found on Robinhood
RequestTooLargeException An exception for API query requests that are too large (e.g. 20000 ticker request)