Cannot get Token in node js

I cannot get Token in node js with upstox-js-sdk as well as with API.

const UpstoxClient =require (‘upstox-js-sdk’)
let apiInstance = new UpstoxClient.LoginApi();
let apiVersion = “2.0”;
let opts = {
“code”: “*******”,
“client_id”: “***",
“client_secret”: "
”,
“redirect_uri”: “https://moneymakers-algo.com”,
“grant_type”: “authorization_code”
};

apiInstance.token(apiVersion, opts, (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ’ + data);
}
});

const url = ‘https://api-v2.upstox.com/login/authorization/token’;
const headers = {
‘Accept’: ‘application/json’,
‘Api-Version’: ‘2.0’,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
};

const data = {
“code”: “",
“client_id”: “***********”,
“client_secret”: "
”,
“redirect_uri”: “link”,
“grant_type”: “authorization_code”
};

fetch(url, {
method: ‘POST’,
headers: headers,
body: data,
})
.then(response => response.json())
.then(data => {
console.log(‘Response:’, data);
// Handle the response data
})
.catch(error => {
console.error(‘Error:’, error);
// Handle fetch errors
});

Could you please share the exact error or a screenshot of the error you’re encountering while trying to obtain the access token?

I suggest using the cURL command to verify whether you can successfully generate an access token. This will help ensure that all parameters are correctly set:

curl -X 'POST' 'https://api.upstox.com/v2/login/authorization/token' \
-H 'accept: application/json' \
-H 'Api-Version: 2.0' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'code={your_code}&client_id={your_client_id}&client_secret={your_client_secret}&redirect_uri={your_redirect_url}&grant_type=authorization_code'

Feel free to attempt the following, and please inform me about the outcome. The code provided below has been verified and is operational

let UpstoxClient = require('upstox-js-sdk');

let apiInstance = new UpstoxClient.LoginApi();
let apiVersion = "2.0"; 
let opts = { 
  'code': "{your_atuh_code}", 
  'clientId': "{ypur_client_code}",
  'clientSecret': "{ypur_client_secret}", 
  'redirectUri': "{your_redirect_uri}",
  'grantType': "authorization_code" 
};
apiInstance.token(apiVersion, opts, (error, data, response) => {
  if (error) {
    console.error(error);
  } else {
    console.log('Returned data: ' + JSON.stringify(data));
  }
});

here is the error

Error: Unauthorized
at Request.callback (C:\Users\rozod\OneDrive\Desktop\money makers\backend\node_modules\superagent\lib\node\index.js:883:15)
at C:\Users\rozod\OneDrive\Desktop\money makers\backend\node_modules\superagent\lib\node\index.js:1126:20
at IncomingMessage. (C:\Users\rozod\OneDrive\Desktop\money makers\backend\node_modules\superagent\lib\node\parsers\json.js:22:7)
at IncomingMessage.emit (node:events:526:35)
at endReadableNT (node:internal/streams/readable:1589:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
status: 401,
response: <ref *1> Response {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
res: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 4,
_maxListeners: undefined,
socket: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: ‘1.1’,
complete: true,
rawHeaders: [Array],
rawTrailers: ,
joinDuplicateHeaders: undefined,
aborted: false,
upgrade: false,
url: ‘’,
method: null,
statusCode: 401,
statusMessage: ‘Unauthorized’,
client: [TLSSocket],
_consuming: true,
_dumped: false,
req: [ClientRequest],
text: ‘{“status”:“error”,“errors”:[{“errorCode”:“UDAPI100057”,“message”:“Invalid Auth code”,“propertyPath”:null,“invalidValue”:null,“error_code”:“UDAPI100057”,“property_path”:null,“invalid_value”:null}]}’,
[Symbol(kCapture)]: false,
[Symbol(kHeaders)]: [Object],
[Symbol(kHeadersCount)]: 42,
[Symbol(kTrailers)]: null,
[Symbol(kTrailersCount)]: 0
},

Ensure you use the correct auth code. It’s crucial to understand that this code is for one-time use only; once used, it will become invalid and may cause the “Invalid Auth code” issue.

Thank you. It’s working with Api now.