Questrade API nodejs wrapper

I wanted to write a script using the Questrade API to help me rebalance my portfolio. I guess wrapping the rest calls behind a function wrapper is a good start. Here is what I started, feel free to help me out, or just take a look:

https://github.com/dk1027/questrade.js

questrade.js is the wrapper around the REST API. When you instantiate it, pass the result of redeeming the refresh token to the constructor, as shown in questapp.js.

I want to experiment with the API initially so I wrote questapp.js. What I do is load it in the node repl and call functions in questrade.js. Something like this:

ltse@ltse-ubuntu:~/questrade_sample$ node
> .load questapp.js
> ...noise ommitted...
> q.accounts()
undefined
> { accounts: 
   [ { type: 'Margin',
       number: '12345678',
       status: 'Active',
       isPrimary: true,
       isBilling: true,
       clientAccountType: 'Individual' },
     { type: 'TFSA',
       number: '2345678',
       status: 'Active',
       isPrimary: false,
       isBilling: false,
       clientAccountType: 'Individual' } ],
  userId: 34567 }

undefined
> 

Come to think of it, wouldn’t it be nice if there is something that would auto-generate functions base on a rest api definition? Someone probably has came up with it already…

Update:

Questrade.js updated with example

Advertisements

Questrade API sample code with nodejs and request

Questrade released an API to retrieve account information, market data, and even making trades. That’s awesome! They have helpful documentation and even a developer console to make the API calls. Here is some sample code to use their API. Remember to paste your own refresh token (their Getting Started page provides instructions on how to generate it)

var request = require('request');

var refresh_token='YOUR TOKEN HERE';

// This gets us the access token for authorizing subsequent requests
console.log("getting access token");
request.get(
	{url:'https://login.questrade.com/oauth2/token?grant_type=refresh_token&refresh_token=' + refresh_token},
	function(err, response, body){
		if(err){
			console.log(err);
			return;
		}

		console.log(response.statusCode);

		if(response.statusCode != 200){
			console.log(body); // If we get here, it is likely to be a 'bad request' because we are using the same refresh_token more than once
			return;
		}

		b = JSON.parse(body)
		var access_token = b.access_token;
		var api_server = b.api_server;

		// Let's try to get our account information
		if(response.statusCode == 200)
		{
			console.log('Make a request');
			request.get(
				{
					url: api_server + 'v1/accounts',
					auth:{
						bearer : access_token
					}
				},
				function(err, response, body){
					if(err){
						console.log(err);
						return;
					}
					console.log(response.statusCode);
					console.log(body);
				}
			);
		}
	}
);

This code redeems the refresh token for an access token, and then use the access token to make a get request to v1/accounts to get account information. You should be able to reuse the access token for other requests; however you can only use the refresh token once. If you use it more than once, you will get a 400 bad request.