Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write json file to Firebase with Node.js?

I have a third party API that I need to call every 5 seconds. I get JSON as response, and I'd like to write the JSON content to a Firebase node using Node.js. Based on Firebase examples I could import data with this code:

var usersRef = ref.child("users");
usersRef.set({
  alanisawesome: {
    date_of_birth: "June 23, 1912",
    full_name: "Alan Turing"
  },
  gracehop: {
    date_of_birth: "December 9, 1906",
    full_name: "Grace Hopper"
  }
});

Curl examples worked too. But what I really wanted to do is to import a third party API response directly to my Firebase database using the API endpoint. How can i do it with Node.js?

like image 471
Speed_John Avatar asked Jan 21 '26 02:01

Speed_John


1 Answers

First, you need to make a request to the api endpoint and receive the data. Then, you can send that json data to firebase

var request = require('request');

var usersRef = ref.child("users");

request('<your_endpoint>', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var asJson = JSON.parse(body)
    usersRef.set(asJson)
   }
})
like image 146
David Avatar answered Jan 22 '26 17:01

David