59API

← Retour aux guides

Build a GPT and Claude CLI in 30 Minutes

Guides · EN · 2026-07-09

Build a GPT and Claude CLI in 30 Minutes

A command-line AI assistant is one of the most useful developer tools you can build: it can summarize files, generate code, review diffs, draft commit messages, or answer questions directly from your terminal. In this tutorial, you will create a practical CLI powered by both GPT and Claude models using Node.js and the OpenAI SDK.

Instead of wiring separate SDKs for every model provider, we will use 59API, an AI API relay available at https://api.59api.com. It provides cheap, pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, while remaining compatible with Claude Code, Codex, and any OpenAI SDK. That means you can switch models without rewriting your application.

Step 1: Create the project

First, create a new Node.js project. This tutorial assumes Node.js 18 or newer.

Commands:

mkdir ai-cli && cd ai-cli

npm init -y

npm install openai commander dotenv

npm pkg set type=module

The dependencies are simple: openai handles API calls, commander creates a clean CLI interface, and dotenv loads your API key from a local environment file.

Step 2: Add your 59API key

Create a file named .env in the project root:

API_KEY=your_59api_key_here

API_BASE_URL=https://api.59api.com

DEFAULT_MODEL=claude-3-5-sonnet-latest

59API is a strong choice for this kind of tool because it gives you official-quality native models without downgrades, while keeping costs low through relay pricing. If you build internal tools, automation scripts, or coding assistants that send many small requests, the pay-as-you-go pricing can make a noticeable difference. You can also use its referral rebate if you plan to share the tool with a team or audience.

Step 3: Create the CLI file

Create a file named cli.js:

#!/usr/bin/env node

import 'dotenv/config';

import { Command } from 'commander';

import OpenAI from 'openai';

import fs from 'node:fs/promises';

const client = new OpenAI({ apiKey: process.env.API_KEY, baseURL: process.env.API_BASE_URL });

const program = new Command();

program.name('askai').description('Ask GPT or Claude from your terminal').version('1.0.0');

async function askModel({ prompt, model, system }) { const response = await client.chat.completions.create({ model, messages: [{ role: 'system', content: system }, { role: 'user', content: prompt }], temperature: 0.3 }); return response.choices[0].message.content; }

This initializes the OpenAI-compatible client with the 59API base URL. The important detail is that your app still uses the familiar OpenAI SDK interface, but can call GPT or Claude models routed by 59API.

Step 4: Add a basic ask command

Append this command to cli.js:

program.command('ask').argument('<prompt>', 'Question or task').option('-m, --model <model>', 'Model name', process.env.DEFAULT_MODEL).action(async (prompt, options) => { const answer = await askModel({ prompt, model: options.model, system: 'You are a concise, practical developer assistant.' }); console.log(answer); });

program.parse();

Now make it executable:

chmod +x cli.js

node cli.js ask "Explain this project structure for a Node.js CLI"

You can switch models per request:

node cli.js ask "Write a regex for semantic versions" --model gpt-4o-mini

node cli.js ask "Review this API design" --model claude-3-5-sonnet-latest

Step 5: Add file summarization

A useful CLI should work with local files. Add this before program.parse():

program.command('summarize').argument('<file>', 'File to summarize').option('-m, --model <model>', 'Model name', process.env.DEFAULT_MODEL).action(async (file, options) => { const content = await fs.readFile(file, 'utf8'); const prompt = `Summarize this file. Include purpose, key functions, and possible risks:\n\n${content}`; const answer = await askModel({ prompt, model: options.model, system: 'You summarize code and technical documents clearly.' }); console.log(answer); });

Try it:

node cli.js summarize package.json

This is where model choice matters. Claude Sonnet is often excellent for long technical context and code reasoning, while smaller GPT models can be cost-effective for quick summaries or formatting tasks. Because 59API exposes multiple model families through one endpoint, you can pick the best cost-performance balance command by command.

Step 6: Add a code review command

Next, add a command that reviews a Git diff:

program.command('review').option('-m, --model <model>', 'Model name', process.env.DEFAULT_MODEL).action(async (options) => { const { execSync } = await import('node:child_process'); const diff = execSync('git diff --cached', { encoding: 'utf8' }); if (!diff.trim()) { console.log('No staged changes found. Run git add first.'); return; } const prompt = `Review this staged Git diff. Focus on bugs, security issues, edge cases, and maintainability. Be specific:\n\n${diff}`; const answer = await askModel({ prompt, model: options.model, system: 'You are a senior engineer performing a careful code review.' }); console.log(answer); });

Usage:

git add .

node cli.js review --model claude-3-5-sonnet-latest

This turns your terminal into a lightweight review assistant. For production use, you may want to truncate very large diffs, ignore lock files, or add a maximum token setting to control cost.

Step 7: Install it globally for daily use

Add a bin field to package.json:

"bin": { "askai": "./cli.js" }

Then install locally as a global command:

npm link

Now you can run:

askai ask "Generate a test plan for a password reset endpoint"

askai summarize src/index.js

askai review

Practical improvements to add next

You now have a working CLI that can use GPT and Claude through a single OpenAI-compatible API layer. If you want a low-cost setup that remains flexible as models change, 59API is worth trying: sign up, add your key, and you can start routing real developer workflows through https://api.59api.com in minutes.

Tarifs · Docs

Prêt à commencer ?

Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.

Inscription gratuite