How to get access token using api key and secret

I tried using login section. but data is coming null in this case. how to get the access token from here.
let UpstoxClient = require(‘upstox-js-sdk’);

let apiInstance = new UpstoxClient.LoginApi();
let apiVersion = “2.0”;
let opts = {
‘code’: “{your_auth_code}”,
‘clientId’: “{your_client_secret}”,
‘clientSecret’: “{your_client_secret}”,
‘redirectUri’: “{your_redirect_url}”,
‘grantType’: “authorization_code”
};
apiInstance.token(apiVersion, opts, (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ’ + JSON.stringify(data));
}
});

Rest assured that the provided code below has been tested and matches the code found in the Example Codes - Login section.

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

let apiInstance = new UpstoxClient.LoginApi();
let apiVersion = "2.0"; 
let opts = { 
  'code': "{your_auth_code}", 
  'clientId': "{your_client_secret}", 
  'clientSecret': "{your_client_secret}", 
  'redirectUri': "{your_redirect_url}", 
  'grantType': "authorization_code" 
};
apiInstance.token(apiVersion, opts, (error, data, response) => {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + JSON.stringify(data));
  }
});

Please verify that you are using the correct values in the opts object:

  • Refer to the API Documentation for code generation.
  • Note that clientId and clientSecret refer to your API Key and API Secret respectively, which are associated with your app. You can find them under My Account -> App -> My Apps -> Your App Name.
  • Ensure that redirectUri matches the redirect URI you specified during app creation.

For detailed instructions on creating an Upstox API app, see How to create an API app. Additionally, explore the API Documentation for a deeper understanding of the Upstox API.

Hello Ketan
Please let me know the details about this.
code’: “{your_auth_code}”,
‘clientId’: “{your_client_secret}”,
‘clientSecret’: “{your_client_secret}”,
what should I give for code? i am providing API KEY as client id and secret as client secret. But I am not able to get the value for code…?

I have provided redirected url as “http://localhost:8000/” in my code and same in app. Will it work?

Please explain what should i provide for these below options?
code’: “{your_auth_code}”, ?? ‘clientId’: “{your_client_secret}”, ?? ‘clientSecret’: “{your_client_secret}”, ?? ‘redirectUri’: “{your_redirect_url}”, ??

Yes http://localhost:8000/ will work

ok good but what about code?

please guide regarding auth_code in the options.

Firstly you have to hit this url changing client_id and redirect url as per your app details

https://api.upstox.com/v2/login/authorization/dialog?response_type=code&client_id=<Your-API-Key-Here>&redirect_uri=<Your-Redirect-URI-Here>
  • This will redirect to upstox login page.

  • After completing your upstox login you will be redirected to the redirect url which you have provided. The code will be present in parameter of the given url.

For example in http://localhost:8000/?code=tXK65M, tXK65M is the auth code.

For more details please refer Upstox Authentication

Will it be constant for every request like buy, sell and getting holdings? Or I need to change it on every request?

After getting the auth_code, you can get Access Token from here.

This access token is valid for one day, and expires at 3:30 AM every morning.

Use this access token for all api calls.

how I can generate the access_code by myself if I want to use it another day?

import UpstoxClient from “upstox-js-sdk”;
import axios from “axios”;

export const authorization = async (req, res) => {
try {
let apiInstance = new UpstoxClient.LoginApi();
let apiVersion = “2.0”;
// console.log(apiInstance);
let opts = {
code: “tX…”,
clientId: “902fc7d9-…”,
clientSecret: “rno…”,
redirectUri: “http://localhost:8000/”,
grantType: “tXK…”,
};
apiInstance.token(apiVersion, opts, (error, data, response) => {
console.log(
"API called successfully. Returned data: " + JSON.stringify(data)
);
// res.status(200).send(“Auth controller working and token returned”);
});
} catch (error) {
res.status(500).send({
success: false,
message: “Error from authorization”,
error,
});
}
};

still getting error as
output: API called successfully. Returned data: null

code: “tXKxxxx”,
clientId: “902fc7d9-xxxxx-xxxxxx”,
clientSecret: “rnodxxxxxxx”,
redirectUri: “http://localhost:8000/”,
grantType: “tXKxxxx”,
};

output is
{
“status”: “error”,
“errors”: [
{
“errorCode”: “UDAPI10000”,
“message”: “This request is not supported by Upstox API”,
“propertyPath”: null,
“invalidValue”: null,
“error_code”: “UDAPI10000”,
“property_path”: null,
“invalid_value”: null
}
]
}