Get Token
Get access token using auth code
- Curl
- Python
- Node.js
- Java
- PHP
- Python SDK
- Node.js SDK
- Java SDK
curl -X 'POST' 'https://api.upstox.com/v2/login/authorization/token' \
-H 'accept: application/json' \
-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'
import requests
url = 'https://api.upstox.com/v2/login/authorization/token'
headers = {
'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
'code': '{your_code}',
'client_id': '{your_client_id}',
'client_secret': '{your_client_secret}',
'redirect_uri': '{your_redirect_url}',
'grant_type': 'authorization_code',
}
response = requests.post(url, headers=headers, data=data)
print(response.status_code)
print(response.json())
const axios = require('axios');
const url = 'https://api.upstox.com/v2/login/authorization/token';
const headers = {
'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
};
const data = {
'code': '{your_code}',
'client_id': '{your_client_id}',
'client_secret': '{your_client_secret}',
'redirect_uri': '{your_redirect_url}',
'grant_type': 'authorization_code',
};
axios.post(url, new URLSearchParams(data), { headers })
.then(response => {
console.log(response.status);
console.log(response.data);
})
.catch(error => {
console.error(error.response.status);
console.error(error.response.data);
});
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws Exception {
String apiUrl = "https://api.upstox.com/v2/login/authorization/token";
HttpURLConnection con = (HttpURLConnection) new java.net.URL(apiUrl).openConnection();
// Set the request method
con.setRequestMethod("POST");
// Set the request headers
con.setRequestProperty("accept", "application/json");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// Enable input/output streams
con.setDoOutput(true);
// Set the request data
String data = "code={your_code}" +
"&client_id={your_client_id}" +
"&client_secret={your_client_secret}" +
"&redirect_uri={your_redirect_url}" +
"&grant_type=authorization_code";
// Write the request data to the output stream
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.write(data.getBytes(StandardCharsets.UTF_8));
wr.flush();
}
// Get the response code
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);
// Read the response
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// Print the response
System.out.println(response.toString());
}
}
}
<?php
$url = 'https://api.upstox.com/v2/login/authorization/token';
$headers = [
'accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
];
$data = [
'code' => '{your_code}',
'client_id' => '{your_client_id}',
'client_secret' => '{your_client_secret}',
'redirect_uri' => '{your_redirect_uri}',
'grant_type' => 'authorization_code',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "Response Code: $httpCode\n";
echo "Response Data: $response\n";
?>
import upstox_client
from upstox_client.rest import ApiException
api_instance = upstox_client.LoginApi()
api_version = '2.0'
code = '{your_auth_code}'
client_id = '{your_client_id}'
client_secret = '{your_client_secret}'
redirect_uri = '{your_redirect_url}'
grant_type = 'grant_type_example'
try:
# Get token API
api_response = api_instance.token(api_version, code=code, client_id=client_id, client_secret=client_secret,
redirect_uri=redirect_uri, grant_type=grant_type)
print(api_response)
except ApiException as e:
print("Exception when calling LoginApi->token: %s\n" % e)
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));
}
});
import com.upstox.ApiException;
import com.upstox.api.TokenResponse;
import io.swagger.client.api.LoginApi;
public class Main {
public static void main(String[] args) {
LoginApi apiInstance = new LoginApi();
String apiVersion = "2.0";
String code = "{your_code}";
String clientId = "{your_clientId}";
String clientSecret = "{your_clientSecret}";
String redirectUri = "{your_redirect_url}";
String grantType = "authorization_code";
try {
TokenResponse result = apiInstance.token(apiVersion, code, clientId, clientSecret, redirectUri, grantType);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LoginApi#token");
e.printStackTrace();
}
}
}