node.js - Request and save API data using setInterval in Javascript

Node.js - Request and save API data using setInterval in Javascript

To request and save API data periodically using setInterval in Node.js, you can follow these steps:

  1. Install Necessary Packages: You will need a package like axios or node-fetch to make HTTP requests. Additionally, you may need fs for saving data to a file.

  2. Write the Script: Use setInterval to periodically make requests and save the data.

Here is a complete example using axios and the native fs module:

Step 1: Install Required Packages

First, ensure you have Node.js installed. Then, create a new project and install the required packages.

mkdir api-data-fetcher cd api-data-fetcher npm init -y npm install axios 

Step 2: Write the Script

Create a file named fetchAndSave.js and add the following code:

const axios = require('axios'); const fs = require('fs'); const path = require('path'); // Function to fetch data from the API async function fetchData() { try { const response = await axios.get('https://api.example.com/data'); return response.data; } catch (error) { console.error('Error fetching data:', error); return null; } } // Function to save data to a file function saveData(data) { const filePath = path.join(__dirname, 'data.json'); fs.writeFile(filePath, JSON.stringify(data, null, 2), (err) => { if (err) { console.error('Error saving data:', err); } else { console.log('Data saved successfully'); } }); } // Function to fetch and save data async function fetchAndSaveData() { const data = await fetchData(); if (data) { saveData(data); } } // Set the interval to fetch and save data every 5 minutes (300,000 milliseconds) const interval = 300000; // 5 minutes setInterval(fetchAndSaveData, interval); // Optionally, you can call the function immediately to fetch data right away fetchAndSaveData(); 

Explanation

  • axios: Used to make HTTP requests to the API.

  • fs: Node.js built-in module to handle file operations.

  • path: Node.js built-in module to handle and transform file paths.

  • fetchData: Asynchronously fetches data from the specified API endpoint. If an error occurs, it logs the error and returns null.

  • saveData: Saves the fetched data to a data.json file in the same directory as the script. It formats the JSON with an indentation of 2 spaces for readability.

  • fetchAndSaveData: Combines fetchData and saveData to fetch the data from the API and then save it to the file.

  • setInterval: Calls fetchAndSaveData every 5 minutes (300,000 milliseconds).

Step 3: Run the Script

Run the script using Node.js:

node fetchAndSave.js 

This script will start fetching and saving API data every 5 minutes. If you want to stop the script, you can simply terminate the Node.js process (e.g., by pressing Ctrl+C in the terminal where it's running).

By following these steps, you will have a Node.js script that periodically requests data from an API and saves it to a file, using setInterval to handle the periodic execution.

Examples

  1. Node.js Periodic API Request and Save to File

    Description: Use setInterval to periodically fetch data from an API and save it to a file.

    const fs = require('fs'); const axios = require('axios'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); fs.writeFileSync('data.json', JSON.stringify(response.data)); console.log('Data saved'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 
  2. Node.js Fetch and Save API Data at Regular Intervals

    Description: Fetch data from an API every minute and append it to a file.

    const fs = require('fs'); const axios = require('axios'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); fs.appendFileSync('data.log', JSON.stringify(response.data) + '\n'); console.log('Data appended'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 
  3. Node.js Scheduled API Call and Database Insert

    Description: Use setInterval to fetch API data and insert it into a MongoDB database.

    const axios = require('axios'); const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017'; const dbName = 'mydatabase'; MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => { if (err) throw err; const db = client.db(dbName); const collection = db.collection('apidata'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); await collection.insertOne(response.data); console.log('Data inserted'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds }); 
  4. Node.js Periodic API Request and Save to JSON

    Description: Periodically request data from an API and save it to a JSON file.

    const fs = require('fs'); const axios = require('axios'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); fs.writeFileSync('data.json', JSON.stringify(response.data, null, 2)); console.log('Data saved to JSON'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 
  5. Node.js Use setInterval to Fetch and Save API Data

    Description: Use setInterval to regularly fetch data from an API and save it in a text file.

    const fs = require('fs'); const axios = require('axios'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); fs.writeFileSync('data.txt', response.data); console.log('Data saved to text file'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 
  6. Node.js Regular API Request and Save to MySQL

    Description: Fetch data from an API at regular intervals and save it to a MySQL database.

    const axios = require('axios'); const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase' }); connection.connect(); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); const data = response.data; const query = 'INSERT INTO apidata SET ?'; connection.query(query, data, (error, results) => { if (error) throw error; console.log('Data inserted into MySQL'); }); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 
  7. Node.js Periodic API Data Fetch and Save to Redis

    Description: Use setInterval to periodically fetch data from an API and save it to a Redis database.

    const axios = require('axios'); const redis = require('redis'); const client = redis.createClient(); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); client.set('apiData', JSON.stringify(response.data), redis.print); console.log('Data saved to Redis'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 
  8. Node.js Fetch API Data Periodically and Save to SQLite

    Description: Use setInterval to fetch data from an API at regular intervals and save it to a SQLite database.

    const axios = require('axios'); const sqlite3 = require('sqlite3').verbose(); const db = new sqlite3.Database('data.db'); db.serialize(() => { db.run("CREATE TABLE IF NOT EXISTS apidata (data TEXT)"); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); const stmt = db.prepare("INSERT INTO apidata VALUES (?)"); stmt.run(JSON.stringify(response.data)); stmt.finalize(); console.log('Data saved to SQLite'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds }); 
  9. Node.js Schedule API Requests and Save to MongoDB

    Description: Schedule periodic API requests and save the data to a MongoDB database using setInterval.

    const axios = require('axios'); const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017'; MongoClient.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => { if (err) throw err; const db = client.db('mydatabase'); const collection = db.collection('apidata'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); await collection.insertOne(response.data); console.log('Data saved to MongoDB'); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds }); 
  10. Node.js Use setInterval to Fetch and Log API Data

    Description: Fetch data from an API at regular intervals and log it to the console.

    const axios = require('axios'); setInterval(async () => { try { const response = await axios.get('https://api.example.com/data'); console.log('Fetched data:', response.data); } catch (error) { console.error('Error fetching data:', error); } }, 60000); // 60 seconds 

More Tags

fragment-identifier reverse-engineering shiny shared-hosting worksheet google-natural-language web recyclerview-layout pytest .net-4.5

More Programming Questions

More Chemical reactions Calculators

More Other animals Calculators

More Retirement Calculators

More Auto Calculators