Smart API only working on Terminal. No where else?


  • Creating a index.js file with code below and running it with command node index.js on terminal works perfectly. I get the jwtToken and holding details using that jwtToken.

    async function fetchData() {
      
      var data = JSON.stringify({
        clientcode: "MyClientCode",
        password: "MyPassword",
        totp: "123456",
      });
    
      const loginResponse = await fetch("https://apiconnect.angelone.in/rest/auth/angelbroking/user/v1/loginByPassword", {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
          "X-ClientLocalIP": "",
          "X-ClientPublicIP": "",
          "X-MACAddress": "",
          "X-UserType": "USER",
          "X-SourceID": "WEB",
          "X-PrivateKey": "myApiKey",
        },
        body: data,
      });
    
      const loginResponseData = await loginResponse.json();
      
      console.log("Got Response:", loginResponseData);
    
      if (loginResponseData.status === true) {
        jwtToken = loginResponseData.data.jwtToken;
        const holdingResponse = await fetch("https://apiconnect.angelone.in/rest/secure/angelbroking/portfolio/v1/getHolding", {
          method: "GET",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            "X-ClientLocalIP": "",
            "X-ClientPublicIP": "",
            "X-MACAddress": "",
            "X-UserType": "USER",
            "X-SourceID": "WEB",
            "X-PrivateKey": "myApiKey",
            "Authorization": `Bearer ${jwtToken}`,
          },
        });
        const holdingResponseData = await holdingResponse.json();
        console.log("Got Holding:", holdingResponse.data);
      } else {
        console.log("Login Error");
      }
    }
    
    fetchData();
    

    But the same code does not work on any web framework backend. I tried using Express and NUXT which are based on node.js.
    Infact i tried in different languages like php, c#, flutter but all fail in login endpoint giving response

    {
      success: false,
      message: 'Invalid Token',
      errorCode: 'AG8001',
      data: ''
    }
    

    I am not sure what is the issue. Please any solution for this.


  • @aasiph express.js code

    const bodyParser = require('body-parser')
    const cors = require('cors')
    const express = require('express')
    
    const app = express()
    const host = 'localhost'
    const port = 3001
    
    ;(async () => {
      try {
        app.use(cors())
        app.use(bodyParser.json())
    
        app.listen(port).on('listening', () => {
          console.log(
            'App server started on http://%s:%d',
            host,
            port
          )
        })
    
        process.on('unhandledRejection', (reason) => {
          console.log('Unhandled rejection:')
          console.log(reason)
        })
    
        await fetchData()
      } catch (error) {
        console.log('ERR:')
        console.log(error)
      }
    })()