59API

← Back to all guides

Claude and GPT in Node.js: A TypeScript Quick Start

Guides · EN · 2026-10-29

Calling Claude and GPT from Node.js can start with one SDK, one API key, and a small TypeScript file. With 59API’s OpenAI-compatible interface, you can use the same application code to send requests to different model families. This quick start covers installation, authentication, a working request, and the checks that prevent expensive mistakes.

59API provides low-cost, pay-as-you-go access to native, official-quality models without model downgrades. Its shared API interface is useful when you want to compare Claude Sonnet 5.5 and GPT-6.1 Sol without maintaining separate SDK integrations. Actual spending still depends on your selected model, prompt size, and generated output.

Start with Node.js 22 or later. The commands below use Node’s built-in environment-file support and tsx to execute TypeScript directly. Create a new project directory, open a terminal there, and run these commands in order:

The module setting enables the import syntax and top-level await used below. You do not need a separate compilation step for this example; tsx handles execution. It does not perform TypeScript type checking.

Add your credentials. Create a file named .env in the project root with the following two entries on separate lines. Replace both placeholder values before running the script:

API_KEY_59API=your_59api_key

MODEL_ID=your_exact_model_id

Copy the exact API model identifier from 59API’s model information. A display name such as Claude Sonnet 5.5 may differ from the identifier accepted in a request. Avoid guessing an ID by lowercasing or adding hyphens. Keep .env out of version control by adding it to .gitignore, and keep API credentials on your server.

Create chat.ts. Copy the following four snippets into that file, in order. The first loads the SDK and checks the required configuration:

import OpenAI from 'openai';

const apiKey = process.env.API_KEY_59API; const model = process.env.MODEL_ID; if (!apiKey || !model) { throw new Error('Set API_KEY_59API and MODEL_ID in .env'); }

const client = new OpenAI({ apiKey, baseURL: 'https://api.59api.com/v1', timeout: 60_000, maxRetries: 1 });

const response = await client.chat.completions.create({ model, messages: [{ role: 'user', content: 'Explain JavaScript promises in three short bullet points.' }] }); console.log(response.choices[0]?.message.content ?? 'No text returned'); console.log('Token usage:', response.usage);

The service host is https://api.59api.com; the SDK configuration includes the /v1 prefix for OpenAI-compatible routes. The SDK adds the chat-completions path and authentication header. A 60-second timeout limits waiting, while one automatic retry provides a modest recovery allowance for eligible failures.

Run the file with: node --env-file=.env --import tsx chat.ts. You should receive generated text followed by usage information when the route supplies it. To compare GPT-6.1 Sol with your first model, replace MODEL_ID with its exact API identifier and rerun the same script.

Check these issues before changing your code:

For production, keep a server-side allowlist of model IDs, limit prompt size, and record latency and token usage without logging secrets or sensitive prompts. Check each model’s supported output-limit parameter before setting a generation cap. Compare input and output prices alongside answer quality: fewer requests and shorter useful answers can matter as much as the advertised token rate.

If you want one inexpensive account for experimenting across model families, sign up for 59API and start with a small balance. Move this example behind your application’s server endpoint once you have confirmed the model, response format, and cost fit your workload.

Pricing · Docs

Ready to get started?

Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.

Sign up free