Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

Request Data in Json format over an API via Node.js

Hello, 

 

I'm a beginner in programming and want to build my first project. I would be glad to find someone who is willing to help me. 

 

First step should be to request Data using the following curl comand with a Node.js Server:

curl -u 123login:456passwd https://api.sigfox.com/v2/devices/{id}/messages

 I'm not sure if there is a way to execute this comand with a node package or if I can program it with javascribt language. 

 

In the Json file there are particular Information which I want to save in a postgres SQL database. 

 

Does anyone have any suggestion how I can start?  Or maybe a whole different Idea how i get to handle it ? 

 

Thanks a lot in advance

 

KK

 

1 Reply
Pilot Lvl 2
Message 2 of 2

Re: Request Data in Json format over an API via Node.js

There is a much easier way to perform a GET method in Node.JS rather than directly executing a cURL command. By using the request NPM package, making API calls is now much easier! To use the package, direct to your project path and run the command below in a terminal:

 

npm install request

afterwards, the request module should now be installed in your project path and ready to use!

 

 

Now in your primary project file, your code should be setup in a way to allow for you to grab data from the SigFox API. Here is one option:

 

const request = require("request");
var id = "yourID";
var username = "yourUsername";
var password = "yourPassword"; var getAPIData = { method: "GET", url: `https://api.sigfox.com/v2/devices/{id}/messages`, auth: { "user": username, "pass": password } } request(getAPIData, function (error, response, body) { if (!error && response.statusCode == 200) { var data = JSON.parse(body); // the variable "data" holds the JSON response } }

^ While this call should work, you must fill in the variables at the top of code-file with your actual data. PLEASE BE AWARE THAT MOST OF THOSE VARIABLES ARE SECRET AND SHOULD BE KEPT AWAY FROM USERS WHILE ALSO STILL BEING CALLABLE IN THE CODE!

 

Once these methods are ran, your JSON response should be kept in the data variable in the local-function. From there, you should be able to get all the data you need!

 

I hope this has helped you! If you have any problems, please respond to this thread and I'll see if I have any other options and/or alternatives!

 

 

Gavin R. Isgar - Freelance Software Developer