@bradtjohnson1/asksage-token-generator
v1.2.0
Published
Secure utility for generating and managing Ask Sage API access tokens
Downloads
16
Maintainers
Readme
Ask Sage Token Generator (JavaScript/Node.js)
A Node.js utility to generate and manage access tokens for the Ask Sage API.
Features
- Interactive credential setup
- Secure token storage (OS keychain or file-based)
- Automatic token validation
- Token caching with 65-minute expiration (1 hour 5 minutes)
- Easy integration with other JavaScript applications
Prerequisites
- Node.js 14.0.0 or higher
- npm (comes with Node.js)
- Ask Sage account with API key
Quick Start
Installation & Setup
Option 1: Automated Setup (Windows)
setup.batThis will:
- Check Node.js installation
- Install npm dependencies
- Show instructions to run
npm start
Option 2: Automated Setup (Linux/macOS/Node.js)
node setup.jsThis will:
- Check Node.js version
- Install npm dependencies
- Show instructions to run
npm start
Option 3: Manual Setup
npm installGenerate a Token
npm startThe script will:
- Ask for your Ask Sage email
- Ask for your Ask Sage API key
- Save credentials to
credentials.json - Request token from Ask Sage API
- Validate the token
- Save token securely (OS keychain or file)
- Create
asksage_config.json
Regenerate Token (credentials already saved)
npm startThe script will use your saved credentials automatically.
Getting Your API Key
- Go to https://asksage.ai
- Log in to your account
- Navigate to Account Settings
- Click "Manage your API keys"
- Create a new API key or copy an existing one
Using the Token
In API Requests
const axios = require('axios');
const headers = {
'x-access-tokens': 'your_token_here',
'Content-Type': 'application/json'
};
const response = await axios.post(
'https://api.asksage.ai/server/get-models',
{},
{ headers }
);As a Module in Your Code
const { getStoredToken, getToken } = require('./asksage_token');
// Get stored token
const token = await getStoredToken('[email protected]');
// Or generate new token
const newToken = await getToken('[email protected]', 'your_api_key');Using Environment Variable
The script sets the ASK_SAGE_TOKEN environment variable for the current session:
const token = process.env.ASK_SAGE_TOKEN;Files Created
After running the token generator:
credentials.json- Your email and API key (gitignored)asksage_config.json- Config with metadata (gitignored)~/asksage_token.txt- Token file (if keytar not available)
File Structure
javascript/
├── asksage_token.js # Main token generator script
├── package.json # npm package configuration
├── setup.bat # Windows setup script
├── setup.js # Cross-platform setup script
├── credentials.json.example # Example credentials file
├── asksage_config.json.example # Example config file
├── .gitignore # Git ignore rules
└── README.md # This fileSecurity
Token Storage
The script uses multiple security layers:
OS Keychain (Preferred): If
keytaris installed, tokens are stored in your operating system's credential manager:- macOS: Keychain
- Windows: Credential Vault
- Linux: Secret Service API/libsecret
File-based (Fallback): Tokens are stored in
~/asksage_token.txtwith restricted permissions (600)
Installing Keytar (Optional but Recommended)
For enhanced security with OS keychain storage:
npm install keytarNote: keytar requires native build tools on some systems. If installation fails, the script will fall back to file-based storage.
Best Practices
- Never commit
credentials.jsonto version control - Keep your API key secure
- Tokens expire after 65 minutes (1 hour 5 minutes)
- Use environment variables in production
Token Expiration
Ask Sage tokens expire after 65 minutes (1 hour 5 minutes). When your token expires:
- Run
npm startagain to generate a new token - The script will automatically test the new token
- Your credentials remain saved for easy regeneration
Troubleshooting
"Node.js not found"
Install Node.js 14.0.0 or higher from https://nodejs.org
"Authentication failed"
- Verify your email and API key in
credentials.json - Ensure your API key is active and has the correct permissions
"keytar not available"
- This is normal if keytar isn't installed
- The script will use file-based storage instead
- To install keytar (optional):
npm install keytar
"Permission denied"
- On Unix systems: Ensure you have write permissions to your home directory
- On Windows: Run as administrator if needed
"Request timed out"
- Check your internet connection
- Verify the API endpoints are accessible
Support
For issues or questions:
- Check the main README in the parent directory
- Visit https://asksage.ai for API documentation
- Contact Ask Sage support
License
MIT
