Getting data from restful API in Node (server to server) Node/Express Using Request
Solution 1
If you look at the end of the documentation you cited, you will notice that the request is:
GET https://api.tradegecko.com/products
This is a RESTful API. You need to read about how to provide the access token.
Typically, you add a header to your request that says:
Authorization: Bearer your token
...and do a simple GET request.
Their documentation for authentication gives details on how to do the authentication. It is RESTful, so my assumption is correct.
TradeGecko uses API keys to allow access to the API. You can register a new TradeGecko API key at our developer portal.
TradeGecko expects for the API key to be included in all API requests to the server in a header that looks like the following:
Authorization: Bearer <ACCESS_TOKEN>
You must replace with your personal API key.
This stack overflow answer has more details regarding performing REST requests from nodejs.
Got it to work using this: if you would like to add this to your answer for others.
myRouter.route('/testRoute')
.get(function(req, res){
request({
method: 'GET',
uri: 'https://api.tradegecko.com/products',
headers: {'Authorization': 'Bearer ' + 'TOKEN HERE'}
}, function (error, response, body){
if(!error && response.statusCode == 200){
res.json(body);
}
})
});
Solution 2
You must send request with ACCESS_TOKEN
request({
url: "",
method: "",
headers: {'Authorization': 'Bearer ' + YOUR_ACCESS_TOKEN},
})
Kevin Zaki
Updated on June 05, 2022Comments
-
Kevin Zaki almost 2 years
I am trying to connect to TradeGecko API located here: (all their examples are in ruby - I am using Node)
http://developer.tradegecko.com/
I have a privileged access token that I created FAKE TOKEN: 146e29b59e7e8861f462101f610f58168ca5edf2f307e5f7adc3314d24ee8015
How can I make a GET request to get all products for example: http://developer.tradegecko.com/?ruby#list-all-products
The help is much appreciated!
Let say I have some code like this:
var express = require('express'); var request = require('request'); var app = express(); var port = process.env.PORT || 3000; var testRouter = express.Router(); myRouter.route('/testRoute') .get(function(req, res){ request('API CALL HERE??', function (error, response, body){ if(!error && response.statusCode == 200){ res.json(body); } }) }); app.use('/api', bookRouter); var myRouter = express.Router(); app.get('/', function(req, res){ res.send('Welcome to my API'); }); app.listen(port, function() { console.log('GULP is running my app on PORT: ' + port); });
-
Kevin Zaki almost 8 yearsBoth answers helped me. Thanks got it up and running.
-
Kevin Zaki almost 8 yearsThanks I got it! Both answers helped me a lot.