59API

← Back to all guides

Claude and GPT in Node.js: A TypeScript Guide for 2026

Guides · EN · 2026-10-26

Calling Claude and GPT from Node.js becomes easier when both use the same client interface. This 2026 guide uses TypeScript, the OpenAI JavaScript SDK, and 59API to make two model calls through one integration. The important details are endpoint configuration, exact model identifiers, request limits, and handling failures without accidentally multiplying costs.

1. Prepare your Node.js project

Use Node.js 24 LTS and keep your API calls on the server. Never embed a relay key in browser JavaScript, a mobile bundle, or a public repository. In a new project directory, run these commands:

npm init -y

npm pkg set type=module

npm install openai

npm install -D typescript tsx @types/node

The tsx package runs TypeScript during development. It does not perform type checking, so add a TypeScript configuration and run tsc --noEmit as a separate CI check when you turn this example into an application.

2. Configure your endpoint and model IDs

59API's service base URL is https://api.59api.com. The OpenAI SDK needs the complete API prefix: if your dashboard documents the OpenAI-compatible route under /v1, use https://api.59api.com/v1. Confirm that prefix in your account and avoid appending /v1 twice.

Create a .env file with the following entries, replacing every placeholder:

API59_KEY=your_api_key

API59_BASE_URL=https://api.59api.com/v1

CLAUDE_MODEL=exact_claude_model_id_from_dashboard

GPT_MODEL=exact_gpt_model_id_from_dashboard

Add .env to .gitignore. Choose, for example, Claude Sonnet 5.5 and GPT-6.1 Sol, then copy their exact API identifiers from 59API. Display names are not necessarily valid request IDs; do not invent identifiers by lowercasing a model name.

3. Call both models with one TypeScript function

Create src/index.ts. The following paragraphs form one complete script:

import OpenAI from 'openai';

const { API59_KEY, API59_BASE_URL, CLAUDE_MODEL, GPT_MODEL } = process.env;

if (!API59_KEY || !API59_BASE_URL || !CLAUDE_MODEL || !GPT_MODEL) throw new Error('Missing required API configuration');

const client = new OpenAI({ apiKey: API59_KEY, baseURL: API59_BASE_URL, timeout: 45_000, maxRetries: 2 });

async function ask(model: string, prompt: string) { const result = await client.chat.completions.create({ model, messages: [{ role: 'user', content: prompt }] }); return result.choices[0]?.message.content ?? ''; }

for (const model of [CLAUDE_MODEL, GPT_MODEL]) { console.log(model, await ask(model, 'Explain database connection pooling in three sentences.')); }

Run it with:

node --env-file=.env --import=tsx src/index.ts

This makes two sequential, billable requests. Keeping the prompt identical helps you compare output quality, response time, and usage. Chat Completions provides a familiar interface, but compatibility does not guarantee identical support for every optional parameter or provider feature.

4. Handle errors and streaming deliberately

Catch OpenAI.APIError at your application's request boundary and record the status, error code, and request ID when available. Authentication failures usually require checking the key; missing-model errors require checking the identifier and account access. Rate limits call for reduced concurrency and backoff.

The configured retry setting permits up to two retries for eligible failures. Avoid stacking another unrestricted retry loop around it. A timeout can occur after processing has started, so a repeated request may create additional usage.

For interactive interfaces, add stream: true and consume the returned stream with for await. Read text from each chunk's choices[0]?.delta?.content and handle chunks without text. Stop upstream work when the user disconnects by passing an AbortSignal in the SDK request options.

5. Keep costs measurable

59API offers inexpensive, pay-as-you-go access across multiple model families and describes its models as native, official-quality offerings without downgrades. Its shared SDK interface makes it convenient to compare Claude and GPT without maintaining separate client integrations.

Check current input, output, and caching prices before choosing a default model. Record returned usage fields where available, cap conversation history, and apply the output limit supported by each selected route. Measure cost per successful task alongside latency and answer quality.

Sign up for 59API and start with a small evaluation budget. Its referral rebate can provide additional savings, while application-level budgets and concurrency limits keep everyday spending predictable.

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