<p><a href="https://en.bitcoin.it/wiki/Node.js_Example" rel="nofollow">The Bitcoin wiki has a nice example program</a>:</p>

<pre><code>var querystring = require('querystring'),
        https = require('https'),
        crypto = require('crypto');

function MtGoxClient(key, secret) {
        this.key = key;
        this.secret = secret;
}

MtGoxClient.prototype.query = function(path, args, callback) {
        var client = this;

        // if no args or invalid args provided, just reset the arg object
        if (typeof args != "object") args = {};

        // generate a nonce
        args['nonce'] = (new Date()).getTime() * 1000;
        // compute the post data
        var post = querystring.stringify(args);
        // compute the sha512 signature of the post data
        var hmac = crypto.createHmac('sha512', new Buffer(client.secret, 'base64'));
        hmac.update(post);

        // this is our query
        var options = {
                host: 'mtgox.com',
                port: 443,
                path: '/api/' + path,
                method: 'POST',
                agent: false,
                headers: {
                        'Rest-Key': client.key,
                        'Rest-Sign': hmac.digest('base64'),
                        'User-Agent': 'Mozilla/4.0 (compatible; MtGox node.js client)',
                        'Content-type': 'application/x-www-form-urlencoded',
                        'Content-Length': post.length
                }
        };

        // run the query, buffer the data and call the callback
        var req = https.request(options, function(res) {
                res.setEncoding('utf8');
                var buffer = '';
                res.on('data', function(data) { buffer += data; });
                res.on('end', function() { if (typeof callback == "function") { callback(JSON.parse(buffer)); } });
        });

        // basic error management
        req.on('error', function(e) {
                console.log('warning: problem with request: ' + e.message);
        });

        // post the data
        req.write(post);
        req.end();
};

var client = new MtGoxClient('mykey', 'mysecret');
client.query('1/BTCUSD/public/ticker', {}, function(json) {
        // do something
        console.log(json);
});
</code></pre>

<p><a href="https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1" rel="nofollow">Here's documentation for the v1 API</a></p>

CC BY-SA 3.0
2013-03-28T02:30:51.543Z
2013-03-28T02:30:51.543Z
Nick ODell
<p>Wanted to add to this, for the streaming/ws API the wiki links to a git repo from ralphtheninja, who has provided a library for a <a href="https://github.com/ralphtheninja/goxstream" rel="nofollow">node.js readable stream for mtgox.</a></p>

<p>Basically it converts the stream to a node.js stream.Readable object.</p>

CC BY-SA 3.0
2013-08-16T15:56:29.053Z
2013-08-16T15:56:29.053Z
Neil Neyman
<p>I have been looking for some help building a platform using node that works with the MtGox api. It seems easy to make request, but I am confused about how to pass a parameter.</p>

<p>Right now my code starts off like below. I can hit the server and get basic market data but I don't know how to do anything else....</p>

<pre><code>request = require('request');

APIResponder = require('../libraries/apiresponder');

exports.market_data = function(req, res, next){
  request('https://mtgox.com/api/1/BTCUSD/ticker', function (err, response, body) {
    console.log(response);
    console.log(body);
    APIResponder.respond(res, response);
  });
};
</code></pre>

<mtgox><api>
Is there any useful documentation on using the MtGox API with node.js?
Andrew Font
# Is there any useful documentation on using the MtGox API with node.js?

(Asked by: username_0 on 2013-01-14)

I have been looking for some help building a platform using node that works with the MtGox api. It seems easy to make request, but I am confused about how to pass a parameter.

Right now my code starts off like below. I can hit the server and get basic market data but I don't know how to do anything else....

```
request = require('request');

APIResponder = require('../libraries/apiresponder');

exports.market_data = function(req, res, next){
  request('https://mtgox.com/api/1/BTCUSD/ticker', function (err, response, body) {
    console.log(response);
    console.log(body);
    APIResponder.respond(res, response);
  });
};

```

**Tags:** mtgox, api


---

## 2 answers

### Answer by username_1 on 2013-03-28. Score: 2 | Accepted answer

[The Bitcoin wiki has a nice example program](https://en.bitcoin.it/wiki/Node.js_Example):

```
var querystring = require('querystring'),
        https = require('https'),
        crypto = require('crypto');

function MtGoxClient(key, secret) {
        this.key = key;
        this.secret = secret;
}

MtGoxClient.prototype.query = function(path, args, callback) {
        var client = this;

        // if no args or invalid args provided, just reset the arg object
        if (typeof args != "object") args = {};

        // generate a nonce
        args['nonce'] = (new Date()).getTime() * 1000;
        // compute the post data
        var post = querystring.stringify(args);
        // compute the sha512 signature of the post data
        var hmac = crypto.createHmac('sha512', new Buffer(client.secret, 'base64'));
        hmac.update(post);

        // this is our query
        var options = {
                host: 'mtgox.com',
                port: 443,
                path: '/api/' + path,
                method: 'POST',
                agent: false,
                headers: {
                        'Rest-Key': client.key,
                        'Rest-Sign': hmac.digest('base64'),
                        'User-Agent': 'Mozilla/4.0 (compatible; MtGox node.js client)',
                        'Content-type': 'application/x-www-form-urlencoded',
                        'Content-Length': post.length
                }
        };

        // run the query, buffer the data and call the callback
        var req = https.request(options, function(res) {
                res.setEncoding('utf8');
                var buffer = '';
                res.on('data', function(data) { buffer += data; });
                res.on('end', function() { if (typeof callback == "function") { callback(JSON.parse(buffer)); } });
        });

        // basic error management
        req.on('error', function(e) {
                console.log('warning: problem with request: ' + e.message);
        });

        // post the data
        req.write(post);
        req.end();
};

var client = new MtGoxClient('mykey', 'mysecret');
client.query('1/BTCUSD/public/ticker', {}, function(json) {
        // do something
        console.log(json);
});

```

[Here's documentation for the v1 API](https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1)


---

### Answer by username_2 on 2013-08-16. Score: 0

Wanted to add to this, for the streaming/ws API the wiki links to a git repo from ralphtheninja, who has provided a library for a [node.js readable stream for mtgox.](https://github.com/ralphtheninja/goxstream)

Basically it converts the stream to a node.js stream.Readable object.


---

