Skip to content

Instantly share code, notes, and snippets.

Last active July 31, 2019 19:56
What would you like to do?
SWEN 262 - Interfacing with Yahoo's Finance API

Using Yahoo's Finance API

It was surprisingly hard to find a simple straight forward stock API; this community built API hosted by Yahoo does the job.

Let's get to it.

Below is a complete URL example if you want to play on your own and figure it out.*

Interactive Dashboard

Theres an awesome interactive dashboard that you should play with. If you still need a breakdown on what's happening, then keep reading.

Let's digest the url.

All urls have a BASE and then PARAMETERS. The Parameter sequence starts with a ? and are delimited with an &.

So you would have something like:

This is the BASE URL

Then comes the first parameter, the QUERY. This query is not directly SQL instead its something Yahoo calls Yahoo Query Language or YQL. For the purposes of the activity; they are very similar and hence straightforward.


The above query translates into SELECT * FROM WHERE SYMBOL IN ("AAPL"). The reason the %20's are there is because to send the space character through a browser it needs to be URL Encoded, but you probably won't be needing to do that.

Note: We are selecting everything with SELECT * ... but we can also select specific field(s). For the project we only really want LastTradePriceOnly so we can say SELECT LastTradePriceOnly ... and its way cleaner.

The final parameter is &env=store:// So long as you include this you will be ok. The reason this is needed is because it's a community kept database so it needs a specific env or environment.

Parsing Yahoo's API with JAVA

We need two main components to make this happen in Java:

  1. A HTTP Library for Java that will allow us to make request to the internet
  2. XML Parser to read in the information.

HTTP Java Library

Java has a built in (library)[] to make HTTP Requests called There are other ones like (apache)[] but that's beyond the scope.

See the file as an example of how to make the GET request for the API.

XML Parsing

The data returned is in XML format; it is literally like getting a file written in XML and then parsing it. In Java there are a few ways to parse XML:

  • SAX
  • JAXP

Click on the (following tutorial)[] on how to parse XML.

Do you like JSON Better?

The data returns in XML format by default, if you want JSON then add the following parameter to the url:


Note: Java has more robust standard libraries for parsing XML than JSON.

public class RequestAPIExample {
public static void main(String[] args) throws IOException {
String url = "*";
// Create a URL and open a connection
URL YahooURL = new URL(url);
HttpURLConnection con = (HttpURLConnection) YahooURL.openConnection();
// Set the HTTP Request type method to GET (Default: GET)
// Created a BufferedReader to read the contents of the request.
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
// response is the contents of the XML
Copy link

jcdesimp commented Oct 8, 2015


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment