How to handle access token expiry in a SuiteScript API call

I encountered some errors when making API requests using ‘Access tokens’ generated from a refresh token within SuiteScript. The issue arose as the access token expired during API calls, causing the next API calls to fail until a new token was issued. This can be solved using a code to create a new token and store it in the cache memory whenever there is an access token expiry error. Call the request within this function.

const retryApiRequest = (requestFunc, ...args) => {
            let result = requestFunc(...args);
            if (!result || result.error) {
                let accessTokenValue = generateNewToken(refreshToken, clientId, clientSecret);
                return requestFunc(accessTokenValue, ...args.slice(1));
            }
            return result;
        }

Leave a comment

Your email address will not be published. Required fields are marked *