# Node.js Client for TD-API This article will explain how to use Node.js bindings for REST API. div b Beta Product This module is still under active development. Some REST API bindings have not been implemented yet. ## Prerequisites - Basic knowledge of Treasure Data, including the [Toolbelt](https://toolbelt.treasuredata.com/). ## Setup The module is published in [NPM](https://www.npmjs.com/package/td). Add the following line to your `packages.json` file. ```java "td": ">=0.2.7" ``` ## Basic Use ### List Databases and Tables The example below prints out all your tables to the console. ```java var TD = require('td'); var client = new TD('TREASURE_DATA_API_KEY'); client.listDatabases(function(err, results) { var i; var fnPrint = function(err, results) { console.log(results); }; if (err || !results.databases) { return; } for (i = 0; i < results.databases.length; i++) { client.listTables(results.databases[i].name, fnPrint); } }); ``` ### Issue Queries The example below issues a query from Node.js. After issuing the query, a `job_id` field is printed out. This can be used to query the results. ```java var TD = require('td'); var client = new TD('TREASURE_DATA_API_KEY'); client.hiveQuery('testdb', "SELECT code, COUNT(1) AS cnt FROM www_access GROUP BY code", function(err, results) { console.log(results); }); ``` ### List and Get the Status of Jobs The example below lists and gets the status of jobs. ```java var TD = require('td'); var client = new TD('TREASURE_DATA_API_KEY'); var fnPrint = function(err, results) { console.log(results); }; // list recent 20 jobs client.listJobs(fnPrint); // recent 127 jobs of specific status client.listJobs(0, 127, 'running', fnPrint); client.listJobs(0, 127, 'success', fnPrint); client.listJobs(0, 127, 'error', fnPrint); client.listJobs(0, 127, 'error', fnPrint); // get job status client.showJob(job_id, fnPrint); // get job result client.jobResult(job_id, fnPrint); ``` ## API reference For more details, refer to [Treasure Data REST API Client](https://treasure-data.github.io/td-client-node/docs/TDClient.html). ## Further Reading - The source code is on [GitHub](https://github.com/treasure-data/td-client-node). - The [Hive Query Engine](https://docs.treasuredata.com/display/public/PD/About+Hive+Query+Engine)