---
# System prepended metadata

title: Query 169

---

Create a JavaScript script that checks the expiration date of a GitHub PAT. The script should determine if the token is about to expire and notify the user to renew it. Implement a countdown mechanism to notify users when the token is close to expiration [1][2]:

First, make sure you have Node.js installed and create a new Node.js project. You can do this by running npm init and installing the required packages:
```
npm install @octokit/rest moment
```
Now, create a JavaScript file, e.g., github_token_checker.js, and add the following code:
```
const Octokit = require('@octokit/rest');
const moment = require('moment');

// Your GitHub Personal Access Token
const PAT = 'YOUR_GITHUB_PAT';

const octokit = new Octokit({
  auth: PAT,
});

async function checkTokenExpiration() {
  try {
    const tokenInfo = await octokit.apps.getAuthenticated();
    const tokenExpiration = moment(tokenInfo.data.expires_at);
    const now = moment();

    const daysUntilExpiration = tokenExpiration.diff(now, 'days');
    const hoursUntilExpiration = tokenExpiration.diff(now, 'hours');

    if (daysUntilExpiration <= 7) {
      console.log(`Your GitHub PAT will expire in ${daysUntilExpiration} days and ${hoursUntilExpiration % 24} hours.`);
      // You can add code here to notify the user to renew the token
    } else {
      console.log(`Your GitHub PAT is valid for another ${daysUntilExpiration} days and ${hoursUntilExpiration % 24} hours.`);
    }
  } catch (error) {
    console.error('Error checking token expiration:', error.message);
  }
}

checkTokenExpiration();
```
Replace `YOUR_GITHUB_PAT` with your actual GitHub Personal Access Token.
This script uses the Octokit library to get information about the authenticated app, which includes the token's expiration date. It then calculates the number of days and hours remaining until the token expires. If the token has less than 7 days remaining, it notifies the user to renew it. You can customize the notification method as needed.
To run the script, simply execute it using Node.js:
```
Make sure to run this script periodically, such as with a scheduled task or a cron job, to receive notifications when the token is about to expire.
```