UNPKG

ai-x-terminal

Version:

Enhance your command-line experience with AI capabilities using OpenAI's API. Easily integrate with projects and leverage AI in the terminal.

29 lines (28 loc) 852 B
import chalk from './chalk.js'; export const testApiKey = async (apiKey) => { try { const response = await fetch('https://api.openai.com/v1/models', { method: 'GET', headers: { Authorization: `Bearer ${apiKey}`, }, }); const res = (await response.json()); if (response.ok) { console.log('API key is valid. Models available:', res.data.map((model) => model.id)); } else { console.log(chalk.red('\nError:', res.error.message)); process.exit(1); } } catch (error) { if (error instanceof Error) { console.log(chalk.red('\nError:', error.message)); } else { console.log(chalk.red('\nError:', String(error))); } process.exit(1); } };