--- title: "Configure the Job function" url: https://docs.catalyst.zoho.com/en/tutorials/newsapp/nodejs/configure-job-function/ description: "Build a web news application that automatically fetches news from a third-party service periodically using the Catalyst Job Scheduling Cron component, and a Job Function and Advanced I/O function coded in Node.js." last_updated: 2026-08-17T12:16:04Z service: All Services type: tutorial related_links: - name: "Job Scheduling" url: /en/job-scheduling/getting-started/introduction/ - name: "Job Functions" url: /en/serverless/help/functions/job-functions/ --- # Configure the Job Function Next, we'll begin coding the news application by configuring the job function component. The function's directory, in this case `functions/NewsApp`, contains: * The `index.js` main function file * The `catalyst-config.json` configuration file * Node modules * [`package.json`](https://docs.npmjs.com/files/package.json) and [`package-lock.json`](https://docs.npmjs.com/configuring-npm/package-lock-json.html) dependency files We will be adding code in the `index.js` file. You will not be modifying the code of the configuration and dependency files. As mentioned in the [introduction](https://docs.catalyst.zoho.com/en/tutorials/newsapp/nodejs/introduction/), the job function performs two tasks: making the API calls to the [NewsAPI](https://newsapi.org/) to fetch news, populating the news in Catalyst Data Store. The API calls are made using an API key provided by NewsAPI. ### Register with NewsAPI Before you code the job function, you must register for a free developer subscription with NewsAPI and obtain the API key in the following way: 1. Visit [https://newsapi.org/register](https://newsapi.org/register). 2. Provide the required details and click **Submit**. ![register-api-key](https://catalyst-docs-development.zohostratus.in/images/tutorials/cron/register-api-key.webp) After your registration is complete, NewsAPI will provide you with an API key. You must use this in your cron function, as instructed after the code section. ![api-key](https://catalyst-docs-development.zohostratus.in/images/tutorials/cron/api-key.webp) ### Install Packages for Node.js The Node.js Cron function requires two packages to be installed: [`express`](https://www.npmjs.com/package/express) and [`axios`](https://www.npmjs.com/package/axios). `express` We will use the Express framework to manage the routing operations that enable us to fetch and delete files. To install `express`, navigate to the function's directory (`functions/NewsApp`) in your terminal and execute the following command: ```sh npm install express ``` This will install the Express module and save the dependencies. ![express](https://catalyst-docs-development.zohostratus.in/images/tutorials/cron/express.webp) `axios` `axios` is a promise-based HTTP client that we will use to fetch data by executing the NewsAPI. To install `axios`, navigate to the Node function's directory (`functions/NewsApp`) and execute the following command: ```sh npm install axios ``` This will install the module. ![axios](https://catalyst-docs-development.zohostratus.in/images/tutorials/cron/axios.webp) You can copy the code below and paste it in index.js located in the `functions/NewsApp` directory of your project and save the file. You can use any IDE to work with the application's files. > **Note**: Please go through the code given in this section to make sure you fully understand it. **index.js** ```javascript /** * * @param {import("./types/job").JobRequest} jobRequest * @param {import("./types/job").Context} context */ const catalyst = require("zcatalyst-sdk-node"); const axios = require('axios'); const HOST = "https://newsapi.org"; const TABLENAME = [ "HEADLINES", "BUSINESS", "ENTERTAINMENT", "HEALTH", "SCIENCE", "SPORTS", "TECHNOLOGY", ]; const COUNTRY = "US"; const APIKEY = "cc30ddcae1684e848f08ed474f6aaf0e"; const DATASTORE_LOAD = 5; const pushNewstoDatastore = async ({ table, rowIds, articles }) => { return Promise.all( articles.map(async (article, idx) => { const payload = { title: article.title, url: article.url, }; if (rowIds.length === 0) { //insert the new row return table.insertRow(payload); } return table.updateRow({ ...payload, ROWID: rowIds[idx] }); }) ); }; module.exports = async (jobRequest, context) => { try{ console.log('Execution started'); const catalystApp = catalyst.initialize(context); const zcqlAPI = catalystApp.zcql(); const datastoreAPI = catalystApp.datastore(); const metaArr = await Promise.all( TABLENAME.map(async (table) => { //construct request path to newsapi let url = `${HOST}/v2/top-headlines?country=${COUNTRY}&apiKey=${APIKEY}`; if (table !== TABLENAME[0]) { url += "&category=" + table; } const response = await axios({ method: "GET", url }); let data = response.data; //query using zcql to check if row exists const queryResult = await zcqlAPI.executeZCQLQuery( `SELECT ROWID FROM ${table}` ); return { table_name: table, table: datastoreAPI.table(table), zcql_response: queryResult, articles: data.articles.splice(0, 15), }; }) ); //sync insert/update to datastore for (const meta of metaArr) { let rowIds = []; while (meta.articles.length > 0) { const chunk = meta.articles.splice(0, DATASTORE_LOAD); if (meta.zcql_response.length > 0) { rowIds = meta.zcql_response .splice(0, DATASTORE_LOAD) .map((row) => row[meta.table_name].ROWID); } await pushNewstoDatastore({ ...meta, articles: chunk, rowIds }); } console.log( `${meta.table} table ${ rowIds.length === 0 ? "inserted" : "updated" } with current news'` ); } context.closeWithSuccess(); //end of application with success }catch(err){ console.log("Error :: "+err); context.closeWithFailure(); //end of application with failure }; }; ``` > **Note**: After you copy and paste this code in your function file, ensure that you replace value of `APIKEY` in the line **19** with the API key you obtained from NewsAPI. The job function is now configured. We will discuss the application's architecture after we configure the client.