title: Azure Function to only run when token response has expired
body: <p>I have an azure function that simply runs an API call to fetch an AD Access Token.  My tokens are set to have a life of 1 hour.</p>
  <p>Currently each time I call the Function (via http) it makes a new API call to get a new Access Token.  Ideally I would like the function to only make a new API call if the previous Access Token has expired or is close to expiring (within 10 mins).</p>
  <p>Here is my function script:</p>
  <pre><code>const axios = require('axios').default;
  
  module.exports = async function (context, req) {
      context.log('FetchToken HTTP trigger function processed a request.');
  
      const tenant_id = GetConfig('tenant_id');
      const client_id = GetConfig('client_id');
      const client_secret = GetConfig('client_secret');
      const resource = GetConfig('resource');
      const url = GetConfig('url').replace(&quot;#tenant_id#&quot;, tenant_id);
  
      var data = `grant_type=client_credentials&amp;client_id=${client_id}&amp;client_secret=${client_secret}&amp;resource=${resource}`
  
      var config = {
          method: 'post',
          url,
          headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
          },
          data
      };
      try {
          var res = await axios(url, config);
          if (res.status == 200) {
              const expires_on_date_time = new Date(res.data.expires_on * 1000).toLocaleString();
              const issue_on_date_time = new Date(res.data.not_before * 1000).toLocaleString();
              context.res = {
                  status: 200,
                  body: {
                      expires_on_date_time,
                      issue_on_date_time,
                      ...res.data
                  }
              };
          }
      } catch (error) {
  
          context.res = {
              status: 400,
              body: error
          };
      }
  }
  
  function GetConfig(configName) {
      return process.env[ configName ]
  }
  </code></pre>
  <p>...and here is a sample response:</p>
  <pre><code>{
    &quot;expires_on_date_time&quot;: &quot;3/8/2022, 6:22:17 PM&quot;,
    &quot;issue_on_date_time&quot;: &quot;3/8/2022, 5:17:17 PM&quot;,
    &quot;token_type&quot;: &quot;Bearer&quot;,
    &quot;expires_in&quot;: &quot;3599&quot;,
    &quot;ext_expires_in&quot;: &quot;3599&quot;,
    &quot;expires_on&quot;: &quot;1646763737&quot;,
    &quot;not_before&quot;: &quot;1646759837&quot;,
    &quot;resource&quot;: &quot;https://analysis.windows.net/powerbi/api&quot;,
    &quot;access_token&quot;: &quot;eyJ0eXAiOiJKV1QiLCJ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QX4nYpiFHJiewy93Fjv6P3n8W3flkZ64Vrte_qeeEeLN3cnPA0DZJTc-4dHI5fY2HJxAnVXQzSUSwNQlLysMjTIy0tbn458kU65Q7O3-EZfRKtYRoTgmuilwQqCqqrylJt4dVNWbRSNgGZszWauanFOu7b7ESXukobeaE06nl1FGb0s_Ackw&quot;
  }
  </code></pre>
  <p>I would like the Function to first check the expiry of the previous call's token and if it's within 10 mins of expiring then fetch a new token.</p>
  <p>Any pointers?</p>