Skip to navigation

Renew Access Token

View as Markdown

Request a renewal for an existing access token.

Extends the current session without creating a new one. Using this function in a long-running application will ensure that you don’t start a new session unless it is 100% necessary. This will prevent your application from kicking itself or dropping dependent services that share an Access Token. See the /auth/accessTokenRequest operation for more details.

const { accessToken, expirationTime } = await fetch(TRADOVATELIVE+'/auth/accessTokenRequest', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(myCredentials) //see auth/accessTokenRequest for more info on the request body
}).then(res => res.json());
sessionStorage.setItem(CURRENT_TOKEN_KEY, accessToken);
sessionStorage.setItem(TOKEN_EXPIRATION_KEY, expirationTime);
//in another file or function, keep the connection alive
//check every 2 mins using useInterval (WARNING on a browser
//this fn will be throttled. Consider using a WebWorker to maintain
//a connection even when tabbed away.)
setInterval(() => {
const expiration = sessionStorage.getItem(TOKEN_EXPIRATION_KEY);
const now = new Date().getTime();
const exp = new Date(expiration).getTime();
//if expiration is within 15 mins, renew
if(exp - now < 15 * 60000) {
const currentToken = sessionStorage.getItem(CURRENT_TOKEN_KEY);
const renewal = await fetch(TRADOVATELIVE+'/auth/renewAccessToken', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${currentToken}` //send existing non-expired token
}
});
//set new token and expiration
sessionStorage.setItem(CURRENT_TOKEN_KEY, renewal.accessToken);
sessionStorage.setItem(TOKEN_EXPIRATION_KEY, renewal.expirationTime);
}
}, 120 * 60000)

Authentication

AuthorizationBearer

Bearer authentication of the form Bearer <token>, where token is your auth token.

Response

AccessTokenResponse
errorTextstringOptional<=8192 characters

Non-empty if the request failed

hibpHintenumOptional
EmailAndPasswordCompromised, PasswordCompromised
Allowed values:
accessTokenstringOptional<=8192 characters
expirationTimedatetimeOptional
passwordExpirationTimedatetimeOptional
userStatusenumOptional
Active, Closed, Initiated, TemporaryLocked, UnconfirmedEmail
Allowed values:
userIdlongOptional
namestringOptional<=64 characters
hasLivebooleanOptional
hasSimPlusbooleanOptional
showKIDsbooleanOptional