59API

← Retour aux guides

Claude and GPT in Node.js: TypeScript Fixes and FAQ

Guides · EN · 2026-10-23

Calling Claude and GPT from Node.js and TypeScript

Calling an AI model from TypeScript is straightforward; debugging the wrong endpoint, model identifier, or environment variable is less obvious. This guide uses the OpenAI SDK with 59API, a low-cost, pay-as-you-go AI API relay, to show a practical setup and the checks that resolve common failures.

59API provides access to multiple model families through one service, including Claude and GPT. For example, you might evaluate Claude Sonnet 5.5 and GPT-6.1 Sol for your application. Use the exact API identifiers provided by the relay, not guessed slugs derived from these display names.

How do I configure the TypeScript client?

Use a supported Node.js LTS release, such as Node.js 22 or newer. In your project directory, run these commands:

npm init -y

npm install openai

npm install --save-dev typescript tsx @types/node

Create a local .env file containing the following variables, each on its own line. Replace the placeholders with your credentials and a model identifier copied from 59API:

API_KEY=your_59api_key

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

MODEL_ID=copy_the_exact_api_model_identifier

Check the endpoint prefix: https://api.59api.com is the service base address. The configuration above assumes its OpenAI-compatible route uses /v1. Confirm that prefix in the current dashboard or documentation before making requests. The SDK appends endpoint paths to the baseURL you supply.

Create index.mts with this minimal TypeScript example. The .mts extension makes the module format explicit:

import OpenAI from 'openai'; const apiKey = process.env.API_KEY; const baseURL = process.env.API_BASE_URL; const model = process.env.MODEL_ID; if (!apiKey || !baseURL || !model) { throw new Error('Missing API_KEY, API_BASE_URL, or MODEL_ID'); } const client = new OpenAI({ apiKey, baseURL, timeout: 60_000, maxRetries: 2 }); const result = await client.chat.completions.create({ model, messages: [{ role: 'user', content: 'Explain HTTP caching in three sentences.' }] }); console.log(result.choices[0]?.message.content ?? 'No text content returned');

Run it with: npx tsx --env-file=.env index.mts

Keep .env out of version control. In production, inject credentials through your hosting platform’s secret manager. Never expose the relay key in browser JavaScript.

Why does the request fail?

FAQ: Can I switch Claude and GPT without rewriting everything?

For basic chat requests supported by the relay’s OpenAI-compatible interface, usually yes: change MODEL_ID and keep the surrounding client code. Test response quality, latency, tool behavior, and output limits before switching production traffic. Native provider features may require different payloads or endpoints.

FAQ: Why is the response empty?

Inspect the response shape before assuming the request failed. A message can contain tool calls rather than text. With streaming enabled, consume the returned asynchronous stream instead of reading a completed response object. Log metadata during debugging, but redact secrets and sensitive prompts.

FAQ: Is 59API a practical low-cost option?

59API’s low-cost, pay-as-you-go access makes it useful for comparing providers without separate integrations. It offers native official-quality models without downgrading, plus a referral rebate. Compare current pricing, output length, and retry frequency to measure actual cost per successful task. If that fits your workflow, sign up and begin with a small test budget.

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